-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwtex.py
369 lines (359 loc) · 16.2 KB
/
wtex.py
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from __future__ import with_statement
import re
from util import Path, GPath
codebox = None
class WryeText:
"""This class provides a function for converting wtxt text files to html
files.
Headings:
= XXXX >> H1 "XXX"
== XXXX >> H2 "XXX"
=== XXXX >> H3 "XXX"
==== XXXX >> H4 "XXX"
Notes:
* These must start at first character of line.
* The XXX text is compressed to form an anchor. E.g == Foo Bar gets anchored as" FooBar".
* If the line has trailing ='s, they are discarded. This is useful for making
text version of level 1 and 2 headings more readable.
Bullet Lists:
* Level 1
* Level 2
* Level 3
Notes:
* These must start at first character of line.
* Recognized bullet characters are: - ! ? . + * o The dot (.) produces an invisible
bullet, and the * produces a bullet character.
Styles:
__Text__
~~Italic~~
**BoldItalic**
Notes:
* These can be anywhere on line, and effects can continue across lines.
Links:
[[file]] produces <a href=file>file</a>
[[file|text]] produces <a href=file>text</a>
[[!file]] produces <a href=file target="_blank">file</a>
[[!file|text]] produces <a href=file target="_blank">text</a>
Contents
{{CONTENTS=NN}} Where NN is the desired depth of contents (1 for single level,
2 for two levels, etc.).
"""
# Data ------------------------------------------------------------------------
htmlHead = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>%s</title>
<style type="text/css">%s</style>
</head>
<body>
"""
defaultCss = """
h1 { margin-top: 0in; margin-bottom: 0in; border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: none; border-right: none; padding: 0.02in 0in; background: #c6c63c; font-family: "Arial", serif; font-size: 12pt; page-break-before: auto; page-break-after: auto }
h2 { margin-top: 0in; margin-bottom: 0in; border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: none; border-right: none; padding: 0.02in 0in; background: #e6e64c; font-family: "Arial", serif; font-size: 10pt; page-break-before: auto; page-break-after: auto }
h3 { margin-top: 0in; margin-bottom: 0in; font-family: "Arial", serif; font-size: 10pt; font-style: normal; page-break-before: auto; page-break-after: auto }
h4 { margin-top: 0in; margin-bottom: 0in; font-family: "Arial", serif; font-style: italic; page-break-before: auto; page-break-after: auto }
a:link { text-decoration:none; }
a:hover { text-decoration:underline; }
p { margin-top: 0.01in; margin-bottom: 0.01in; font-family: "Arial", serif; font-size: 10pt; page-break-before: auto; page-break-after: auto }
p.empty {}
p.list-1 { margin-left: 0.15in; text-indent: -0.15in }
p.list-2 { margin-left: 0.3in; text-indent: -0.15in }
p.list-3 { margin-left: 0.45in; text-indent: -0.15in }
p.list-4 { margin-left: 0.6in; text-indent: -0.15in }
p.list-5 { margin-left: 0.75in; text-indent: -0.15in }
p.list-6 { margin-left: 1.00in; text-indent: -0.15in }
.code-n { background-color: #FDF5E6; font-family: "Lucide Console", monospace; font-size: 10pt; white-space: pre; }
pre { border: 1px solid; overflow: auto; width: 750px; word-wrap: break-word; background: #FDF5E6; padding: 0.5em; margin-top: 0in; margin-bottom: 0in; margin-left: 0.25in}
code { background-color: #FDF5E6; font-family: "Lucida Console", monospace; font-size: 10pt; }
td.code { background-color: #FDF5E6; font-family: "Lucida Console", monospace; font-size: 10pt; border: 1px solid #000000; padding:5px; width:50%;}
body { background-color: #ffffcc; }
"""
# Conversion ------------------------------------------------------------------
@staticmethod
def genHtml(ins,out=None,*cssDirs):
"""Reads a wtxt input stream and writes an html output stream."""
# Path or Stream? -----------------------------------------------
if isinstance(ins,(Path,str,unicode)):
srcPath = GPath(ins)
outPath = GPath(out) or srcPath.root+'.html'
cssDirs = (srcPath.head,) + cssDirs
ins = srcPath.open()
out = outPath.open('w')
else:
srcPath = outPath = None
cssDirs = map(GPath,cssDirs)
# Setup ---------------------------------------------------------
#--Headers
reHead = re.compile(r'(=+) *(.+)')
headFormat = "<h%d><a id='%s'>%s</a></h%d>\n"
headFormatNA = "<h%d>%s</h%d>\n"
#--List
reList = re.compile(r'( *)([-x!?\.\+\*o])(.*)')
#--Code
reCode = re.compile(r'\[code\](.*?)\[/code\]',re.I)
reCodeStart = re.compile(r'(.*?)\[code\](.*?)$',re.I)
reCodeEnd = re.compile(r'(.*?)\[/code\](.*?)$',re.I)
reCodeBoxStart = re.compile(r'\s*\[codebox\](.*?)',re.I)
reCodeBoxEnd = re.compile(r'(.*?)\[/codebox\]\s*',re.I)
reCodeBox = re.compile(r'\s*\[codebox\](.*?)\[/codebox\]\s*',re.I)
codeLines = None
codeboxLines = None
def subCode(match):
try:
return ' '.join(codebox([match.group(1)],False,False))
except:
return match(1)
#--Misc. text
reHr = re.compile('^------+$')
reEmpty = re.compile(r'\s+$')
reMDash = re.compile(r' -- ')
rePreBegin = re.compile('<pre',re.I)
rePreEnd = re.compile('</pre>',re.I)
anchorlist = [] #to make sure that each anchor is unique.
def subAnchor(match):
text = match.group(1)
anchor = reWd.sub('',text)
count = 0
if re.match(r'\d', anchor):
anchor = '_' + anchor
while anchor in anchorlist and count < 10:
count += 1
if count == 1:
anchor = anchor + str(count)
else:
anchor = anchor[:-1] + str(count)
anchorlist.append(anchor)
return "<a id='%s'>%s</a>" % (anchor,text)
#--Bold, Italic, BoldItalic
reBold = re.compile(r'__')
reItalic = re.compile(r'~~')
reBoldItalic = re.compile(r'\*\*')
states = {'bold':False,'italic':False,'boldItalic':False,'code':0}
def subBold(match):
state = states['bold'] = not states['bold']
return ('</b>','<b>')[state]
def subItalic(match):
state = states['italic'] = not states['italic']
return ('</i>','<i>')[state]
def subBoldItalic(match):
state = states['boldItalic'] = not states['boldItalic']
return ('</b></i>','<i><b>')[state]
#--Preformatting
#--Links
reLink = re.compile(r'\[\[(.*?)\]\]')
reHttp = re.compile(r' (http://[_~a-zA-Z0-9\./%-]+)')
reWww = re.compile(r' (www\.[_~a-zA-Z0-9\./%-]+)')
reWd = re.compile(r'(<[^>]+>|\[[^\]]+\]|\W+)')
rePar = re.compile(r'^(\s*[a-zA-Z(;]|\*\*|~~|__|\s*<i|\s*<a)')
reFullLink = re.compile(r'(:|#|\.[a-zA-Z0-9]{2,4}$)')
reColor = re.compile(r'\[\s*color\s*=[\s\"\']*(.+?)[\s\"\']*\](.*?)\[\s*/\s*color\s*\]',re.I)
reBGColor = re.compile(r'\[\s*bg\s*=[\s\"\']*(.+?)[\s\"\']*\](.*?)\[\s*/\s*bg\s*\]',re.I)
def subColor(match):
return '<span style="color:%s;">%s</span>' % (match.group(1),match.group(2))
def subBGColor(match):
return '<span style="background-color:%s;">%s</span>' % (match.group(1),match.group(2))
def subLink(match):
address = text = match.group(1).strip()
if '|' in text:
(address,text) = [chunk.strip() for chunk in text.split('|',1)]
if address == '#': address += reWd.sub('',text)
if address.startswith('!'):
newWindow = ' target="_blank"'
address = address[1:]
else:
newWindow = ''
if not reFullLink.search(address):
address = address+'.html'
return '<a href="%s"%s>%s</a>' % (address,newWindow,text)
#--Tags
reAnchorTag = re.compile('{{A:(.+?)}}')
reContentsTag = re.compile(r'\s*{{CONTENTS=?(\d+)}}\s*$')
reAnchorHeadersTag = re.compile(r'\s*{{ANCHORHEADERS=(\d+)}}\s*$')
reCssTag = re.compile('\s*{{CSS:(.+?)}}\s*$')
#--Defaults ----------------------------------------------------------
title = ''
level = 1
spaces = ''
cssName = None
#--Init
outLines = []
contents = []
addContents = 0
inPre = False
anchorHeaders = True
#--Read source file --------------------------------------------------
for line in ins:
#--Codebox -----------------------------------
if codebox:
if codeboxLines is not None:
maCodeBoxEnd = reCodeBoxEnd.match(line)
if maCodeBoxEnd:
codeboxLines.append(maCodeBoxEnd.group(1))
outLines.append('<pre style="width:850px;">')
try:
codeboxLines = codebox(codeboxLines)
except:
pass
outLines.extend(codeboxLines)
outLines.append('</pre>')
codeboxLines = None
continue
else:
codeboxLines.append(line)
continue
maCodeBox = reCodeBox.match(line)
if maCodeBox:
outLines.append('<pre style="width:850px;">')
try:
outLines.extend(codebox([maCodeBox.group(1)]))
except:
outLines.append(maCodeBox.group(1))
outLines.append('</pre>\n')
continue
maCodeBoxStart = reCodeBoxStart.match(line)
if maCodeBoxStart:
codeboxLines = [maCodeBoxStart.group(1)]
continue
#--Code --------------------------------------
if codeLines is not None:
maCodeEnd = reCodeEnd.match(line)
if maCodeEnd:
codeLines.append(maCodeEnd.group(1))
try:
codeLines = codebox(codeLines,False)
except:
pass
outLines.extend(codeLines)
codeLines = None
line = maCodeEnd.group(2)
else:
codeLines.append(line)
continue
line = reCode.sub(subCode,line)
maCodeStart = reCodeStart.match(line)
if maCodeStart:
line = maCodeStart.group(1)
codeLines = [maCodeStart.group(2)]
#--Preformatted? -----------------------------
maPreBegin = rePreBegin.search(line)
maPreEnd = rePreEnd.search(line)
if inPre or maPreBegin or maPreEnd:
inPre = maPreBegin or (inPre and not maPreEnd)
outLines.append(line)
continue
#--Font/Background Color
line = reColor.sub(subColor,line)
line = reBGColor.sub(subBGColor,line)
#--Re Matches -------------------------------
maContents = reContentsTag.match(line)
maAnchorHeaders = reAnchorHeadersTag.match(line)
maCss = reCssTag.match(line)
maHead = reHead.match(line)
maList = reList.match(line)
maPar = rePar.match(line)
maEmpty = reEmpty.match(line)
#--Contents
if maContents:
if maContents.group(1):
addContents = int(maContents.group(1))
else:
addContents = 100
inPar = False
elif maAnchorHeaders:
anchorHeaders = maAnchorHeaders.group(1) != '0'
continue
#--CSS
elif maCss:
#--Directory spec is not allowed, so use tail.
cssName = GPath(maCss.group(1).strip()).tail
continue
#--Headers
elif maHead:
lead,text = maHead.group(1,2)
text = re.sub(' *=*#?$','',text.strip())
anchor = reWd.sub('',text)
level = len(lead)
if anchorHeaders:
if re.match(r'\d', anchor):
anchor = '_' + anchor
count = 0
while anchor in anchorlist and count < 10:
count += 1
if count == 1:
anchor = anchor + str(count)
else:
anchor = anchor[:-1] + str(count)
anchorlist.append(anchor)
line = (headFormatNA,headFormat)[anchorHeaders] % (level,anchor,text,level)
if addContents: contents.append((level,anchor,text))
else:
line = headFormatNA % (level,text,level)
#--Title?
if not title and level <= 2: title = text
#--Paragraph
elif maPar and not states['code']:
line = '<p>'+line+'</p>\n'
#--List item
elif maList:
spaces = maList.group(1)
bullet = maList.group(2)
text = maList.group(3)
if bullet == '.': bullet = ' '
elif bullet == '*': bullet = '•'
level = len(spaces)/2 + 1
line = spaces+'<p class="list-'+`level`+'">'+bullet+' '
line = line + text + '</p>\n'
#--Empty line
elif maEmpty:
line = spaces+'<p class="empty"> </p>\n'
#--Misc. Text changes --------------------
line = reHr.sub('<hr>',line)
line = reMDash.sub(' – ',line)
#--Bold/Italic subs
line = reBold.sub(subBold,line)
line = reItalic.sub(subItalic,line)
line = reBoldItalic.sub(subBoldItalic,line)
#--Wtxt Tags
line = reAnchorTag.sub(subAnchor,line)
#--Hyperlinks
line = reLink.sub(subLink,line)
line = reHttp.sub(r' <a href="\1">\1</a>',line)
line = reWww.sub(r' <a href="http://\1">\1</a>',line)
#--Save line ------------------
#print line,
outLines.append(line)
#--Get Css -----------------------------------------------------------
if not cssName:
css = WryeText.defaultCss
else:
if cssName.ext != '.css':
raise "Invalid Css file: "+cssName.s
for dir in cssDirs:
cssPath = GPath(dir).join(cssName)
if cssPath.exists(): break
else:
raise 'Css file not found: '+cssName.s
css = ''.join(cssPath.open().readlines())
if '<' in css:
raise "Non css tag in "+cssPath.s
#--Write Output ------------------------------------------------------
out.write(WryeText.htmlHead % (title,css))
didContents = False
for line in outLines:
if reContentsTag.match(line):
if contents and not didContents:
baseLevel = min([level for (level,name,text) in contents])
for (level,name,text) in contents:
level = level - baseLevel + 1
if level <= addContents:
out.write('<p class="list-%d">• <a href="#%s">%s</a></p>\n' % (level,name,text))
didContents = True
else:
out.write(line)
out.write('</body>\n</html>\n')
#--Close files?
if srcPath:
ins.close()
out.close()