pipadoc update, new upstream version with macro facility
[nobug.git] / pipadoc
blobff4da957e6ea4b3cd9de313f319203e4d5d19640
1 #!/bin/sh
3 #TIT pipadoc - Documentation extractor
4 #TIT =================================
5 #TIT Christian Thaeter <ct@pipapo.org>
6 #TIT
7 #LIC Copyright (C) Pipapo Project
8 #LIC 2009, Christian Thaeter <ct@pipapo.org>
9 #LIC
10 #LIC This program is free software; you can redistribute it and/or modify
11 #LIC it under the terms of the GNU General Public License as published by
12 #LIC the Free Software Foundation; either version 2, or (at your option)
13 #LIC any later version.
14 #LIC
15 #LIC This program is distributed in the hope that it will be useful,
16 #LIC but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #LIC MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #LIC GNU General Public License for more details.
19 #LIC
20 #LIC You should have received a copy of the GNU General Public License
21 #LIC along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
22 #LIC
24 #INT Embedding documentation in program source files often yields the problem that the
25 #INT structure of a program is most often not the optimal structure for the associated documentation.
26 #INT Still there are good reasons to maintain documentation together with the source right at the code
27 #INT which defines the documented functionality. Pipadoc addresses this problem by extracting
28 #INT special comments out of a source file and let one define rules how to bring the
29 #INT documentation into proper order.
30 #INT
31 #INT Pipadoc only extracts and reorders the text from it special comments, it never ever looks at the
32 #INT sourcecode or the text it extracts.
33 #INT
34 #INT This is somewhat similar to ``Literate Programming'' but it puts the emphasis back to the code.
35 #INT There is no need to extract the source from a literate source and in contrast to ``Literate Programming''
36 #INT the order of source and text is defined by the programmer and programming language constraints.
37 #INT
38 #INT Pipadoc is programming language and documentation system agnostic, all it requires is that
39 #INT the programming language has some line comments or block comments where one places doc statements
40 #INT on each block line (see xref:c-example[Example for C]).
41 #INT
42 #BAS HEAD- Basic concepts; BAS; concepts
43 #BAS
44 #BAS NOTE: The following description uses the xref:ENV[default] settings for all examples.
45 #BAS
46 #BAS Pipadoc is controlled by special line comments.
47 #BAS
48 #DIR PARA Direct comments; DIR; low level control
49 #DIR
50 #DIR Line comments immediately followed by a special documentation character (the underscore `_` by default)
51 #DIR are treated as direct comments. They will appear in order of appearance in the generated output.
52 #DIR These can be used to do some boilerplate stuff. Usually one wants to define a controlling document and
53 #DIR use this direct comments only there, since using them in different files might yield unexpected results
54 #DIR since the order then depends on the load order of the files.
55 #DIR ----
56 #DIR //_ This is a direct comment,
57 #DIR //_ it will appear just verbatim in the generated output
58 #DIR ----
59 #DIR
60 #KEY PARA Keys; KEY; sectioning content
61 #KEY
62 #KEY A line comment immediately followed by a alphanumeric keyword (including the `_` underscore character)
63 #KEY is treated as key, all such keyed comments can later be placed in intended order with
64 #KEY a xref:SUB[substitution] comment.
65 #KEY ----
66 #KEY //example This text will later be inserted where one uses the `//=example` substitution.
67 #KEY //example All example lines are appended there in order even if they are defined at different
68 #KEY //example places or in different files
69 #KEY ----
70 #KEY
71 #SOR PARA Sorted Keys; SOR; sorted sections
72 #SOR A key can be appended with a dot `.` and a non-space string. This string will then be used to sort
73 #SOR these lines alphabetically. This can be used to create sorted indices and glossars, as well as reordering
74 #SOR paragraphs in stored under one primary key.
75 #SOR ----
76 #SOR //example.omega This is sorted after the next line
77 #SOR //example.alpha comes before the omega line above
78 #SOR ----
79 #SOR
80 #SUB PARA Substitutions; SUB; paste sections
81 #SUB A line comment immediately followed by a special `substitution` character (the equal `=` sign by default)
82 #SUB followed by a xref:KEY[key] will be replaced by the text defined under that key. The rest of the line
83 #SUB is ignored and can be used as comment.
84 #SUB ----
85 #SUB //=example this will insert anything defined under `//example` here
86 #SUB ----
87 #SUB
89 #USE HEAD- Documenting Files; USE; usage
90 #USE
91 #USE Usually one wants to write documentation in more or less smaller blocks which later shall be
92 #USE brought into proper order. The xref:SUB[substitutions] feature is the key for this. One writes
93 #USE his documentation blocks with comments which later get replaced by the right sorting key and
94 #USE finally brought into (alphabetical) stable-sort order. You might take a look at the pipadoc
95 #USE source itself to see it in action.
96 #USE
98 #PLU HEAD- Plugins; PLU; extending pipadoc
99 #PLU Pipadoc can be extended with awk. Please refer to the provided 'asciidoc.pawk' for an example.
100 #PLU
102 #ENV HEAD~ Environment Variables; ENV; configuration
103 #ENV
104 #ENV `COM`::
105 #ENV Defines the line-comment character used in the source file.
106 #ENV Defaults to `//` (C++/C99) if not set. Set this to `#` for shell
107 #ENV like languages.
108 test "$COM" || COM='//'
109 #ENV `DOC`::
110 #ENV The Documentation character which must follow a line comment to be recognized
111 #ENV by pipadoc as documentation. Either one for local definitions or two for global
112 #ENV definitions are used. Defaults to `_` and needs rarely to be changed.
113 test "$DOC" || DOC='_'
114 #ENV `SUB`::
115 #ENV Substitution character. Defaults to `=` and rarely needs to be changed.
116 #ENV See xref:SUB[substitutions] for details.
117 #ENV
118 test "$SUB" || SUB='='
119 #ENV [[ENVSXT]]
120 #ENV `SXT`::
121 #ENV Section eXTention. Defaults to `.txt`.
122 #ENV See xref:SXT[plaintext files] for details.
123 #ENV
124 test "$SXT" || SXT='.txt'
125 #ENV `PLG`::
126 #ENV Extension for xref:PLU[awk plugins] to implement generic text manipulations and macros.
127 #ENV Defaults to '.pawk'
128 #ENV
129 test "$PLG" || PLG='.pawk'
131 #SXT HEAD- Documentation Only Files; SXT; plain documentation
132 #SXT
133 #SXT One can write documentation without the need of pipadoc special comments in files
134 #SXT which have a configured extension (see the xref:ENVSXT[SXT] environment variable).
135 #SXT Each line in such a file which does not have a pipadoc special comment is then implicitly
136 #SXT prepended with the line comment sequence and the basename of that file, for example
137 #SXT lines in a file 'foo.txt' will be treated as if they where written with `//foo ` in front of
138 #SXT them. Later xref:SUB[substitutions] can be used to organize the document.
139 #SXT When such a file has an ordinary pipadoc special comment line then this takes precedence over
140 #SXT the implicit commenting.
141 #SXT
142 #INV HEAD- Invocation; INV; running pipadoc
143 #INV
144 #INV Pipadoc is called with a list of files from which the documentation shall be extracted.
145 #INV The extracted documentation is piped to stdout. No other command line options are available.
146 #INV
147 #INV There are few xref:ENV[environment variables] to do basic configuration.
148 #INV
151 sources=($(for element in "$@"; do [[ $element != *$PLG ]] && printf "%s " $element; done))
152 plugins=($(for element in "$@"; do [[ $element = *$PLG ]] && printf "%s " $element; done))
155 # here we go
156 gawk -f /dev/fd/3 ${COM:+-v com="$COM"} ${DOC:+-v doc="$DOC"} ${SUB:+-v subs="$SUB"} ${SXT:+-v sxt="$SXT"} "${sources[@]}" 3<<EOF
158 function append(idx, sort, text)
160 if (idx in maybe)
161 maybe[idx] = maybe[idx] "\n.. " text
162 else
163 maybe[idx] = "."sort" "text
166 $([[ ${#plugins[*]} -gt 0 ]] && cat "${plugins[@]}")
168 # Plaintext file handling
169 FILENAME ~ sxt "\$" && \$0 !~ "^" com {
170 match(FILENAME, "/?([^/]*)" sxt "\$", p)
171 \$0 = com p[1] " " \$0
174 # Substitution
175 match(\$0, com subs "([[:alpha:]][[:alnum:]_]*)", s) {
177 subst[n] = s[1]
178 next
181 # doc comment
182 match(\$0, com doc "([[:space:]](.*))?\$", s) {
184 output[n] = s[2]
185 next
188 # record all other comments which may be candidate comments
189 match(\$0, "(.*)" com "([[:alpha:]][[:alnum:]_]*)(([.]([^[:space:]]*)))?([[:space:]](.*))?", m) && m[1] !~ com {
190 #print "append("m[2]"," m[5] "," m[7]")"
192 append(m[2], m[5] , m[7])
193 next
196 # finally output
197 END {
198 for (i=1; i<=n; ++i)
200 if (i in output)
201 print output[i]
202 else
204 nelements = split(maybe[subst[i]], s, "\n[.]")
205 split("", tosort)
207 for (j=1; j <=nelements; ++j)
209 match(s[j], ".([^[:space:]]*) (.*)", entries)
211 if ("." entries[1] in tosort)
212 tosort["." entries[1]] = tosort["." entries[1]] "\n" entries[2]
213 else
214 tosort["." entries[1]] = entries[2]
217 elements = asorti(tosort, sorted)
218 for (k = 1; k <= elements; ++k)
220 print tosort[sorted[k]]
227 #Document structure:
228 #=TIT Titles and stuff
229 #=INT Introduction
230 #=BAS Basics
231 #=DIR Direct comments
232 #=KEY Key comments
233 #=SOR sorted keys
234 #=SUB Substitutions
235 #=INV Invocation
236 #=USE Real usage
237 #=SXT Plain Documentation
238 #=PLU Plugins
239 #_ Appendix
240 #_ --------
241 #=ENV Environment variables
242 #=APP Appendix
243 #_ License
244 #_ ~~~~~~~
245 #_ [[LIC]]
246 #_ ....
247 #=LIC
248 #_ ....
249 #_ Index
250 #_ -----
251 #=index
253 # Examples and appendixes:
254 #USE .A small C99 Program
255 #USE [source,c]
256 #USE ----
257 #USE //intro This is the well known ``Hello World'' example
258 #USE //glos.helloworld A common program to show examples for programming language and tools
259 #USE
260 #USE //depends Only the Standard C library is needed
261 #USE #include <stdio.h>
262 #USE
263 #USE int main(int argc, char* argv[])
264 #USE {
265 #USE //hello print the first commandline argument
266 #USE //glos.argument the text you pass to the programm when calling it from the shell
267 #USE //hello if no argument is given, then exit silently
268 #USE if (argc > 1)
269 #USE printf("Hello %s\n", argv[1]);
270 #USE return 0;
271 #USE }
272 #USE
273 #USE // Now the document structure with substitutions:
274 #USE //_ Yet another 'Hello' program
275 #USE //_
276 #USE //=intro introduction first right after the title
277 #USE //intro
278 #USE //=hello The main documentation
279 #USE //_
280 #USE //_ Appendix
281 #USE //_ Dependencies:
282 #USE //=depends
283 #USE //_ Glossary
284 #USE //=glos glossary will be sorted
285 #USE ----
286 #USE
287 #USE Runnning this through pipadoc gives following output:
288 #USE ----
289 #USE Yet another 'Hello' program
290 #USE
291 #USE This is the well known ``Hello World'' example
292 #USE
293 #USE print the first commandline argument
294 #USE if no argument is given, then exit silently
295 #USE
296 #USE Appendix
297 #USE Dependencies:
298 #USE Only the Standard C library is needed
299 #USE Glossary
300 #USE the text you pass to the programm when calling it from the shell
301 #USE A common program to show examples for programming language and tools
302 #USE ----
303 #USE
305 #APP [[c-example]]
306 #APP
307 #APP .Using C block comments with pipadoc: +example.c+
308 #APP ----
309 #APP /*
310 #APP //_ this is a documentation line
311 #APP */
312 #APP ----
313 #APP use `pipadoc example.c` to process the documentation.
314 #APP
315 #APP Creating this File
316 #APP ~~~~~~~~~~~~~~~~~~
317 #APP Pipadoc itself is documented with asciidoc, one can extract the asciidoc source with
318 #APP by:
319 #APP ----
320 #APP COM='#' ./pipadoc pipadoc asciidoc.pawk >pipadoc.txt
321 #APP ----
322 #APP And then further process the generated asciidoc.txt with the asciidoc toolchain.
323 #APP