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

Comparing gvpe/doc/pod2texi (file contents):
Revision 1.5 by pcg, Sat Jan 22 17:39:07 2005 UTC vs.
Revision 1.8 by pcg, Wed Aug 27 22:14:03 2008 UTC

1#!/usr/bin/perl 1#!/usr/bin/perl
2 2
3use Pod::Tree; 3# TODO: texinfo fails on @cindex in title elements etc. *sigh*
4
5use Pod::POM;
4 6
5sub escape_texi($) { 7sub escape_texi($) {
6 local $_ = shift; 8 local $_ = shift;
7 s/([\@\{\}])/\@$1/g; 9 s/([\@\{\}])/\@$1/g;
8 s/\n+/ /g; 10 s/\n+/ /g;
24 26
25sub out { 27sub out {
26 $ctx[-1]{out} .= join "", @_; 28 $ctx[-1]{out} .= join "", @_;
27} 29}
28 30
31sub TEX::view_seq_code { "\@t{$_[1]}" }
32sub TEX::view_seq_bold { "\@strong{$_[1]}" }
33sub TEX::view_seq_italic { "\@emph{$_[1]}" }
34
35sub TEX::view_seq_space { escape_texi $_[1] }
36sub TEX::view_seq_text { escape_texi $_[1] }
37
38sub TEX::view_seq_link { $_[1] }
39sub TEX::view_seq_index {
40 "\n\@cindex $_[1]\n$_[1]"
41}
42
43*TXT::view_seq_code =
44*TXT::view_seq_bold =
45*TXT::view_seq_italic =
46*TXT::view_seq_space =
47*TXT::view_seq_text =
48*TXT::view_seq_link =
49*TXT::view_seq_index = sub { $_[1] };
50
51my %ignore = (
52 "SEE ALSO" => 1,
53 "AUTHOR" => 1,
54);
55
29sub parse_pod { 56sub parse_pod {
30 my ($data) = @_; 57 my ($data) = @_;
31 local $out; 58 local $out;
32 59
60 local $Pod::POM::DEFAULT_VIEW = TEX::;
61
33 my $pod = new Pod::Tree; 62 my $parser = new Pod::POM;
34 $pod->load_string($data); 63 my $pod = $parser->parse_text ($data)
64 or die;
35 65
36 my $walker; $walker = sub { 66 my $walker; $walker = sub {
37 my $n = $_[0]; 67 my $n = $_[0];
38 if ($n->is_code) { 68 my $t = $n->type;
39 # nop 69
40 } elsif ($n->is_link) { 70 if ($t eq "text") {
41 my $target = $n->get_target; 71 out $n->text . "\n\@refill\n";
42 my $page = $target->get_page; 72
43 my $section = $target->get_section; 73 } elsif ($t eq "pod") {
74 $walker->($_) for $n->content;
75
76 } elsif ($t eq "verbatim") {
77 out example $n->text;
78
79 } elsif ($t eq "head1") {
80
81 return if $ignore{$n->title};
44 82
83 out "\n\@section " . $n->title . "\n";
45 $walker->($_) for @{$n->get_children}; 84 $walker->($_) for $n->content;
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 "}"; 85 out "\n";
55 } elsif ($n->get_letter eq "B") { 86
56 out "\@strong{"; 87 } elsif ($t eq "head2") {
88 out "\n\n\@subsection " . $n->title . "\n";
57 $walker->($_) for @{$n->get_children}; 89 $walker->($_) for $n->content;
58 out "}"; 90
59 } elsif ($n->get_letter eq "I" or $n->get_letter eq "F") { 91 } elsif ($t eq "over") {
60 out "\@emph{"; 92 out "\n\n\@itemize\n";
61 $walker->($_) for @{$n->get_children}; 93 $walker->($_) for $n->content;
62 out "}"; 94 out "\@end itemize\n\n";
95
96 } elsif ($t eq "item") {
97 out "\n\n\@item\n" . $n->title . "\n\n";
98
99 if ($n->title->present (TXT::) =~ /^\s*([a-zA-Z0-9\-\_]+)\s*=/) {
100 out "\@cindex $1\n";
101 }
102 $walker->($_) for $n->content;
103
104 } elsif ($t eq "begin") {
105 local $Pod::POM::DEFAULT_VIEW = Pod::POM::View::Pod;
106 my $format = $n->format;
107
108 if ($format =~ /texinfo\s+header/) {
109 $header = $n->content;
110 } elsif ($format =~ /texinfo\s+footer/) {
111 $footer = $n->content;
63 } else { 112 } else {
64 # S would mean to use nbsp 113 out $n->content;
65 $walker->($_) for @{$n->get_children};
66 } 114 }
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 115
97 if ($n->get_command eq "begin") { 116 } elsif ($t eq "for") {
98 if ($n->{brackets}[0] =~ / header/) { 117 my $text = $n->text;
99 $header = $text;
100 } elsif ($n->{brackets}[0] =~ / footer/) {
101 $footer = $text;
102 } else {
103 out $text;
104 }
105 118
106 } elsif ($text =~ /^menu-begin/) { 119 if ($text =~ /^menu-begin/) {
107 out "\n\@menu\n"; 120 out "\n\@menu\n";
108 121
109 push @ctx, {}; # dummy node 122 push @ctx, {}; # dummy node
110 123
111 } elsif ($text =~ /^menu-item (.*?)::\s+(.*)/) { 124 } elsif ($text =~ /^menu-item (.*?)::\s+(.*)/) {
147 } else { 160 } else {
148 die "UNKNOWN for command <$text>\n"; 161 die "UNKNOWN for command <$text>\n";
149 } 162 }
150 163
151 } else { 164 } else {
152 die "UNKNOWN NODE $_[0]{type}\n"; 165 die "UNKNOWN NODE $t\n";
153 $walker->($_) for @{$n->get_children};
154 } 166 }
155 }; 167 };
156 168
157 $walker->($pod->get_root); 169 $walker->($pod);
158} 170}
159 171
160@ctx = @nodes = { 172@ctx = @nodes = {
161 up => "(dir)", 173 up => "(dir)",
162 name => "Top", 174 name => "Top",
168 180
169for (0 .. $#nodes) { 181for (0 .. $#nodes) {
170 my $node = $nodes[$_]; 182 my $node = $nodes[$_];
171 my $prev = $_ > 0 ? $nodes[$_-1] : undef; 183 my $prev = $_ > 0 ? $nodes[$_-1] : undef;
172 my $next = $nodes[$_+1]; 184 my $next = $nodes[$_+1];
185 my $chapter = $node->{name} eq "Top" ? "Introduction" : $node->{name};
173 186
174 print "\@node $node->{name},$next->{name},$prev->{name},$node->{up}\n\n", 187 print "\@node $node->{name},$next->{name},$prev->{name},$node->{up}\n\n",
175 "\@chapter $node->{name}\n", 188 "\@chapter $chapter\n",
176 "$node->{out}\n\n"; 189 "$node->{out}\n\n";
177} 190}
178 191
179print $footer; 192print $footer;
180 193

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines