-
Notifications
You must be signed in to change notification settings - Fork 4
/
man2html
executable file
·193 lines (163 loc) · 3.54 KB
/
man2html
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
#!/usr/bin/perl
sub getIndent {
my ($l) = @_;
$l =~ m/^(\s+)/;
return length ($1);
}
sub printBlock {
$CODE_BLOCK =~ s/<\/B><B>//g;
$CODE_BLOCK =~ s/<\/I><I>//g;
$CODE_BLOCK =~ s/=/<span class="oper">=<\/span>/g;
$CODE_BLOCK =~ s/\(/<span class="oper">(<\/span>/g;
$CODE_BLOCK =~ s/\)/<span class="oper">)<\/span>/g;
$CODE_BLOCK =~ s/\$/<span class="oper">\$<\/span>/g;
print ("<CODE>\n");
print ("$CODE_BLOCK");
print ("</CODE>\n");
$CODE_BLOCK = "";
}
$COLUMNS=90;
if ($ARGV[0] =~ m/^(.*?)\/(.*?)$/ || $ARGV[0] =~ m/^(.*?)\((.*?)\)$/) {
$CMD="man $2 $1";
$COMMAND="$1";
$SECTION=$2;
}
else {
$CMD="man $ARGV[0]";
$COMMAND="$ARGV[0]";
$SECTION=1;
}
$MANCGI="lc://lookup?APP_KEYWORDS=man&text=";
$STYLE = <<END;
<HTML>
<HEAD>
<TITLE>UNIX man pages : $COMMAND ($SECTION)</TITLE>
</HEAD>
<style type="text/css">
body {
padding-left: 20px;
color: black;
background-color: white;
text-align: center;
}
.oper {
color:gray;
}
b {
color: brown;
font-weight: bold;
}
i {
color: gray;
text-decoration: underline;
font-style: normal;
}
a {
color: #990000;
font-style: bold;
}
p {
margin: 25px;
}
dl {
padding-left: 25px;
}
dd {
padding-left: 25px;
}
pre {
font-family: Monaco, monospace;
font-size: 12px;
text-align: left;
display: inline-block;
}
code {
margin-left: 20px;
width: 600px;
display: block;
background-color: lightgray;
font-family: Monaco, monospace;
font-size: 10px;
}
code b {
color: #990000;
font-weight: normal;
}
</style>
END
print ("$STYLE<PRE>\n");
$CODE_BLOCK = "";
$LAST_SECTION = "";
$LAST_LINE = "";
$SPACER = -1;
$LINES = 0;
open (MAN, "PATH=/opt/local/bin:/usr/local/bin:/usr/bin:/bin COLUMNS=$COLUMNS $CMD |");
while (<MAN>) {
chomp ();
$l = $_;
$l =~ s/\s*$//;
#print ("$l\n");
if (m/^(([A-Z][A-Z]?)+)$/) {
$l = $1;
$l =~ s/.//g;
printBlock () if ($CODE_BLOCK ne "");
print ("<H1>$l</H1>\n");
$LAST_SECTION = $l;
$LAST_LINE = $l;
next;
}
elsif (m/^([A-Z].*)$/) {
$l = $1;
$l =~ s/.//g;
printBlock () if ($CODE_BLOCK ne "");
print ("<H3>$l</H3><HR/>\n");
$LAST_SECTION = $l;
$LAST_LINE = $l;
next;
}
$l =~ s/>//g;
$l =~ s/<//g;
$l =~ s/_(.)(.)/<I><B>\1<\/B><\/I>/g;
$l =~ s/_(.)/<I>\1<\/I>/g;
$l =~ s/(.)(.)/<B>\1<\/B>/g;
$l =~ s/``(.*?)''/<B>"\1"<\/B>/g;
$l =~ s/''(.*?)''/<B>"\1"<\/B>/g;
$l =~ s/`(.*?)'/<B>'\1'<\/B>/g;
$l =~ s/'(.*?)'/<B>'\1'<\/B>/g;
$l =~ s/"(.*?)"/<B>'\1'<\/B>/g;
$l =~ s/ ([A-Z][A-Z0-9_]+)[\. ]/<B> \1 <\/B>/g;
$l =~ s//\>/g;
$l =~ s//\</g;
if ($LAST_SECTION eq "NAME" && $SPACER == -1 && $l !~ /^\s*$/) {
$SPACER = getIndent ($l);
}
elsif ($SPACER > 0 && getIndent ($l) > $SPACER && ($LAST_LINE eq "" || getIndent ($LAST_LINE) == 0)) {
$CODE_BLOCK .= "$l\n";
next;
}
if ($CODE_BLOCK ne "") {
if ($l eq "") {
$CODE_BLOCK .= "\n";
next;
}
printBlock () if ($CODE_BLOCK ne "");
}
$l =~ s/<\/B><B>//g;
$l =~ s/<\/I><I>//g;
$l =~ s/<B>([0-9A-z][-.,0-9A-z:]*)<\/B>\(([1-9][A-z]*)\)/<A HREF="$MANCGI\1\/\2">\1(\2)<\/A>/g;
$l =~ s/<I>([0-9A-z][-.,0-9A-z:]*)<\/I>\(([1-9][A-z]*)\)/<A HREF="$MANCGI\1\/\2">\1(\2)<\/A>/g;
$l =~ s/([0-9A-z][-.,0-9A-z:]*)\(([1-9][A-z]*)\)/<A HREF="$MANCGI\1\/\2">\1(\2)<\/A>/g;
$l =~ s/\b(https?:\/\/[-\w\.\/]+)\b/<A HREF="\1">\1<\/A>/g;
$l =~ s/(\s)([A-Z][A-Za-z]+(::[A-Za-z][a-z]+)+?)(\s)/\1<A HREF="$MANCGI\2">\2<\/A>\4/g;
if ($l eq "" && $LAST_LINE eq "") {
next;
}
print ("$l\n");
$LAST_LINE = $l;
$LINES++;
}
if ($LINES == 0) {
print("Manual page not found?");
}
close (MAN);
print ("</PRE></HTML></BODY>\n");