View File | Revision Log | Show Annotations | Download File | View Changeset
/hippo/hippo-documentation/hst_docs/trunk/scripts/txt2xml
Revision: 21417
Committed: Tue Jan 26 17:00:05 2010 UTC (10 years, 5 months ago) by rbijlsma
File size: 6220 byte(s)
Log Message:
HSTTWODOCS-242

File Contents

# Content
1 #!/usr/bin/gawk -f
2 #
3 # NAME
4 # :: Preformatter: it changes characters that are forbidden in text in XML (< and >)
5 # and adds p tags on empty lines.
6 #
7 #
8 # OPTIONS
9 # escape :: If 1, elements in the input are escaped. All html elements are in a prefixed namespace so that
10 # even without escaping, the imported elements will be recognized as not being operational html.
11 # Only elements with prefix `h' will be recognized as operational html.
12 #
13
14 # _control["after"] = empty|postitle|sectiontitle|line
15 # Class of previous line
16 # _control["openpar"] = h:p|h:li|...
17 # Element name of open paragraph-like elements, i.e. that do not need a paragraph in them.
18 # _control["last"] = sectiontitle|line
19 #
20 # _control["openblock"] = h:ol|h:ul|...
21 # Element name of last open block. Note that you can not nest
22 # blocks, as this is a feature not expected in the text input.
23 # You can have a paragraph in an block, though.
24 # _control["lastblock"] = _control["openblock"]
25 # _control["lastpar"] = _control["openpar"]
26 #
27 #
28
29 BEGIN {
30 # TODO print XML header line
31 _control["after"] = "empty"
32
33 print "<?xml version='1.0' encoding='UTF-8'?>"
34 print "<h:html\n xmlns:h='http://www.w3.org/1999/xhtml'\n xmlns:docs='http://www.hippoecm.org/nt/docs/1.2'\n>"
35 print " <h:body>"
36 }
37
38
39 {
40 gsub(/&/, "\\&amp;" )
41 if ( escape == 1 ) {
42 gsub(/</, "ss\\&lt;" )
43 gsub(/>/, "\\&gt;" )
44 }
45 }
46
47 /^[[:blank:]]*Key:/ && _control["openblock"] != "docs:goal" {
48 closeblock()
49 closepar()
50 printf "\n <docs:goal>"
51 _control["openblock"]="docs:goal"
52 _control["after"]="line"
53 }
54
55
56 /^[[:blank:]]*<|;$/ && _control["openblock"] != "h:blockquote" {
57 closeblock()
58 closepar()
59 printf "\n <h:blockquote>"
60 printf "\n <h:code>"
61 _control["openpar"]="h:code"
62 _control["openblock"]="h:blockquote"
63 _control["after"]="codestart"
64 }
65
66
67 _control["openblock"] == "h:blockquote" {
68
69 if ( /^[[:blank:]]*$/ ) {
70 _control["after"]="empty"
71 next
72 } else if ( _control["after"]=="empty" && $0 !~ /^[[:blank:]]*<|;$/ ) {
73 closepar()
74 closeblock()
75 } else {
76 if ( _control["after"]=="empty" ) print ""
77 if ( _control["after"]!="codestart" ) printf "\n"
78 printf "%s", $0
79 _control["after"]="line"
80 next
81 }
82 }
83
84
85 /^[[:blank:]]*<|;$/ && _control["openblock"] != "h:blockquote" {
86 closeblock()
87 closepar()
88 printf "\n <h:blockquote>"
89 printf "\n <h:code>"
90 _control["openpar"]="h:code"
91 _control["openblock"]="h:blockquote"
92 _control["after"]="codestart"
93 }
94
95
96 _control["openblock"] == "h:blockquote" {
97
98 if ( /^[[:blank:]]*$/ ) {
99 _control["after"]="empty"
100 next
101 } else if ( _control["after"]=="empty" && $0 !~ /^[[:blank:]]*<|;$/ ) {
102 closepar()
103 closeblock()
104 } else {
105 if ( _control["after"]=="empty" ) print ""
106 if ( _control["after"]!="codestart" ) printf "\n"
107 printf "%s", $0
108 _control["after"]="line"
109 next
110 }
111 }
112
113
114 /^[[:blank:]]*----------------------------*$|^[[:blank:]]*___________________________*$|^[[:blank:]]*=============================*$/ {
115
116 if ( _control["after"] == "postitle" ) {
117 printf "\n<h:h2>%s</h:h2>\n", _data["postitle"]
118 # printf "<subsection name=\"%s\"/>\n", _data["postitle"]
119 _control["last"]="sectiontitle"
120 _control["after"]="sectiontitle"
121 } else { printf "\n<h:br/>"
122 _control["last"]="line"
123 _control["after"]="empty"
124 }
125 next
126 }
127
128
129 /^[[:blank:]]*$/ {
130
131 if ( _control["after"] != "empty" ) {
132 closepar()
133 closeblock()
134 }
135 print ""
136
137 if ( _control["after"] == "postitle" ) {
138 printpostitle()
139 closepar()
140 }
141
142 _control["after"]="empty"
143 next
144 }
145
146
147 function makeli(list, entry) {
148
149 if ( entry == "" ) entry = $0
150
151 if ( _control["openblock"] != list) {
152 printf "\n<%s>", list
153 _control["openblock"] = list
154 }
155 else closepar()
156 _control["openpar"]="h:li"
157
158 entry = activate_links(entry)
159 printf "\n <h:li>%s", entry
160 _control["after"]="line"
161 }
162
163
164 _control["after"] == "empty" {
165
166 _data["postitle"]=$0
167 _control["after"]="postitle"
168 next
169 }
170
171
172 # ________________________________________________________________
173 # print possible title
174 #
175 function printpostitle() {
176
177 if ( length(_data["postitle"]) < 40 ) {
178 printf "\n <h:h3>%s</h:h3>\n", _data["postitle"]
179 _control["after"] = "sectiontitle"
180 _control["last"] = "sectiontitle"
181 } else if ( $0 ~ /^[[:blank:]]*[0-9]+\.[[:blank:]]/ && sub( /^[[:blank:]]*[0-9]+\.[[:blank:]]/, "", _data["postitle"] ) ) {
182 makeli("h:ol", _data["postitle"])
183 } else {
184 _data["postitle"] = activate_links(_data["postitle"])
185 printf "%s%s", " <h:p>", _data["postitle"]
186 _control["openpar"]="h:p"
187 _control["after"]="line"
188 }
189 }
190
191
192 _control["after"]=="postitle" {
193 printpostitle()
194 # No next!
195 }
196
197
198 sub(/^[[:blank:]]*[0-9]+\.[[:blank:]]/, "") {
199 makeli("h:ol")
200 next
201 }
202
203 sub(/^[[:blank:]]*[-\*][[:blank:]]/, "") || $0 ~ /^[A-Z]+-[0-9]+/ {
204 makeli("h:ul")
205 next
206 }
207
208
209 _control["after"]=="empty" || _control["after"]=="sectiontitle" {
210 closepar()
211 activate_links()
212 printf "\n <h:p>%s", $0
213 _control["openpar"]="h:p"
214 _control["after"]="line"
215 next
216 }
217
218
219 {
220 printf "\n%s", activate_links($0)
221 }
222
223
224 function activate_links(entry) {
225
226 if ( entry == "" ) entry = $0
227
228 if ( ! gsub(/http:[^[:blank:]\(\)]+/, "<h:code><h:a href='&'>&</h:a></h:code>", entry ) )
229 gsub(/[A-Z][A-Z]+-[0-9]+/, "<h:a href='http://issues.onehippo.com/browse/&'>&</h:a>", entry )
230
231 return entry
232 }
233
234
235 function closepar() {
236
237 if ( _control["openpar"] != "" ) {
238 printf "</%s>", _control["openpar"]
239 _control["openpar"]=""
240 _control["lastpar"]=_control["openpar"]
241 }
242 }
243
244
245 function closeblock() {
246
247 if ( _control["openblock"] != "" ) {
248 printf "</%s>", _control["openblock"]
249 _control["openblock"]=""
250 _control["lastblock"]=_control["openblock"]
251 }
252 }
253
254
255 END {
256 closepar()
257 closeblock()
258
259 print "</h:body>"
260 print "</h:html>"
261 }

Properties

Name Value
svn:executable *