]> Sergey Matveev's repositories - dotfiles.git/blob - bin/bin/docstringer.pl
Allow indentation of @DOCSTRING@
[dotfiles.git] / bin / bin / docstringer.pl
1 #!/usr/bin/env perl
2 # Simple script for substitute placeholders in Texinfo files with
3 # docstring values found in source code.
4 # Copyright (C) 2020 Sergey Matveev (stargrave@stargrave.org)
5 #
6 # If you C source code contains:
7 #
8 #     // TEXINFO: SomeKey
9 #     // ...
10 #     // last line of docstring
11 #     some C code
12 #
13 # Then under "SomeKey" you will have the whole docstring (starting from
14 # the line after "TEXINFO", till "last line"). You can include its
15 # contents (excluding comment characters) in your .texi files placing
16 #
17 #     @DOCSTRING SomeKey@
18
19 use strict;
20 use warnings;
21
22 my $verbose = 0;
23 if ($ARGV[0] eq "-v") {
24     $verbose = 1;
25     shift @ARGV;
26 };
27 my @srcDirs = split / /, $ARGV[0];
28 my $docDir = $ARGV[1];
29 my $docBuildDir = $ARGV[2];
30
31 my %docstrings;
32
33 foreach my $srcDir (@srcDirs) {
34     print "src: $srcDir\n" if $verbose;
35     opendir(my $dir, $srcDir) or die "can not open $srcDir";
36     foreach my $fn (readdir $dir) {
37         next unless $fn =~ /\.[ch]$/;
38         open(my $src, "<:encoding(UTF-8)", "$srcDir/$fn") or
39             die "can not open $srcDir/$fn";
40         my $curEntry;
41         while(<$src>) {
42             chomp;
43             if (not /^\/\//) {
44                 if (defined $curEntry) {
45                     undef $curEntry;
46                 };
47                 next;
48             };
49             s/^\/\/ ?//;
50             if (/^TEXINFO: (.*)$/) {
51                 $curEntry = $1;
52                 $docstrings{$curEntry} = "";
53                 print "\t$fn: $curEntry\n" if $verbose;
54                 next;
55             };
56             ( $docstrings{$curEntry} .= "$_\n" ) if defined $curEntry;
57         };
58         close $src;
59     };
60     closedir $dir;
61 };
62
63 print "doc: $docDir\n" if $verbose;
64 opendir(my $dir, $docDir) or die "can not open $docDir";
65 foreach my $fn (readdir $dir) {
66     next unless $fn =~ /\.texi$/;
67     open(my $src, "<:encoding(UTF-8)", "$docDir/$fn") or
68         die "can not open $docDir/$fn";
69     open(my $dst, ">:encoding(UTF-8)", "$docBuildDir/$fn") or
70         die "can not open $docBuildDir/$fn";
71     while(<$src>) {
72         ( print($dst $_) and next ) unless /^\s*\@DOCSTRING (.*)\@$/;
73         print "\t$fn: $1\n" if $verbose;
74         die "unable to find docstring: $1" unless defined $docstrings{$1};
75         print $dst $docstrings{$1};
76     };
77     close $src;
78     close $dst;
79 };
80 closedir $dir;