History

docproc: add support for reStructuredText format via --rst option

Expect reStructuredText input and have kernel-doc produce reStructuredText output with the new --rst option. Also add --docbook option for completeness. If no option is given, default to reStructuredText if the input file has ".rst" extension, DocBook otherwise. Directives for reStructuredText use .. ! instead of just !, to make them reStructuredText comments.

Jani Nikula committed 10 years ago

Commit 064669b1 parent 1dcdad01 file · +80 −10Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/docproc.c+80−10
@@ -42,6 +42,7 @@ · 41 unmodified lines
4242 #include <unistd.h>
4343 #include <limits.h>
4444 #include <errno.h>
45+#include <getopt.h>
4546 #include <sys/types.h>
4647 #include <sys/wait.h>
4748
@@ -68,12 +69,23 @@ FILELINE * docsection; · 20 unmodified lines
6869 #define KERNELDOCPATH "scripts/"
6970 #define KERNELDOC "kernel-doc"
7071 #define DOCBOOK "-docbook"
72+#define RST "-rst"
7173 #define LIST "-list"
7274 #define FUNCTION "-function"
7375 #define NOFUNCTION "-nofunction"
7476 #define NODOCSECTIONS "-no-doc-sections"
7577 #define SHOWNOTFOUND "-show-not-found"
7678
79+enum file_format {
80+ FORMAT_AUTO,
81+ FORMAT_DOCBOOK,
82+ FORMAT_RST,
83+};
84+
85+static enum file_format file_format = FORMAT_AUTO;
86+
87+#define KERNELDOC_FORMAT (file_format == FORMAT_RST ? RST : DOCBOOK)
88+
7789 static char *srctree, *kernsrctree;
7890
7991 static char **all_list = NULL;
@@ -95,7 +107,7 @@ static void consume_symbol(const char *sym) · 15 unmodified lines
95107
96108 static void usage (void)
97109 {
98− fprintf(stderr, "Usage: docproc {doc|depend} file\n");
110+ fprintf(stderr, "Usage: docproc [{--docbook|--rst}] {doc|depend} file\n");
99111 fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
100112 fprintf(stderr, "doc: frontend when generating kernel documentation\n");
101113 fprintf(stderr, "depend: generate list of files referenced within file\n");
@@ -242,7 +254,7 @@ static void find_export_symbols(char * filename) · 140 unmodified lines
242254 /*
243255 * Document all external or internal functions in a file.
244256 * Call kernel-doc with following parameters:
245− * kernel-doc -docbook -nofunction function_name1 filename
257+ * kernel-doc [-docbook|-rst] -nofunction function_name1 filename
246258 * Function names are obtained from all the src files
247259 * by find_export_symbols.
248260 * intfunc uses -nofunction
@@ -263,7 +275,7 @@ static void docfunctions(char * filename, char * type) · 14 unmodified lines
263275 exit(1);
264276 }
265277 vec[idx++] = KERNELDOC;
266− vec[idx++] = DOCBOOK;
278+ vec[idx++] = KERNELDOC_FORMAT;
267279 vec[idx++] = NODOCSECTIONS;
268280 for (i=0; i < symfilecnt; i++) {
269281 struct symfile * sym = &symfilelist[i];
@@ -275,7 +287,10 @@ static void docfunctions(char * filename, char * type) · 5 unmodified lines
275287 }
276288 vec[idx++] = filename;
277289 vec[idx] = NULL;
278− printf("<!-- %s -->\n", filename);
290+ if (file_format == FORMAT_RST)
291+ printf(".. %s\n", filename);
292+ else
293+ printf("<!-- %s -->\n", filename);
279294 exec_kernel_doc(vec);
280295 fflush(stdout);
281296 free(vec);
@@ -294,7 +309,7 @@ static void singfunc(char * filename, char * line) · 12 unmodified lines
294309 int i, idx = 0;
295310 int startofsym = 1;
296311 vec[idx++] = KERNELDOC;
297− vec[idx++] = DOCBOOK;
312+ vec[idx++] = KERNELDOC_FORMAT;
298313 vec[idx++] = SHOWNOTFOUND;
299314
300315 /* Split line up in individual parameters preceded by FUNCTION */
@@ -343,7 +358,7 @@ static void docsect(char *filename, char *line) · 42 unmodified lines
343358 free(s);
344359
345360 vec[0] = KERNELDOC;
346− vec[1] = DOCBOOK;
361+ vec[1] = KERNELDOC_FORMAT;
347362 vec[2] = SHOWNOTFOUND;
348363 vec[3] = FUNCTION;
349364 vec[4] = line;
@@ -448,8 +463,10 @@ static char *chomp(char *s) · 98 unmodified lines
448463 /* Return pointer to directive content, or NULL if not a directive. */
449464 static char *is_directive(char *line)
450465 {
451− if (line[0] == '!')
466+ if (file_format == FORMAT_DOCBOOK && line[0] == '!')
452467 return line + 1;
468+ else if (file_format == FORMAT_RST && !strncmp(line, ".. !", 4))
469+ return line + 4;
453470
454471 return NULL;
455472 }
@@ -516,6 +533,22 @@ static void parse_file(FILE *infile) · 60 unmodified lines
516533 fflush(stdout);
517534 }
518535
536+/*
537+ * Is this a RestructuredText template? Answer the question by seeing if its
538+ * name ends in ".rst".
539+ */
540+static int is_rst(const char *file)
541+{
542+ char *dot = strrchr(file, '.');
543+
544+ return dot && !strcmp(dot + 1, "rst");
545+}
546+
547+enum opts {
548+ OPT_DOCBOOK,
549+ OPT_RST,
550+ OPT_HELP,
551+};
519552
520553 int main(int argc, char *argv[])
521554 {
@@ -529,13 +562,50 @@ int main(int argc, char *argv[]) · 7 unmodified lines
529562 kernsrctree = getenv("KBUILD_SRC");
530563 if (!kernsrctree || !*kernsrctree)
531564 kernsrctree = srctree;
532− if (argc != 3) {
565+
566+ for (;;) {
567+ int c;
568+ struct option opts[] = {
569+ { "docbook", no_argument, NULL, OPT_DOCBOOK },
570+ { "rst", no_argument, NULL, OPT_RST },
571+ { "help", no_argument, NULL, OPT_HELP },
572+ {}
573+ };
574+
575+ c = getopt_long_only(argc, argv, "", opts, NULL);
576+ if (c == -1)
577+ break;
578+
579+ switch (c) {
580+ case OPT_DOCBOOK:
581+ file_format = FORMAT_DOCBOOK;
582+ break;
583+ case OPT_RST:
584+ file_format = FORMAT_RST;
585+ break;
586+ case OPT_HELP:
587+ usage();
588+ return 0;
589+ default:
590+ case '?':
591+ usage();
592+ return 1;
593+ }
594+ }
595+
596+ argc -= optind;
597+ argv += optind;
598+
599+ if (argc != 2) {
533600 usage();
534601 exit(1);
535602 }
536603
537− subcommand = argv[1];
538− filename = argv[2];
604+ subcommand = argv[0];
605+ filename = argv[1];
606+
607+ if (file_format == FORMAT_AUTO)
608+ file_format = is_rst(filename) ? FORMAT_RST : FORMAT_DOCBOOK;
539609
540610 /* Open file, exit on error */
541611 infile = fopen(filename, "r");

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t