1 | #!/usr/bin/perl -p |
1 | #!/usr/bin/perl |
2 | |
2 | |
|
|
3 | use Pod::Tree; |
|
|
4 | |
3 | sub escape_texi($) { |
5 | sub 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 | |
10 | while (<>) { |
12 | sub 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\@example\n"; |
|
|
42 | my $text = $n->get_text; |
|
|
43 | $text =~ s/\n+$//; |
|
|
44 | $text =~ s/([\@\{\}])/@$1/g; |
|
|
45 | print $text; |
|
|
46 | print "\n\@end example\n\n"; |
|
|
47 | } elsif ($n->is_sequence) { |
|
|
48 | if ($n->get_letter eq "C") { |
|
|
49 | print "\@t{"; |
|
|
50 | $walker->($_) for @{$n->get_children}; |
|
|
51 | print "}"; |
|
|
52 | } elsif ($n->get_letter eq "B") { |
|
|
53 | print "\@strong{"; |
|
|
54 | $walker->($_) for @{$n->get_children}; |
|
|
55 | print "}"; |
|
|
56 | } elsif ($n->get_letter eq "I" or $n->get_letter eq "F") { |
|
|
57 | print "\@emph{"; |
|
|
58 | $walker->($_) for @{$n->get_children}; |
|
|
59 | print "}"; |
|
|
60 | } else { |
|
|
61 | # S would mean to use nbsp |
|
|
62 | $walker->($_) for @{$n->get_children}; |
|
|
63 | } |
|
|
64 | } elsif ($n->is_command) { |
|
|
65 | if ($n->is_c_head1) { |
|
|
66 | print "\n\@subsection "; |
|
|
67 | $walker->($_) for @{$n->get_children}; |
|
|
68 | print "\n"; |
|
|
69 | } elsif ($n->is_c_head2) { |
|
|
70 | print "\n\n\@subsubsection "; |
|
|
71 | $walker->($_) for @{$n->get_children}; |
|
|
72 | print "\n"; |
|
|
73 | } else { |
|
|
74 | # nop? |
|
|
75 | } |
|
|
76 | } elsif ($n->is_ordinary) { |
|
|
77 | $walker->($_) for @{$n->get_children}; |
|
|
78 | print "\@refill\n"; |
|
|
79 | } elsif ($n->is_root) { |
|
|
80 | $walker->($_) for @{$n->get_children}; |
|
|
81 | } elsif ($n->is_list) { |
|
|
82 | print "\n\n\@itemize\n"; |
|
|
83 | $walker->($_) for @{$n->get_children}; |
|
|
84 | print "\@end itemize\n\n"; |
|
|
85 | } elsif ($n->is_item) { |
|
|
86 | print "\n\n\@item\n"; |
|
|
87 | print "\@b{"; |
|
|
88 | $walker->($_) for @{$n->get_children}; |
|
|
89 | print "}\n\n"; |
|
|
90 | $walker->($_) for @{$n->get_siblings}; |
|
|
91 | } else { |
|
|
92 | die "UNKNOWN NODE $_[0]{type}<br/>"; |
|
|
93 | $walker->($_) for @{$n->get_children}; |
|
|
94 | } |
|
|
95 | }; |
|
|
96 | |
|
|
97 | $walker->($pod->get_root); |
|
|
98 | } else { |
|
|
99 | print; |
|
|
100 | } |
|
|
101 | } |
20 | } |
102 | |
21 | |
|
|
22 | my @nodes; # nodelist |
|
|
23 | my @ctx; # curstack |
|
|
24 | |
|
|
25 | sub out { |
|
|
26 | $ctx[-1]{out} .= join "", @_; |
|
|
27 | } |
|
|
28 | |
|
|
29 | sub 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 | |
|
|
165 | parse_pod do { local $/; <> }; |
|
|
166 | |
|
|
167 | print $header; |
|
|
168 | |
|
|
169 | for (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 | |
|
|
179 | print $footer; |
|
|
180 | |