forked from jensl/critic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextformatting.py
152 lines (133 loc) · 5.54 KB
/
textformatting.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
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import configuration
import dbutils
import re
def renderFormatted(db, table, lines, toc=False):
re_h1 = re.compile("^=+$")
re_h2 = re.compile("^-+$")
data = { "configuration.URL": dbutils.getURLPrefix(db) }
blocks = []
block = []
for line in lines:
if line.strip():
block.append(line % data)
elif block:
blocks.append(block)
block = []
else:
if block:
blocks.append(block)
text = None
for block in blocks:
def textToId(text):
return text.lower().replace(' ', '_')
if len(block) == 2:
if re_h1.match(block[1]):
table.setTitle(block[0])
table.tr("h1").td("h1").h1(id=textToId(block[0])).text(block[0])
text = None
if toc:
toc = table.tr("toc").td("toc").div().table("toc")
toc.tr("heading").th().text("Table of contents")
continue
elif re_h2.match(block[1]):
if toc: toc.tr("h2").td().a(href="#" + textToId(block[0])).text(block[0])
table.tr("h2").td("h2").div().h2(id=textToId(block[0])).text(block[0])
text = None
continue
if len(block) == 1 and block[0] == "[repositories]":
text = None
repositories = table.tr("repositories").td("repositories").table("repositories", align="center", cellspacing=0)
headings = repositories.tr("headings")
headings.th("name").text("Short name")
headings.th("path").text("Repository path")
cursor = db.cursor()
cursor.execute("SELECT name, path FROM repositories ORDER BY id ASC")
first = " first"
for name, path in cursor:
row = repositories.tr("repository" + first)
row.td("name").text(name)
row.td("path").text("%s:%s" % (configuration.base.HOSTNAME, path))
first = ""
continue
if not text:
text = table.tr("text").td("text")
def translateConfigLinks(text):
def linkify(match):
item = match.group(1)
return "<a href='config?highlight=%s'>%s</a>" % (item, item)
return re.sub("CONFIG\\(([^)]+)\\)", linkify, text)
def processText(lines):
for index, line in enumerate(lines):
if line.startswith(" http"):
lines[index] = "<a href='%s'>%s</a>" % (line.strip(), line.strip())
return translateConfigLinks("\n".join(lines)).replace("--", "—")
if len(block) > 2 and re_h2.match(block[1]):
if toc: toc.tr("h3").td().a(href="#" + textToId(block[0])).text(block[0])
div = text.div()
div.h3(id=textToId(block[0])).text(block[0])
block = block[2:]
if block[0].startswith("|"):
pre = table.tr("pre").td("pre").div().table("pre").tr().td().preformatted()
pre.text("\n".join([line[2:] for line in block]))
text = None
continue
elif block[0].startswith("* ") or block[0].startswith("1 "):
if block[0].startswith("* "):
items = text.div().ul()
else:
items = text.div().ol()
item = []
for line in block:
if line[:2] != ' ':
if item: items.li().text("\n".join(item), cdata=True)
item = []
else:
assert line[:2] == " "
item.append(line[2:])
if item: items.li().text("\n".join(item), cdata=True)
elif block[0].startswith("? "):
items = text.div().dl()
term = []
definition = None
for line in block:
if line[:2] == '? ':
if definition:
items.dt().text(" ".join(term))
items.dd().text(processText(definition))
definition = None
term = [line[2:]]
elif line[:2] == '= ':
assert term
assert definition is None
definition = [line[2:]]
elif definition is None:
term.append(line[2:])
else:
definition.append(line[2:])
items.dt().text(" ".join(term))
items.dd().text(processText(definition))
elif block[0].startswith(" "):
text_data = translateConfigLinks("\n".join(block))
if block[0].startswith(" <code>"):
className = "example"
else:
className = "hint"
text_data = text_data.replace("--", "—")
text.div().div(className).text(text_data, cdata=True)
else:
text.div().text(processText(block), cdata=True)