ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/doc/pod2texi
Revision: 1.9
Committed: Tue Feb 15 13:31:22 2011 UTC (13 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-3_0, rel-2_25, HEAD
Changes since 1.8: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.4 #!/usr/bin/perl
2 pcg 1.1
3 pcg 1.7 # TODO: texinfo fails on @cindex in title elements etc. *sigh*
4 pcg 1.6
5     use Pod::POM;
6 pcg 1.3
7 pcg 1.1 sub escape_texi($) {
8     local $_ = shift;
9     s/([\@\{\}])/\@$1/g;
10     s/\n+/ /g;
11     $_;
12     }
13    
14 pcg 1.3 sub example {
15     my $text = $_[0];
16     $text =~ s/\n+$//;
17 pcg 1.4 $text =~ s/([\@\{\}])/\@$1/g;
18 pcg 1.5
19     "\n\n\@example\n"
20     . $text
21     . "\n\@end example\n\n";
22     }
23    
24     my @nodes; # nodelist
25     my @ctx; # curstack
26    
27     sub out {
28     $ctx[-1]{out} .= join "", @_;
29 pcg 1.3 }
30    
31 pcg 1.6 sub TEX::view_seq_code { "\@t{$_[1]}" }
32 root 1.9 sub TEX::view_seq_file { "\@file{$_[1]}" }
33 pcg 1.6 sub TEX::view_seq_bold { "\@strong{$_[1]}" }
34     sub TEX::view_seq_italic { "\@emph{$_[1]}" }
35    
36     sub TEX::view_seq_space { escape_texi $_[1] }
37     sub TEX::view_seq_text { escape_texi $_[1] }
38    
39     sub TEX::view_seq_link { $_[1] }
40     sub TEX::view_seq_index {
41     "\n\@cindex $_[1]\n$_[1]"
42     }
43    
44     *TXT::view_seq_code =
45     *TXT::view_seq_bold =
46     *TXT::view_seq_italic =
47     *TXT::view_seq_space =
48     *TXT::view_seq_text =
49     *TXT::view_seq_link =
50     *TXT::view_seq_index = sub { $_[1] };
51    
52     my %ignore = (
53     "SEE ALSO" => 1,
54     "AUTHOR" => 1,
55     );
56    
57 pcg 1.5 sub parse_pod {
58     my ($data) = @_;
59     local $out;
60    
61 pcg 1.6 local $Pod::POM::DEFAULT_VIEW = TEX::;
62    
63     my $parser = new Pod::POM;
64     my $pod = $parser->parse_text ($data)
65     or die;
66 pcg 1.5
67     my $walker; $walker = sub {
68     my $n = $_[0];
69 pcg 1.6 my $t = $n->type;
70    
71     if ($t eq "text") {
72     out $n->text . "\n\@refill\n";
73    
74     } elsif ($t eq "pod") {
75     $walker->($_) for $n->content;
76    
77     } elsif ($t eq "verbatim") {
78     out example $n->text;
79    
80     } elsif ($t eq "head1") {
81    
82     return if $ignore{$n->title};
83 pcg 1.5
84 pcg 1.6 out "\n\@section " . $n->title . "\n";
85     $walker->($_) for $n->content;
86     out "\n";
87    
88     } elsif ($t eq "head2") {
89     out "\n\n\@subsection " . $n->title . "\n";
90     $walker->($_) for $n->content;
91    
92     } elsif ($t eq "over") {
93     out "\n\n\@itemize\n";
94     $walker->($_) for $n->content;
95     out "\@end itemize\n\n";
96    
97     } elsif ($t eq "item") {
98 pcg 1.8 out "\n\n\@item\n" . $n->title . "\n\n";
99 pcg 1.6
100     if ($n->title->present (TXT::) =~ /^\s*([a-zA-Z0-9\-\_]+)\s*=/) {
101     out "\@cindex $1\n";
102 pcg 1.5 }
103 pcg 1.6 $walker->($_) for $n->content;
104    
105     } elsif ($t eq "begin") {
106     local $Pod::POM::DEFAULT_VIEW = Pod::POM::View::Pod;
107     my $format = $n->format;
108    
109     if ($format =~ /texinfo\s+header/) {
110     $header = $n->content;
111     } elsif ($format =~ /texinfo\s+footer/) {
112     $footer = $n->content;
113 pcg 1.5 } else {
114 pcg 1.6 out $n->content;
115 pcg 1.5 }
116 pcg 1.3
117 pcg 1.6 } elsif ($t eq "for") {
118     my $text = $n->text;
119    
120     if ($text =~ /^menu-begin/) {
121 pcg 1.5 out "\n\@menu\n";
122    
123     push @ctx, {}; # dummy node
124    
125     } elsif ($text =~ /^menu-item (.*?)::\s+(.*)/) {
126     my ($name, $desc) = ($1, $2);
127    
128     push @{ $ctx[-2]{menu} }, [$name, $desc];
129     $ctx[-2]{width} = length $name if $ctx[-2]{width} < length $name;
130    
131     my $ctx = {
132     name => $name,
133     up => $ctx[-2]{name},
134     };
135     push @nodes, $ctx;
136     $ctx[-1] = $ctx;
137    
138     } elsif ($text =~ /^menu-end/) {
139     pop @ctx;
140    
141     for (@{ $ctx[-1]{menu} }) {
142     out sprintf "* %-*s %s\n", $ctx[-1]{width} + 2, "$_->[0]::", $_->[1];
143     }
144    
145     out "\@end menu\n";
146    
147     } elsif ($text =~ /^include (\S+) (.*)/) {
148     my ($type, $path) = ($1, $2);
149    
150     open my $x, "<$path" or die "$path: $!";
151     my $data = do { local $/; <$x> };
152    
153     if ($type eq "pod") {
154     out parse_pod ($data);
155     } elsif ($type eq "text") {
156     out $data;
157     } elsif ($type eq "example") {
158     out example $data;
159     }
160    
161     } else {
162     die "UNKNOWN for command <$text>\n";
163     }
164    
165     } else {
166 pcg 1.6 die "UNKNOWN NODE $t\n";
167 pcg 1.3 }
168 pcg 1.5 };
169    
170 pcg 1.6 $walker->($pod);
171 pcg 1.1 }
172    
173 pcg 1.5 @ctx = @nodes = {
174     up => "(dir)",
175     name => "Top",
176     };
177    
178     parse_pod do { local $/; <> };
179    
180     print $header;
181    
182     for (0 .. $#nodes) {
183     my $node = $nodes[$_];
184     my $prev = $_ > 0 ? $nodes[$_-1] : undef;
185     my $next = $nodes[$_+1];
186 pcg 1.6 my $chapter = $node->{name} eq "Top" ? "Introduction" : $node->{name};
187 pcg 1.5
188     print "\@node $node->{name},$next->{name},$prev->{name},$node->{up}\n\n",
189 pcg 1.6 "\@chapter $chapter\n",
190 pcg 1.5 "$node->{out}\n\n";
191     }
192    
193     print $footer;
194