/
my_pod2html
executable file
·240 lines (216 loc) · 7.76 KB
/
my_pod2html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/perl -w
use strict;
use XML::Twig;
my $right_after_name=0; # so I can get the first para as the title
my $handlers=
{ Document => sub { $_->set_tag( 'html');
$_->insert( 'body' => { onLoad => "init_toc()" });
{ local undef $/;
my $head= XML::Twig::Elt->parse( <DATA>);
$head->paste( first_child => $_);
}
},
head1 => sub { process_title( @_, 1); },
head2 => sub { process_title( @_, 2); },
Para => sub { $_->set_tag( 'p');
if( $right_after_name)
{ my $head= XML::Twig::Elt->new( 'head');
my $title= $head->insert( 'title');
$title->set_text( $_->text);
}
},
'L[@type="pod"]' => \&store_link,
'L[@type="url"]' => sub{ $_->set_tag( 'a');
my $href= $_->att( 'to');
$_->del_atts;
$_->set_att( href => $href);
},
F => sub { if( my $a= $_->first_child( 'a'))
{ my $link_text= $_->first_child( '#PCDATA');
$link_text->cut;
$a->set_text( $link_text->text);
}
else
{ $_->set_tag( 'i'); }
},
Verbatim => sub { $_->set_tag( 'pre'); },
C => sub { $_->set_tag( 'tt');
if( $_->text eq '$optional_condition')
{ $_->wrap_in( a => { href => '#METHODS_cond' }); }
},
B => sub { $_->set_tag( 'b'); },
I => sub { $_->set_tag( 'i'); },
'over-text' => sub { $_->set_tag( 'ul'); },
'item-text' => sub { process_title( @_, $_->level + 1); },
_all_ => sub { $_->del_att( 'start_line'); },
_default_ => sub { warn "unknown tag ", $_->sprint, "\n"; },
};
my $t= XML::Twig->new( twig_handlers => $handlers,
pretty_print => 'indented',
);
open( XML, "perldoc -oxml -T $ARGV[0] |") or die $!;
$t->parse( \*XML);
process_links( $t);
foreach my $ul ($t->descendants( 'ul'))
{ next unless( $ul->level( 'ul') > 1);
$ul->sort_children_by_value;
}
$t->print;
sub process_title
{ my( $t, $h, $level)= @_;
$h->set_tag( "h$level");
my $name= $_->text;
if( $name=~ m{^\s*NAME\s*$}i) { $right_after_name=1; }
my $id= id_ify( $h, $level-1);
my $a= $h->insert( 'a', { name => $id});
gen_toc( $t, $name, $id, $level);
}
BEGIN{ my $toc;
my $current_ul;
my $current_level=1;
sub gen_toc
{ my( $t, $name, $id, $level)=@_;
unless( $toc)
{ $toc= XML::Twig::Elt->new( div => { id => "toc" } );
$toc->paste( $t->root);
$current_ul= $toc->insert( 'ul');
my $hr= XML::Twig::Elt->new( hr => '#EMPTY');
$hr->paste( last_child => $toc);
}
my $li = XML::Twig::Elt->new( 'li');
my $a = $li->insert( 'a');
$a->set_att( href => "#$id");
$name=~ s{\(.*\)}{};
$a->set_text( $name);
if( $level < $current_level)
{ while( $level < $current_level)
{ $current_level--; $current_ul= $current_ul->parent( 'ul') || $current_ul; }
}
elsif( $level > $current_level)
{ my $new_ul= XML::Twig::Elt->new( ul => { style => 'display:block;' });
my $current_li= $current_ul->last_child( 'li');
$new_ul->paste( last_child => $current_li);
my $button= XML::Twig::Elt->new( img => { class => "collapsed", src => "/img/plus.png",
height => 20, width => 20,
alt => "Show Next ToC level", onclick => "expand(this)",
style => "display:none",
}
);
$button->paste( after => $current_li->first_child( 'a'));
$current_ul= $new_ul;
}
#warn" level: /$level/ - current_level: /$current_level/\n";
$li->paste( last_child => $current_ul);
$current_level= $level;
}
}
{ my %seen; # complete_id => 1
my @id; # store current titles for level 1 to 3
my %to; # id (not complete) => [ complete id's]
my @link;
sub id_ify
{ my( $h, $level)= @_;
my $text= name_ify( $h->text);
$#id= $level;
$id[$level]= $text;
my $id= join( '_',grep { $_} @id);
if( $seen{$id}) { warn "duplicate id $id\nlevel: ", $h->level, "\n"; }
$seen{$id}++;
push @{$to{$text}}, $id;
return $id
}
sub name_ify
{ my $text= shift or return '';
$text=~ s{\(.*\)}{};
$text=~ s{[\W_]+}{_}g;
$text=~ s{^_}{};
$text=~ s{_$}{};
return $text;
}
sub store_link
{ my( $t, $l)= @_;
my @loc= @id;
push @link, {elt => $l, loc => \@loc };
}
sub process_links
{ my $t= shift;
foreach my $link (@link)
{ my $elt= $link->{elt};
my @loc= $link->{loc};
$elt->set_tag( 'a');
my $to= name_ify( $elt->att( 'to'));
$elt->del_atts;
pop @loc;
push @loc, $to;
my $target= join( '_',grep { $_} @loc );
if( $seen{$target} or ( $target= $to{$to}->[0]))
{ $elt->set_att( href => "#$target"); }
else
{ warn "no target found for /$to/"; }
}
}
}
__DATA__
<head>
<title>XML::Twig - Development version</title>
<!-- scripts -->
<script language="Javascript" type="text/javascript">
// set the display style for the element to display (block or none usually)
function set_display( elt, display)
{ var style_att= elt.getAttributeNode( "style");
if( ! style_att)
{ style_att= document.createAttribute( "style");
child.setAttributeNode( style_att);
}
var style= "display:" + display;
style_att.value= style;
}
// return the first node child of elt with a nodeName of name
function first_child( elt, name)
{ var child= elt.firstChild;
while( child)
{ if( child.nodeName.toLowerCase() == name)
{ return child; }
child= child.nextSibling;
}
return "";
}
// set the display type for the ul after a button
function hide_next_ul (img, display)
{ var ul= first_child( img.parentNode, "ul");
set_display( ul, display)
}
function collapse_section (img)
{ img.className= "collapsed";
img.src= "/img/plus.png";
img.alt= "show next ToC level"
set_display( first_child( img.parentNode, "ul"), "none");
}
function expand_section (img)
{ img.className= "expanded";
img.src= "/img/minus.png";
img.alt= "hide next ToC level"
set_display( first_child( img.parentNode, "ul"), "block");
}
// expands or collapse the Toc
// v == 1 -> expand (set display to 'block' in the first rule
// v == 0 -> collapse (set display to 'none' in the first rule
function expand (img)
{ if( img.className == "expanded")
{ collapse_section( img); }
else
{ expand_section( img); }
}
function init_toc ()
{ // show buttons
var imgs= document.getElementsByTagName( "img");
for( var i = 0; imgs.length > i ; i++)
{ var img= imgs.item(i);
if( (img.className == "expanded") || (img.className == "collapsed") )
{ set_display( img, "inline");
collapse_section( img);
}
}
}
</script>
</head>