-
Notifications
You must be signed in to change notification settings - Fork 1
/
t2ol.pl
executable file
·66 lines (52 loc) · 1.8 KB
/
t2ol.pl
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
#!/usr/bin/perl
# (c) 2005 Eric Hsu <textmate@betterfilecabinet.com>
# Produce ordered HTML lists from Tab-indented standard input
# Just a hack.
# Based on t2h.pl
my $FIRST_LINE = 1;
my $html = "";
my @type = (1,a,i,A,I,1,a,i,A,I,1,a,i,A,I,1,a,i,A,I); # first level uses type=1, second uses type=1, etc.
while (<>) {
( $tablead, $text ) = /^(\t*)(.*?)$/;
$tabs = length($tablead);
if ($FIRST_LINE) {
$FIRST_LINE_TABS=$tabs;
$FIRST_LINE = 0;
$html = "<ol type=" . $type[$tabs-$FIRST_LINE_TABS] . ">\n";
# first time through, start the list.
$FIRST_LIST_ITEM=1;
}
elsif ( $tabs < $lasttab ) {
foreach ( 1 .. ( $lasttab - $tabs ) ) {
$html .= "</li>\n" . "\t" x ($lasttab-$_). "</ol>\n";
# if the indent is less than the last,
# then close up one list level for each tab less
# by closing the last element, then closing the ul list.
# That leaves the last <li> containing the ul unclosed,
# but that's okay. The next pass will close it.
}
}
elsif ( $tabs > $lasttab ) {
$html .= "\n" . "\t" x $lasttab . "<ol type="
. $type[$tabs-$FIRST_LINE_TABS] . ">\n";
$FIRST_LIST_ITEM=1;
# if the indent is more, then start a new list.
# To validate, don't close up the last </li>; we do that in the
# previous clause when the tabs end.
}
unless ($FIRST_LIST_ITEM) {
$html .= "</li>\n"; # close the last one unless it's the first in the list
} else {
$FIRST_LIST_ITEM=0;
}
$html .= "$tablead<li>$text";
$lasttab = $tabs;
}
# close up all the remaining tabs.
foreach ( 1 .. ( $lasttab - $FIRST_LINE_TABS ) ) {
$html .= "</li>\n" . "\t" x ($lasttab-$_). "</ol>\n";
# if the indent is less than the last,
# then close up one list level for each tab less
}
$html .= "</li>\n</ol>\n";
print $html;