ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/doc/pod2texi
(Generate patch)

Comparing gvpe/doc/pod2texi (file contents):
Revision 1.1 by pcg, Fri Mar 28 19:46:47 2003 UTC vs.
Revision 1.5 by pcg, Sat Jan 22 17:39:07 2005 UTC

1#!/usr/bin/perl -p 1#!/usr/bin/perl
2 2
3use Pod::Tree;
4
3sub escape_texi($) { 5sub escape_texi($) {
4 local $_ = shift; 6 local $_ = shift;
5 s/([\@\{\}])/\@$1/g; 7 s/([\@\{\}])/\@$1/g;
6 s/\n+/ /g; 8 s/\n+/ /g;
7 $_; 9 $_;
8} 10}
9 11
10while (<>) { 12sub example {
11 if (/^\@c INCLUDE (\S+)/) { 13 my $text = $_[0];
12 my $name = $1; 14 $text =~ s/\n+$//;
13 my $pod = "$name.pod"; 15 $text =~ s/([\@\{\}])/\@$1/g;
14 16
15 $name =~ s/\.(\d)$/($1)/; 17 "\n\n\@example\n"
16 18 . $text
17 print "\@chapter $name\n\n"; 19 . "\n\@end example\n\n";
18
19 open $x, "<$pod" or die "$pod: $!";
20
21 use Pod::Tree;
22
23 my $pod = new Pod::Tree;
24 $pod->load_string(do { local $/; <$x> });
25
26 my $walker; $walker = sub {
27 my $n = $_[0];
28 if ($n->is_code) {
29 # nop
30 } elsif ($n->is_link) {
31 my $target = $n->get_target;
32 my $page = $target->get_page;
33 my $section = $target->get_section;
34
35 #print "<b>";
36 $walker->($_) for @{$n->get_children};
37 #print "</b>";
38 } elsif ($n->is_text) {
39 print escape_texi $n->get_text;
40 } elsif ($n->is_verbatim) {
41 print "\n\n\@verbatim\n";
42 my $text = $n->get_text;
43 $text =~ s/\n+$//;
44 print $text;
45 print "\n\@end verbatim\n\n";
46 } elsif ($n->is_sequence) {
47 if ($n->get_letter eq "C") {
48 print "\@t{";
49 $walker->($_) for @{$n->get_children};
50 print "}";
51 } elsif ($n->get_letter eq "B") {
52 print "\@strong{";
53 $walker->($_) for @{$n->get_children};
54 print "}";
55 } elsif ($n->get_letter eq "I" or $n->get_letter eq "F") {
56 print "\@emph{";
57 $walker->($_) for @{$n->get_children};
58 print "}";
59 } else {
60 # S would mean to use nbsp
61 $walker->($_) for @{$n->get_children};
62 }
63 } elsif ($n->is_command) {
64 if ($n->is_c_head1) {
65 print "\n\@subsection ";
66 $walker->($_) for @{$n->get_children};
67 print "\n";
68 } elsif ($n->is_c_head2) {
69 print "\n\n\@subsubsection ";
70 $walker->($_) for @{$n->get_children};
71 print "\n";
72 } else {
73 # nop?
74 }
75 } elsif ($n->is_ordinary) {
76 $walker->($_) for @{$n->get_children};
77 print "\@refill\n";
78 } elsif ($n->is_root) {
79 $walker->($_) for @{$n->get_children};
80 } elsif ($n->is_list) {
81 print "\n\n\@itemize\n";
82 $walker->($_) for @{$n->get_children};
83 print "\@end itemize\n\n";
84 } elsif ($n->is_item) {
85 print "\n\n\@item\n";
86 print "\@b{";
87 $walker->($_) for @{$n->get_children};
88 print "}\n\n";
89 $walker->($_) for @{$n->get_siblings};
90 } else {
91 die "UNKNOWN NODE $_[0]{type}<br/>";
92 $walker->($_) for @{$n->get_children};
93 }
94 };
95
96 $walker->($pod->get_root);
97 } else {
98 print;
99 }
100} 20}
101 21
22my @nodes; # nodelist
23my @ctx; # curstack
24
25sub out {
26 $ctx[-1]{out} .= join "", @_;
27}
28
29sub parse_pod {
30 my ($data) = @_;
31 local $out;
32
33 my $pod = new Pod::Tree;
34 $pod->load_string($data);
35
36 my $walker; $walker = sub {
37 my $n = $_[0];
38 if ($n->is_code) {
39 # nop
40 } elsif ($n->is_link) {
41 my $target = $n->get_target;
42 my $page = $target->get_page;
43 my $section = $target->get_section;
44
45 $walker->($_) for @{$n->get_children};
46 } elsif ($n->is_text) {
47 out escape_texi $n->get_text;
48 } elsif ($n->is_verbatim) {
49 out example $n->get_text;
50 } elsif ($n->is_sequence) {
51 if ($n->get_letter eq "C") {
52 out "\@t{";
53 $walker->($_) for @{$n->get_children};
54 out "}";
55 } elsif ($n->get_letter eq "B") {
56 out "\@strong{";
57 $walker->($_) for @{$n->get_children};
58 out "}";
59 } elsif ($n->get_letter eq "I" or $n->get_letter eq "F") {
60 out "\@emph{";
61 $walker->($_) for @{$n->get_children};
62 out "}";
63 } else {
64 # S would mean to use nbsp
65 $walker->($_) for @{$n->get_children};
66 }
67 } elsif ($n->is_command) {
68 if ($n->is_c_head1) {
69 out "\n\@section ";
70 $walker->($_) for @{$n->get_children};
71 out "\n";
72 } elsif ($n->is_c_head2) {
73 out "\n\n\@subsection ";
74 $walker->($_) for @{$n->get_children};
75 out "\n";
76 } else {
77 # nop?
78 }
79 } elsif ($n->is_ordinary) {
80 $walker->($_) for @{$n->get_children};
81 out "\@refill\n";
82 } elsif ($n->is_root) {
83 $walker->($_) for @{$n->get_children};
84 } elsif ($n->is_list) {
85 out "\n\n\@itemize\n";
86 $walker->($_) for @{$n->get_children};
87 out "\@end itemize\n\n";
88 } elsif ($n->is_item) {
89 out "\n\n\@item\n";
90 out "\@b{";
91 $walker->($_) for @{$n->get_children};
92 out "}\n\n";
93 $walker->($_) for @{$n->get_siblings};
94 } elsif ($n->is_for) {
95 my $text = $n->get_text;
96
97 if ($n->get_command eq "begin") {
98 if ($n->{brackets}[0] =~ / header/) {
99 $header = $text;
100 } elsif ($n->{brackets}[0] =~ / footer/) {
101 $footer = $text;
102 } else {
103 out $text;
104 }
105
106 } elsif ($text =~ /^menu-begin/) {
107 out "\n\@menu\n";
108
109 push @ctx, {}; # dummy node
110
111 } elsif ($text =~ /^menu-item (.*?)::\s+(.*)/) {
112 my ($name, $desc) = ($1, $2);
113
114 push @{ $ctx[-2]{menu} }, [$name, $desc];
115 $ctx[-2]{width} = length $name if $ctx[-2]{width} < length $name;
116
117 my $ctx = {
118 name => $name,
119 up => $ctx[-2]{name},
120 };
121 push @nodes, $ctx;
122 $ctx[-1] = $ctx;
123
124 } elsif ($text =~ /^menu-end/) {
125 pop @ctx;
126
127 for (@{ $ctx[-1]{menu} }) {
128 out sprintf "* %-*s %s\n", $ctx[-1]{width} + 2, "$_->[0]::", $_->[1];
129 }
130
131 out "\@end menu\n";
132
133 } elsif ($text =~ /^include (\S+) (.*)/) {
134 my ($type, $path) = ($1, $2);
135
136 open my $x, "<$path" or die "$path: $!";
137 my $data = do { local $/; <$x> };
138
139 if ($type eq "pod") {
140 out parse_pod ($data);
141 } elsif ($type eq "text") {
142 out $data;
143 } elsif ($type eq "example") {
144 out example $data;
145 }
146
147 } else {
148 die "UNKNOWN for command <$text>\n";
149 }
150
151 } else {
152 die "UNKNOWN NODE $_[0]{type}\n";
153 $walker->($_) for @{$n->get_children};
154 }
155 };
156
157 $walker->($pod->get_root);
158}
159
160@ctx = @nodes = {
161 up => "(dir)",
162 name => "Top",
163};
164
165parse_pod do { local $/; <> };
166
167print $header;
168
169for (0 .. $#nodes) {
170 my $node = $nodes[$_];
171 my $prev = $_ > 0 ? $nodes[$_-1] : undef;
172 my $next = $nodes[$_+1];
173
174 print "\@node $node->{name},$next->{name},$prev->{name},$node->{up}\n\n",
175 "\@chapter $node->{name}\n",
176 "$node->{out}\n\n";
177}
178
179print $footer;
180

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines