3 #TIT pipadoc - Documentation extractor
4 #TIT =================================
5 #TIT Christian Thaeter <ct@pipapo.org>
7 #LIC Copyright (C) Pipapo Project
8 #LIC 2009, Christian Thaeter <ct@pipapo.org>
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.
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.
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>.
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.
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.
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.
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]).
42 #BAS HEAD- Basic concepts; BAS; concepts
44 #BAS NOTE: The following description uses the xref:ENV[default] settings for all examples.
46 #BAS Pipadoc is controlled by special line comments.
48 #DIR PARA Direct comments; DIR; low level control
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.
56 #DIR //_ This is a direct comment,
57 #DIR //_ it will appear just verbatim in the generated output
60 #KEY PARA Keys; KEY; sectioning content
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.
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
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.
76 #SOR //example.omega This is sorted after the next line
77 #SOR //example.alpha comes before the omega line above
80 #VAR PARA Variable Substitution; VAR; variable substitution
81 #VAR The strings `%%FILE%%` and `%%LINE%%` are substituted with the filename and line currently proccessed.
82 #VAR They can be used in the secondary sort key, as well as in documentation text directly. When one wants
83 #VAR to use this words verbatim then then percent signs must be doubled as in `%%%FILE%%%` or `%%%LINE%%%`.
86 #SUB PARA Substitutions; SUB; paste sections
87 #SUB A line comment immediately followed by a special `substitution` character (the equal `=` sign by default)
88 #SUB followed by a xref:KEY[key] will be replaced by the text defined under that key. The rest of the line
89 #SUB is ignored and can be used as comment.
91 #SUB //=example this will insert anything defined under `//example` here
95 #USE HEAD- Documenting Files; USE; usage
97 #USE Usually one wants to write documentation in more or less smaller blocks which later shall be
98 #USE brought into proper order. The xref:SUB[substitutions] feature is the key for this. One writes
99 #USE his documentation blocks with comments which later get replaced by the right sorting key and
100 #USE finally brought into (alphabetical) stable-sort order. You might take a look at the pipadoc
101 #USE source itself to see it in action.
104 #PLU HEAD- Plugins; PLU; extending pipadoc
105 #PLU Pipadoc can be extended with awk. This plugins are `awk` snippets which are included into
106 #PLU the processing stream. Take a look at the supplied plugins to figure out how it works.
108 #PLU Supplied Plugins
109 #PLU ~~~~~~~~~~~~~~~~
112 #PLU Defines HEAD and PARA macros for headers and paragaphs with automatic index generation
116 #PLU Lets one include the code _before_ an pipadoc comment with the magic word `VERBATIM`.
117 #PLU Used to include code in the documentation.
120 #ENV HEAD~ Environment Variables; ENV; configuration
123 #ENV Defines the line-comment character used in the source file.
124 #ENV Defaults to `//` (C++/C99) if not set. Set this to `#` for shell
126 test "$COM" || COM
='//'
128 #ENV The Documentation character which must follow a line comment to be recognized
129 #ENV by pipadoc as documentation. Either one for local definitions or two for global
130 #ENV definitions are used. Defaults to `_` and needs rarely to be changed.
131 test "$DOC" || DOC
='_'
133 #ENV Substitution character. Defaults to `=` and rarely needs to be changed.
134 #ENV See xref:SUB[substitutions] for details.
136 test "$SUB" || SUB
='='
139 #ENV Section eXTention. Defaults to `.txt`.
140 #ENV See xref:SXT[plaintext files] for details.
142 test "$SXT" || SXT
='.txt'
144 #ENV Extension for xref:PLU[awk plugins] to implement generic text manipulations and macros.
145 #ENV Defaults to '.pawk'
147 test "$PLG" || PLG
='.pawk'
149 #SXT HEAD- Documentation Only Files; SXT; plain documentation
151 #SXT One can write documentation without the need of pipadoc special comments in files
152 #SXT which have a configured extension (see the xref:ENVSXT[SXT] environment variable).
153 #SXT Each line in such a file which does not have a pipadoc special comment is then implicitly
154 #SXT prepended with the line comment sequence and the basename of that file, for example
155 #SXT lines in a file 'foo.txt' will be treated as if they where written with `//foo ` in front of
156 #SXT them. Later xref:SUB[substitutions] can be used to organize the document.
157 #SXT When such a file has an ordinary pipadoc special comment line then this takes precedence over
158 #SXT the implicit commenting.
160 #INV HEAD- Invocation; INV; running pipadoc
162 #INV Pipadoc is called with a list of files from which the documentation shall be extracted.
163 #INV The extracted documentation is piped to stdout. No other command line options are available.
165 #INV There are few xref:ENV[environment variables] to do basic configuration.
169 sources
=($
(for element
in "$@"; do [[ $element != *$PLG ]] && printf "%s " $element; done))
170 plugins
=($
(for element
in "$@"; do [[ $element = *$PLG ]] && printf "%s " $element; done))
174 gawk
-f /dev
/fd
/3 ${COM:+-v com="$COM"} ${DOC:+-v doc="$DOC"} ${SUB:+-v subs="$SUB"} ${SXT:+-v sxt="$SXT"} "${sources[@]}" 3<<EOF
176 function append(idx, sort, text)
179 maybe[idx] = maybe[idx] "\n.
"sort" " text
181 maybe[idx] = ".
"sort" "text
184 $([[ ${#plugins[*]} -gt 0 ]] && cat "${plugins[@]}")
186 # Plaintext file handling
187 FILENAME ~ sxt "\$
" && \$0 !~ "^
" com {
188 match(FILENAME, "/?
([^
/]*)" sxt "\$
", p)
189 \$0 = com p[1] " " \$0
193 match(\$0, com subs "([[:alpha
:]][[:alnum
:]_
]*)", s) {
200 match(\$0, com doc "([[:space
:]](.
*))?\$
", s) {
206 # record all other comments which may be candidate comments
207 match(\$0, "(.
*)" com "([[:alpha
:]][[:alnum
:]_
]*)(([.
]([^
[:space
:]]*)))?
([[:space
:]](.
*))?
", m) && m[1] !~ com {
208 gsub("%%FILE
%%", " FILE
%FILE
", m[5])
209 gsub("%%LINE
%%", " LINE
%LINE
", m[5])
210 gsub("%LINE
%", FNR, m[5])
211 gsub("%FILE
%", FILENAME, m[5])
212 gsub(" FILE
%FILE
", "%FILE
%", m[5])
213 gsub(" LINE
%LINE
", "%LINE
%", m[5])
215 gsub("%%FILE
%%", " FILE
%FILE
", m[7])
216 gsub("%%LINE
%%", " LINE
%LINE
", m[7])
217 gsub("%LINE
%", FNR, m[7])
218 gsub("%FILE
%", FILENAME, m[7])
219 gsub(" FILE
%FILE
", "%FILE
%", m[7])
220 gsub(" LINE
%LINE
", "%LINE
%", m[7])
222 append(m[2], m[5] , m[7])
234 nelements = split(maybe[subst[i]], s, "\n")
238 for (j=1; j <= nelements; ++j)
240 match(s[j], "[.
]([^
[:space
:]]*) (.
*)", entries)
242 if (entries[1] in tosort)
243 tosort[entries[1]] = tosort[entries[1]] "\n" entries[2]
245 tosort[entries[1]] = entries[2]
248 elements = asorti(tosort, sorted)
249 for (k = 1; k <= elements; ++k)
251 print tosort[sorted[k]]
259 #=TIT Titles and stuff
262 #=DIR Direct comments
265 #=VAR Variable substitutions
269 #=SXT Plain Documentation
273 #=ENV Environment variables
285 # Examples and appendixes:
286 #USE .A small C99 Program
289 #USE //intro This is the well known ``Hello World'' example
290 #USE //glos.helloworld A common program to show examples for programming language and tools
292 #USE //depends Only the Standard C library is needed
293 #USE #include <stdio.h>
295 #USE int main(int argc, char* argv[])
297 #USE //hello print the first commandline argument
298 #USE //glos.argument the text you pass to the programm when calling it from the shell
299 #USE //hello if no argument is given, then exit silently
301 #USE printf("Hello
%s
\n", argv[1]);
305 #USE // Now the document structure with substitutions:
306 #USE //_ Yet another 'Hello' program
308 #USE //=intro introduction first right after the title
310 #USE //=hello The main documentation
313 #USE //_ Dependencies:
316 #USE //=glos glossary will be sorted
319 #USE Runnning this through pipadoc gives following output:
321 #USE Yet another 'Hello' program
323 #USE This is the well known ``Hello World'' example
325 #USE print the first commandline argument
326 #USE if no argument is given, then exit silently
330 #USE Only the Standard C library is needed
332 #USE the text you pass to the programm when calling it from the shell
333 #USE A common program to show examples for programming language and tools
339 #APP .Using C block comments with pipadoc: +example.c+
342 #APP //_ this is a documentation line
345 #APP use `pipadoc example.c` to process the documentation.
347 #APP Creating this File
348 #APP ~~~~~~~~~~~~~~~~~~
349 #APP Pipadoc itself is documented with asciidoc, one can extract the asciidoc source with
352 #APP COM='#' ./pipadoc pipadoc asciidoc.pawk >pipadoc.txt
354 #APP And then further process the generated asciidoc.txt with the asciidoc toolchain.