|
| 1 | +# PYLE - A WikiClone in Python |
| 2 | +# Copyright (C) 2004 - 2009 Tony Garnock-Jones <[email protected]> |
| 3 | +# Copyright (C) 2004 - 2009 LShift Ltd <[email protected]> |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation; either version 2 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | + |
| 19 | +# This module contains a basic block-structure parser for Wiki-style |
| 20 | +# documents, similar to the Zope StructuredText in some ways. The |
| 21 | +# routines here are used by the renderer in Utils.py. |
| 22 | + |
| 23 | +from __future__ import nested_scopes |
| 24 | + |
| 25 | +import sys |
| 26 | +import string |
| 27 | +import re |
| 28 | + |
| 29 | +from pprint import pprint |
| 30 | + |
| 31 | +def indentby(amount, lines): |
| 32 | + prefix = ' ' * amount |
| 33 | + return [prefix + line for line in lines] |
| 34 | + |
| 35 | +class Paragraph: |
| 36 | + def __init__(self, indent, line_number, lines): |
| 37 | + self.indent = indent |
| 38 | + self.line = line_number |
| 39 | + self.lines = lines |
| 40 | + |
| 41 | + def indented_lines(self): |
| 42 | + if self.indent > 0: |
| 43 | + return indentby(self.indent, self.lines) |
| 44 | + else: |
| 45 | + return self.lines |
| 46 | + |
| 47 | + def as_string(self): |
| 48 | + return '\n'.join(self.lines) |
| 49 | + |
| 50 | +class Document: |
| 51 | + def __init__(self, para, kids): |
| 52 | + self.paragraph = para |
| 53 | + self.children = kids |
| 54 | + |
| 55 | + def first_child_line(self): |
| 56 | + if self.children: |
| 57 | + return self.children[0].paragraph.line |
| 58 | + else: |
| 59 | + return self.paragraph.line + len(self.paragraph.lines) |
| 60 | + |
| 61 | + def reconstruct_text(self, additional_indent = 0): |
| 62 | + r = [] |
| 63 | + _reconstruct([self], additional_indent, r, |
| 64 | + max(0, self.paragraph.indent), ## might be -1 if root doc |
| 65 | + self.paragraph.line) |
| 66 | + return Paragraph(0, self.paragraph.line, r) |
| 67 | + |
| 68 | + def reconstruct_child_text(self, additional_indent = 0): |
| 69 | + r = [] |
| 70 | + if self.children: |
| 71 | + _reconstruct(self.children, additional_indent, r, |
| 72 | + min([kid.paragraph.indent for kid in self.children]), |
| 73 | + self.first_child_line()) |
| 74 | + return Paragraph(0, self.paragraph.line, r) |
| 75 | + |
| 76 | +def _reconstruct(docs, additional_indent, lines, baseindent, linenum): |
| 77 | + if docs: |
| 78 | + for doc in docs: |
| 79 | + docline = doc.paragraph.line |
| 80 | + if docline > linenum: |
| 81 | + lines.extend([""] * (docline - linenum)) |
| 82 | + linenum = docline |
| 83 | + lines.extend(indentby(doc.paragraph.indent + additional_indent - baseindent, |
| 84 | + doc.paragraph.lines)) |
| 85 | + linenum = _reconstruct(doc.children, |
| 86 | + additional_indent, |
| 87 | + lines, |
| 88 | + baseindent, |
| 89 | + linenum + len(doc.paragraph.lines)) |
| 90 | + return linenum |
| 91 | + |
| 92 | +def parsefile(filename): |
| 93 | + f = open(filename, 'r') |
| 94 | + lines = f.readlines() |
| 95 | + f.close() |
| 96 | + return parselines(lines) |
| 97 | + |
| 98 | +def parsestring(s): |
| 99 | + lines = string.split(s, '\n') |
| 100 | + return parselines(lines) |
| 101 | + |
| 102 | +_ws_prefix_re = re.compile('( *)(.*)') |
| 103 | +def parselines(lines): |
| 104 | + def collect_paras(lines): |
| 105 | + paras = [] |
| 106 | + line_number = 1 |
| 107 | + lineacc = [] |
| 108 | + previndent = None |
| 109 | + |
| 110 | + for line in lines: |
| 111 | + line = string.expandtabs(string.rstrip(line, '\r\n')) |
| 112 | + (prefix, line) = _ws_prefix_re.match(line).groups() |
| 113 | + indent = len(prefix) |
| 114 | + if previndent is None: previndent = indent |
| 115 | + |
| 116 | + if previndent != indent or not line: |
| 117 | + if lineacc: paras.append(Paragraph(previndent, prevline_number, lineacc)) |
| 118 | + previndent = indent |
| 119 | + lineacc = [] |
| 120 | + |
| 121 | + if line: |
| 122 | + if not lineacc: prevline_number = line_number |
| 123 | + lineacc.append(line) |
| 124 | + |
| 125 | + line_number = line_number + 1 |
| 126 | + |
| 127 | + if lineacc: |
| 128 | + paras.append(Paragraph(previndent, prevline_number, lineacc)) |
| 129 | + |
| 130 | + return paras |
| 131 | + |
| 132 | + def group_paras(paras): |
| 133 | + stack = [Document(Paragraph(-1, 0, []), [])] |
| 134 | + for para in paras: |
| 135 | + while para.indent <= stack[-1].paragraph.indent: |
| 136 | + doc = stack.pop() |
| 137 | + stack[-1].children.append(doc) |
| 138 | + stack.append(Document(para, [])) |
| 139 | + while len(stack) > 1: |
| 140 | + doc = stack.pop() |
| 141 | + stack[-1].children.append(doc) |
| 142 | + return stack.pop() |
| 143 | + |
| 144 | + paras = collect_paras(lines) |
| 145 | + return group_paras(paras) |
| 146 | + |
| 147 | +def doc_to_tuple(d): |
| 148 | + return (d.paragraph.indent, d.paragraph.line, d.paragraph.lines, map(doc_to_tuple, d.children)) |
| 149 | + |
| 150 | +# Interface BlockRenderer: |
| 151 | +# def begin_list(self, is_ordered) |
| 152 | +# def begin_listitem(self, para) |
| 153 | +# def end_listitem(self) |
| 154 | +# def end_list(self, is_ordered) |
| 155 | +# def visit_section(self, rank, titleline, doc) |
| 156 | +# def visit_separator(self) |
| 157 | +# def visit_sublanguage(self, commandline, doc) |
| 158 | +# def visit_normal(self, para) |
| 159 | + |
| 160 | +class BasicWikiMarkup: |
| 161 | + def __init__(self, visitor): |
| 162 | + self.visitor = visitor |
| 163 | + |
| 164 | + def goto_state(self, oldstate, newstate): |
| 165 | + if newstate != oldstate: |
| 166 | + if oldstate == 'ordered': self.visitor.end_list(1) |
| 167 | + elif oldstate == 'unordered': self.visitor.end_list(0) |
| 168 | + elif oldstate is None: pass |
| 169 | + else: raise 'Illegal BasicWikiMarkup oldstate', oldstate |
| 170 | + |
| 171 | + if newstate == 'ordered': self.visitor.begin_list(1) |
| 172 | + elif newstate == 'unordered': self.visitor.begin_list(0) |
| 173 | + elif newstate is None: pass |
| 174 | + else: raise 'Illegal BasicWikiMarkup newstate', newstate |
| 175 | + return newstate |
| 176 | + |
| 177 | + def visit(self, docs): |
| 178 | + self._visitkids(docs) |
| 179 | + |
| 180 | + def _visitkids(self, children): |
| 181 | + state = None |
| 182 | + for kid in children: |
| 183 | + para = kid.paragraph |
| 184 | + lines = para.lines |
| 185 | + firstline = lines[0] |
| 186 | + |
| 187 | + if para.indent == 0 and firstline.startswith('*'): |
| 188 | + numstars = 1 |
| 189 | + while numstars < len(firstline) and firstline[numstars] == '*': |
| 190 | + numstars = numstars + 1 |
| 191 | + state = self.goto_state(state, None) |
| 192 | + lines[0] = firstline[numstars:].strip() |
| 193 | + self.visitor.visit_section(numstars, para, kid) |
| 194 | + self._visitkids(kid.children) |
| 195 | + continue |
| 196 | + |
| 197 | + firstchar = firstline[0] |
| 198 | + if firstchar == '-': |
| 199 | + if len(lines) == 1 and \ |
| 200 | + string.count(firstline, '-') == len(firstline) and \ |
| 201 | + len(firstline) >= 4: |
| 202 | + self.visitor.visit_separator() |
| 203 | + else: |
| 204 | + state = self.goto_state(state, 'unordered') |
| 205 | + self.visit_list_item('-', kid) |
| 206 | + elif firstchar == '#': |
| 207 | + state = self.goto_state(state, 'ordered') |
| 208 | + self.visit_list_item('#', kid) |
| 209 | + elif firstchar == '@': |
| 210 | + state = self.goto_state(state, None) |
| 211 | + self.visitor.visit_sublanguage(firstline[1:].strip(), kid) |
| 212 | + else: |
| 213 | + state = self.goto_state(state, None) |
| 214 | + self.visitor.visit_normal(kid.paragraph) |
| 215 | + self._visitkids(kid.children) |
| 216 | + state = self.goto_state(state, None) |
| 217 | + return None |
| 218 | + |
| 219 | + # Builds a list item. Has to check for a few cases: |
| 220 | + # |
| 221 | + # -------------------- |
| 222 | + # - this case |
| 223 | + # where the first paragraph runs on in the first child |
| 224 | + # -------------------- |
| 225 | + # - this case |
| 226 | + # |
| 227 | + # where there's a gap to force a split |
| 228 | + # -------------------- |
| 229 | + # - this case |
| 230 | + # - where there are several |
| 231 | + # - items to return |
| 232 | + # -------------------- |
| 233 | + # - this case |
| 234 | + # where the first paragraph runs on |
| 235 | + # directly, without indent (already covered) |
| 236 | + # -------------------- |
| 237 | + # - this case |
| 238 | + # - where there's a nested sublist immediately |
| 239 | + # -------------------- |
| 240 | + def visit_list_item(self, leader, doc): |
| 241 | + para = doc.paragraph |
| 242 | + lines = para.lines |
| 243 | + is_compound = 1 |
| 244 | + for line in lines: |
| 245 | + if not line.startswith(leader): |
| 246 | + is_compound = 0 |
| 247 | + break |
| 248 | + if is_compound: |
| 249 | + for line in lines[:-1]: |
| 250 | + (pos, line) = trim_item_line(line, leader) |
| 251 | + self.visitor.begin_listitem(Paragraph(para.indent + pos, para.line, [line])) |
| 252 | + self.visitor.end_listitem() |
| 253 | + (itempos, itemline) = trim_item_line(lines[-1], leader) |
| 254 | + itemrestlines = [] |
| 255 | + itemlinenum = para.line + len(lines) - 1 |
| 256 | + else: |
| 257 | + (itempos, itemline) = trim_item_line(lines[0], leader) |
| 258 | + itemrestlines = lines[1:] |
| 259 | + itemlinenum = para.line |
| 260 | + if doc.children: |
| 261 | + firstkid = doc.children[0] |
| 262 | + else: |
| 263 | + firstkid = None |
| 264 | + runs_on = firstkid and \ |
| 265 | + is_compound and \ |
| 266 | + firstkid.paragraph.line == para.line + len(lines) and \ |
| 267 | + firstkid.paragraph.indent == para.indent + itempos + 1 and \ |
| 268 | + firstkid.paragraph.lines[0][0] not in '-#' |
| 269 | + itemlines = [itemline] |
| 270 | + itemlines.extend(itemrestlines) |
| 271 | + if runs_on: |
| 272 | + itemlines.extend(firstkid.paragraph.lines) |
| 273 | + remainingkids = doc.children[1:] |
| 274 | + else: |
| 275 | + remainingkids = doc.children |
| 276 | + self.visitor.begin_listitem(Paragraph(para.indent + itempos, itemlinenum, itemlines)) |
| 277 | + self._visitkids(remainingkids) |
| 278 | + self.visitor.end_listitem() |
| 279 | + |
| 280 | +def trim_item_line(line, leader): |
| 281 | + pos = 0 |
| 282 | + while pos < len(line) and line[pos] == leader: |
| 283 | + pos = pos + 1 |
| 284 | + return (pos, line[pos:]) |
| 285 | + |
| 286 | +class TestVisitor: |
| 287 | + def begin_list(self, is_ordered): |
| 288 | + if is_ordered: |
| 289 | + print '<ol>' |
| 290 | + else: |
| 291 | + print '<ul>' |
| 292 | + |
| 293 | + def begin_listitem(self, para): |
| 294 | + print '<li>' |
| 295 | + print para.as_string() |
| 296 | + |
| 297 | + def end_listitem(self): |
| 298 | + print '</li>' |
| 299 | + |
| 300 | + def end_list(self, is_ordered): |
| 301 | + if is_ordered: |
| 302 | + print '</ol>' |
| 303 | + else: |
| 304 | + print '</ul>' |
| 305 | + |
| 306 | + def visit_section(self, rank, titlepara, doc): |
| 307 | + print '<h' + str(rank) + '>' + titlepara.as_string() + '</h' + str(rank) + '>' |
| 308 | + |
| 309 | + def visit_separator(self): |
| 310 | + print '<hr>' |
| 311 | + |
| 312 | + def visit_sublanguage(self, commandline, doc): |
| 313 | + print '<pre>' |
| 314 | + print '@ ' + commandline |
| 315 | + print doc.reconstruct_child_text().as_string() |
| 316 | + print '</pre>' |
| 317 | + |
| 318 | + def visit_normal(self, para): |
| 319 | + print '<p>' + para.as_string() + '</p>' |
| 320 | + |
| 321 | +def _testfile(filename): |
| 322 | + doc = parsefile(filename) |
| 323 | + #pprint(doc_to_tuple(doc)) |
| 324 | + |
| 325 | + #print doc.reconstruct_text().as_string() |
| 326 | + #print '-' * 75 |
| 327 | + |
| 328 | + markup = BasicWikiMarkup(TestVisitor()) |
| 329 | + print 'Content-type: text/html' |
| 330 | + print |
| 331 | + markup.visit(doc.children) |
| 332 | + |
| 333 | +if __name__ == '__main__': |
| 334 | + if len(sys.argv) < 2: |
| 335 | + print 'Missing filename' |
| 336 | + filename = sys.argv[1] |
| 337 | + _testfile(filename) |
0 commit comments