/
html2wiki.sh
154 lines (149 loc) · 4.23 KB
/
html2wiki.sh
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
#!/bin/bash
#
# Translate really simple html to googlecode.com wiki.
#
# Usage: cat input.html | html2wiki.sh > outputwiki.txt
#
# Most of this script is simple sed substitutions with an awk script to handle
# hierarchical lists.
# Awk program to escape all instances of * outside of <listing></listing>
awk '
BEGIN { in_listing = 0; }
/<[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/ { in_listing = 1; }
/<\/[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/ { in_listing = 0; }
/.*/ {
if (in_listing) {
print $0;
} else {
print gensub("*", "`*`", "g", $0)
}
}' | \
# Awk program to convert hierachical unordered and ordered lists into
# googlecode wiki list markup. This is limited to converting very simple
# html lists in the form:
#
# <ul>
# <li>item 1</li>
# ...
# <li>item N</li>
# </ul>
#
# This script also removes leading spaces from all lines outside of <listing>
# sections.
awk '
BEGIN {
list_type_none = 0;
list_type_ordered = 1;
list_type_unordered = 2;
# Number of nested lists.
list_depth = 0;
# Number of items in the list.
list_items[list_depth] = 0;
# Type of list.
list_type[list_depth] = list_type_none;
# Do nott strip whitespace from listing sections.
in_listing = 0;
}
# Generate a string of indent spaces.
function list_indent(indent) {
format = sprintf("%%%ds", indent);
return sprintf(format, "");
}
/<[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/ { in_listing = 1; }
/<\/[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>/ { in_listing = 0; }
# Process all lines non-blank lines.
/^.*$/ {
# Remove leading white space.
if (!in_listing) {
output_string = gensub(/^ */, "", 1, $0);
} else {
output_string = $0;
}
search_string = output_string
# Replace list tags with googlecode wiki markup.
while (match(search_string, /<[^>]*>/, matches)) {
tag = matches[0];
search_string = substr(search_string,
matches[0, "start"] + matches[0, "length"]);
if (match(tag, /^<[Uu][Ll]>$/)) {
list_depth++;
list_type[list_depth] = list_type_unordered;
list_items[list_depth] = 0;
output_string = gensub(tag, "", 1, output_string);
} else if (match(tag, /^[Oo][Ll]>$/)) {
list_depth++;
list_type[list_depth] = list_type_ordered;
list_items[list_depth] = 0;
output_string = gensub(tag, "", 1, output_string);
} else if (match(tag, /^<\/[Ll][Ii]>$/)) {
output_string = gensub(tag, "", 1, output_string);
} else if (list_depth) {
if (match(tag, /^<[Ll][Ii]>$/)) {
if (list_type[list_depth] == list_type_unordered) {
output_string = gensub(tag, list_indent(list_depth) "* ", 1,
output_string);
} else if (list_type[list_depth] == list_type_ordered) {
output_string = gensub(tag, list_indent(list_depth) "# ", 1,
output_string);
}
} else if (match(tag, /^<\/[Uu][Ll]>$/) ||
match(tag, /^<\/[Ou][Ll]>$/)) {
output_string = gensub(tag, "", 1, output_string);
list_depth --;
}
}
}
# If a list is being parsed then filter blank lines.
if (list_depth == 0 || length(output_string)) {
print output_string
}
}
' | \
# This sed program translates really simple html into wiki suitable for
# googlecode.com.
#
# Supported tags:
# <p>
# <br>
# <h1>
# <h2>
# <h3>
# <h4>
# <h5>
# <b>
# <i>
# <a href="#.*">.*</a>
# <a href=".*">.*</a>
# <a name=".*'>.*</a>
#
# Supported entities:
# >
# <
#
# Limitations:
# * Anchors must be on a single line and must contain one of either the name or
# href attributes.
# * Href of local anchors (href="#.*") should be set to the name of a heading
# within the document. If the heading contains spaces the href should
# contain underscores.
# * All external links are relative to
# http://cmockery.googlecode.com/svn/trunk/doc/
sed -r '
s@<[Pp]>@\n@g;
s@<[[Bb][Rr]]>@\n@g;
s@</?[Hh]1>@=@g;
s@</?[Hh]2>@==@g;
s@</?[Hh]3>@===@g;
s@</?[Hh]4>@====@g;
s@</?[Hh]5>@====@g;
s@</?[Bb]>@*@g;
s@</?[Ii]>@_@g;
s@<[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>@{{{@g;
s@</[Ll][Ii][Ss][Tt][Ii][Nn][Gg]>@}}}@g;
s@<[Aa].*?href="#(.*)?">(.*)?</[Aa]>@[#\1 \2]@g;
s@<[Aa].*?href="(.*)?">(.*)?</[Aa]>@[http://cmockery.googlecode.com/svn/trunk/doc/\1 \2]@g;
s@<[Aa].*?name="(.*)?">@@g;
s@</[Aa]>@@g;
s@<.*?>@@g;
s@<@<@g;
s@>@>@g;'