aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>2015-07-28 15:45:15 -0400
committerJonathan Corbet <corbet@lwn.net>2015-08-17 00:11:16 -0400
commit5699f871d2d51ce40012501378670613d4d49214 (patch)
treeb35b519696abf5a24c3ee4de623e0851607103c5
parent9ed71e7ad95eb9885420e8d33f33bd4d49e1b775 (diff)
scripts/kernel-doc: Adding cross-reference links to html documentation.
Functions, Structs and Parameters definitions on kernel documentation are pure cosmetic, it only highlights the element. To ease the navigation in the documentation we should use <links> inside those tags so readers can easily jump between methods directly. This was discussed in 2014[1] and is implemented by getting a list of <refentries> from the DocBook XML to generate a database. Then it looks for <function>,<structnames> and <paramdef> tags that matches the ones in the database. As it only links existent references, no broken links are added. [1] - lists.freedesktop.org/archives/dri-devel/2014-August/065404.html Signed-off-by: Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: Stephan Mueller <smueller@chronox.de> Cc: Michal Marek <mmarek@suse.cz> Cc: intel-gfx <intel-gfx@lists.freedesktop.org> Cc: dri-devel <dri-devel@lists.freedesktop.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
-rw-r--r--Documentation/DocBook/Makefile43
-rwxr-xr-xscripts/kernel-doc-xml-ref198
2 files changed, 228 insertions, 13 deletions
diff --git a/Documentation/DocBook/Makefile b/Documentation/DocBook/Makefile
index 5e9702194dfe..b1d6c951ea29 100644
--- a/Documentation/DocBook/Makefile
+++ b/Documentation/DocBook/Makefile
@@ -66,8 +66,9 @@ installmandocs: mandocs
66 66
67### 67###
68#External programs used 68#External programs used
69KERNELDOC = $(srctree)/scripts/kernel-doc 69KERNELDOCXMLREF = $(srctree)/scripts/kernel-doc-xml-ref
70DOCPROC = $(objtree)/scripts/docproc 70KERNELDOC = $(srctree)/scripts/kernel-doc
71DOCPROC = $(objtree)/scripts/docproc
71 72
72XMLTOFLAGS = -m $(srctree)/$(src)/stylesheet.xsl 73XMLTOFLAGS = -m $(srctree)/$(src)/stylesheet.xsl
73XMLTOFLAGS += --skip-validation 74XMLTOFLAGS += --skip-validation
@@ -91,7 +92,7 @@ define rule_docproc
91 ) > $(dir $@).$(notdir $@).cmd 92 ) > $(dir $@).$(notdir $@).cmd
92endef 93endef
93 94
94%.xml: %.tmpl $(KERNELDOC) $(DOCPROC) FORCE 95%.xml: %.tmpl $(KERNELDOC) $(DOCPROC) $(KERNELDOCXMLREF) FORCE
95 $(call if_changed_rule,docproc) 96 $(call if_changed_rule,docproc)
96 97
97# Tell kbuild to always build the programs 98# Tell kbuild to always build the programs
@@ -142,7 +143,20 @@ quiet_cmd_db2html = HTML $@
142 echo '<a HREF="$(patsubst %.html,%,$(notdir $@))/index.html"> \ 143 echo '<a HREF="$(patsubst %.html,%,$(notdir $@))/index.html"> \
143 $(patsubst %.html,%,$(notdir $@))</a><p>' > $@ 144 $(patsubst %.html,%,$(notdir $@))</a><p>' > $@
144 145
145%.html: %.xml 146###
147# Rules to create an aux XML and .db, and use them to re-process the DocBook XML
148# to fill internal hyperlinks
149 gen_aux_xml = :
150 quiet_gen_aux_xml = echo ' XMLREF $@'
151silent_gen_aux_xml = :
152%.aux.xml: %.xml
153 @$($(quiet)gen_aux_xml)
154 @rm -rf $@
155 @(cat $< | egrep "^<refentry id" | egrep -o "\".*\"" | cut -f 2 -d \" > $<.db)
156 @$(KERNELDOCXMLREF) -db $<.db $< > $@
157.PRECIOUS: %.aux.xml
158
159%.html: %.aux.xml
146 @(which xmlto > /dev/null 2>&1) || \ 160 @(which xmlto > /dev/null 2>&1) || \
147 (echo "*** You need to install xmlto ***"; \ 161 (echo "*** You need to install xmlto ***"; \
148 exit 1) 162 exit 1)
@@ -211,15 +225,18 @@ dochelp:
211### 225###
212# Temporary files left by various tools 226# Temporary files left by various tools
213clean-files := $(DOCBOOKS) \ 227clean-files := $(DOCBOOKS) \
214 $(patsubst %.xml, %.dvi, $(DOCBOOKS)) \ 228 $(patsubst %.xml, %.dvi, $(DOCBOOKS)) \
215 $(patsubst %.xml, %.aux, $(DOCBOOKS)) \ 229 $(patsubst %.xml, %.aux, $(DOCBOOKS)) \
216 $(patsubst %.xml, %.tex, $(DOCBOOKS)) \ 230 $(patsubst %.xml, %.tex, $(DOCBOOKS)) \
217 $(patsubst %.xml, %.log, $(DOCBOOKS)) \ 231 $(patsubst %.xml, %.log, $(DOCBOOKS)) \
218 $(patsubst %.xml, %.out, $(DOCBOOKS)) \ 232 $(patsubst %.xml, %.out, $(DOCBOOKS)) \
219 $(patsubst %.xml, %.ps, $(DOCBOOKS)) \ 233 $(patsubst %.xml, %.ps, $(DOCBOOKS)) \
220 $(patsubst %.xml, %.pdf, $(DOCBOOKS)) \ 234 $(patsubst %.xml, %.pdf, $(DOCBOOKS)) \
221 $(patsubst %.xml, %.html, $(DOCBOOKS)) \ 235 $(patsubst %.xml, %.html, $(DOCBOOKS)) \
222 $(patsubst %.xml, %.9, $(DOCBOOKS)) \ 236 $(patsubst %.xml, %.9, $(DOCBOOKS)) \
237 $(patsubst %.xml, %.aux.xml, $(DOCBOOKS)) \
238 $(patsubst %.xml, %.xml.db, $(DOCBOOKS)) \
239 $(patsubst %.xml, %.xml, $(DOCBOOKS)) \
223 $(index) 240 $(index)
224 241
225clean-dirs := $(patsubst %.xml,%,$(DOCBOOKS)) man 242clean-dirs := $(patsubst %.xml,%,$(DOCBOOKS)) man
diff --git a/scripts/kernel-doc-xml-ref b/scripts/kernel-doc-xml-ref
new file mode 100755
index 000000000000..104a5a5ba2c8
--- /dev/null
+++ b/scripts/kernel-doc-xml-ref
@@ -0,0 +1,198 @@
1#!/usr/bin/perl -w
2
3use strict;
4
5## Copyright (C) 2015 Intel Corporation ##
6# ##
7## This software falls under the GNU General Public License. ##
8## Please read the COPYING file for more information ##
9#
10#
11# This software reads a XML file and a list of valid interal
12# references to replace Docbook tags with links.
13#
14# The list of "valid internal references" must be one-per-line in the following format:
15# API-struct-foo
16# API-enum-bar
17# API-my-function
18#
19# The software walks over the XML file looking for xml tags representing possible references
20# to the Document. Each reference will be cross checked against the "Valid Internal Reference" list. If
21# the referece is found it replaces its content by a <link> tag.
22#
23# usage:
24# kernel-doc-xml-ref -db filename
25# xml filename > outputfile
26
27# read arguments
28if ($#ARGV != 2) {
29 usage();
30}
31
32#Holds the database filename
33my $databasefile;
34my @database;
35
36#holds the inputfile
37my $inputfile;
38my $errors = 0;
39
40my %highlights = (
41 "<function>(.*?)</function>",
42 "\"<function>\" . convert_function(\$1, \$line) . \"</function>\"",
43 "<structname>(.*?)</structname>",
44 "\"<structname>\" . convert_struct(\$1) . \"</structname>\"",
45 "<funcdef>(.*?)<function>(.*?)</function></funcdef>",
46 "\"<funcdef>\" . convert_param(\$1) . \"<function>\$2</function></funcdef>\"",
47 "<paramdef>(.*?)<parameter>(.*?)</parameter></paramdef>",
48 "\"<paramdef>\" . convert_param(\$1) . \"<parameter>\$2</parameter></paramdef>\"");
49
50while($ARGV[0] =~ m/^-(.*)/) {
51 my $cmd = shift @ARGV;
52 if ($cmd eq "-db") {
53 $databasefile = shift @ARGV
54 } else {
55 usage();
56 }
57}
58$inputfile = shift @ARGV;
59
60sub open_database {
61 open (my $handle, '<', $databasefile) or die "Cannot open $databasefile";
62 chomp(my @lines = <$handle>);
63 close $handle;
64
65 @database = @lines;
66}
67
68sub process_file {
69 open_database();
70
71 my $dohighlight;
72 foreach my $pattern (keys %highlights) {
73 $dohighlight .= "\$line =~ s:$pattern:$highlights{$pattern}:eg;\n";
74 }
75
76 open(FILE, $inputfile) or die("Could not open $inputfile") or die ("Cannot open $inputfile");
77 foreach my $line (<FILE>) {
78 eval $dohighlight;
79 print $line;
80 }
81}
82
83sub trim($_)
84{
85 my $str = $_[0];
86 $str =~ s/^\s+|\s+$//g;
87 return $str
88}
89
90sub has_key_defined($_)
91{
92 if ( grep( /^$_[0]$/, @database)) {
93 return 1;
94 }
95 return 0;
96}
97
98# Gets a <function> content and add it a hyperlink if possible.
99sub convert_function($_)
100{
101 my $arg = $_[0];
102 my $key = $_[0];
103
104 my $line = $_[1];
105
106 $key = trim($key);
107
108 $key =~ s/[^A-Za-z0-9]/-/g;
109 $key = "API-" . $key;
110
111 # We shouldn't add links to <funcdef> prototype
112 if (!has_key_defined($key) || $line =~ m/\s+<funcdef/i) {
113 return $arg;
114 }
115
116 my $head = $arg;
117 my $tail = "";
118 if ($arg =~ /(.*?)( ?)$/) {
119 $head = $1;
120 $tail = $2;
121 }
122 return "<link linkend=\"$key\">$head</link>$tail";
123}
124
125# Converting a struct text to link
126sub convert_struct($_)
127{
128 my $arg = $_[0];
129 my $key = $_[0];
130 $key =~ s/(struct )?(\w)/$2/g;
131 $key =~ s/[^A-Za-z0-9]/-/g;
132 $key = "API-struct-" . $key;
133
134 if (!has_key_defined($key)) {
135 return $arg;
136 }
137
138 my ($head, $tail) = split_pointer($arg);
139 return "<link linkend=\"$key\">$head</link>$tail";
140}
141
142# Identify "object *" elements
143sub split_pointer($_)
144{
145 my $arg = $_[0];
146 if ($arg =~ /(.*?)( ?\* ?)/) {
147 return ($1, $2);
148 }
149 return ($arg, "");
150}
151
152sub convert_param($_)
153{
154 my $type = $_[0];
155 my $keyname = convert_key_name($type);
156
157 if (!has_key_defined($keyname)) {
158 return $type;
159 }
160
161 my ($head, $tail) = split_pointer($type);
162 return "<link linkend=\"$keyname\">$head</link>$tail";
163
164}
165
166# DocBook links are in the API-<TYPE>-<STRUCT-NAME> format
167# This method gets an element and returns a valid DocBook reference for it.
168sub convert_key_name($_)
169{
170 #Pattern $2 is optional and might be uninitialized
171 no warnings 'uninitialized';
172
173 my $str = $_[0];
174 $str =~ s/(const|static)? ?(struct)? ?([a-zA-Z0-9_]+) ?(\*|&)?/$2 $3/g ;
175
176 # trim
177 $str =~ s/^\s+|\s+$//g;
178
179 # spaces and _ to -
180 $str =~ s/[^A-Za-z0-9]/-/g;
181
182 return "API-" . $str;
183}
184
185sub usage {
186 print "Usage: $0 -db database filename\n";
187 print " xml source file(s) > outputfile\n";
188 exit 1;
189}
190
191# starting point
192process_file();
193
194if ($errors) {
195 print STDERR "$errors errors\n";
196}
197
198exit($errors);