annotate MoinMoin/parser/_ParserBase.py @ 4384:09328e4aab45

deprecate ParserBase syntax highlighter
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 12 Oct 2008 00:59:13 +0200
parents 817d99d715fe
children fc47ff90b96f
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
2274
01f6172dabd0 remove tabs from source files, remove excludes from test_tab
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
3 MoinMoin - Base Source Parser
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
5 @copyright: 2002 by Taesu Pyo <bigflood@hitel.net>,
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
6 2005 by Oliver Graf <ograf@bitart.de>,
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
7 2005-2008 MoinMoin:ThomasWaldmann
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
8
249
de46a69b9865 changed wrong license to intended one in Taesu Pyo's parser code contribution
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
9 @license: GNU GPL, see COPYING for details.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
10
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
11
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12 basic css:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
13
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
14 pre.codearea { font-style: sans-serif; color: #000000; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
16 pre.codearea span.ID { color: #000000; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
17 pre.codearea span.Char { color: #004080; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
18 pre.codearea span.Comment { color: #808080; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19 pre.codearea span.Number { color: #008080; font-weight: bold; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
20 pre.codearea span.String { color: #004080; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
21 pre.codearea span.SPChar { color: #0000C0; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 pre.codearea span.ResWord { color: #4040ff; font-weight: bold; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
23 pre.codearea span.ConsWord { color: #008080; font-weight: bold; }
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26
4363
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3915
diff changeset
27 import re
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
28
4384
09328e4aab45 deprecate ParserBase syntax highlighter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4363
diff changeset
29 from MoinMoin import log
09328e4aab45 deprecate ParserBase syntax highlighter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4363
diff changeset
30 logging = log.getLogger(__name__)
09328e4aab45 deprecate ParserBase syntax highlighter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4363
diff changeset
31
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
32 from MoinMoin import config, wikiutil
4363
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3915
diff changeset
33 from MoinMoin.support.python_compatibility import hash_new
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
34
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
35 class FormatTextBase:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
36 pass
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
37
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
38 class FormatBeginLine(FormatTextBase):
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
39 def formatString(self, formatter, word):
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
40 return formatter.code_line(1)
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
41
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
42 class FormatEndLine(FormatTextBase):
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
43 def formatString(self, formatter, word):
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
44 return formatter.code_line(0)
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
45
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
46 class FormatText(FormatTextBase):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
47
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
48 def __init__(self, fmt):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
49 self.fmt = fmt
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
50
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
51 def formatString(self, formatter, word):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
52 return (formatter.code_token(1, self.fmt) +
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
53 formatter.text(word) +
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
54 formatter.code_token(0, self.fmt))
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
55
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
56 class FormatTextID(FormatTextBase):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
57
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
58 def __init__(self, fmt, icase=False):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
59 if not isinstance(fmt, FormatText):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
60 fmt = FormatText(fmt)
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
61 self.setDefaultFormat(fmt)
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
62 self._ignore_case = icase
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
63 self.fmt = {}
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
64
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
65 def setDefaultFormat(self, fmt):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
66 self._def_fmt = fmt
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
67
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
68 def addFormat(self, word, fmt):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
69 if self._ignore_case:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
70 word = word.lower()
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
71 self.fmt[word] = fmt
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
72
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
73 def formatString(self, formatter, word):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
74 if self._ignore_case:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
75 sword = word.lower()
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
76 else:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
77 sword = word
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
78 return self.fmt.get(sword, self._def_fmt).formatString(formatter, word)
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
79
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
80
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
81 class FormattingRuleSingle:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
82
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
83 def __init__(self, name, str_re, icase=False):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
84 self.name = name
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
85 self.str_re = str_re
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
86
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
87 def getStartRe(self):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
88 return self.str_re
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
89
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
90 def getText(self, parser, hit):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
91 return hit
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
92
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
93
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
94 class FormattingRulePair:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
95
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
96 def __init__(self, name, str_begin, str_end, icase=False):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
97 self.name = name
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
98 self.str_begin = str_begin
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
99 self.str_end = str_end
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
100 re_flags = re.M
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
101 if icase:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
102 re_flags |= re.I
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
103 self.end_re = re.compile(str_end, re_flags)
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
104
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
105 def getStartRe(self):
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
106 return self.str_begin
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
107
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
108 def getText(self, parser, hit):
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
109 match = self.end_re.search(parser.text, parser.lastpos)
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
110 if not match:
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
111 next_lastpos = parser.text_len
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
112 else:
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
113 next_lastpos = match.end() + (match.end() == parser.lastpos)
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
114 r = parser.text[parser.lastpos:next_lastpos]
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
115 parser.lastpos = next_lastpos
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
116 return hit + r
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
117
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
118
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
119 # ------------------------------------------------------------------------
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
120
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
121 def parse_start_step(request, args):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
122 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
123 Parses common Colorizer parameters start, step, numbers.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
124 Uses L{wikiutil.parseAttributes} and sanitizes the results.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
125
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
126 Start and step must be a non negative number and default to 1,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
127 numbers might be on, off, or none and defaults to on. On or off
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
128 means that numbers are switchable via JavaScript (html formatter),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
129 disabled means that numbers are disabled completely.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
130
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
131 attrdict is returned as last element in the tuple, to enable the
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
132 calling parser to extract further arguments.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
133
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
134 @param request: a request instance
490
ca35d9e6d63e eypdoc comments: fixed some warnings/errors
Thomas Waldmann <tw@waldmann-edv.de>
parents: 249
diff changeset
135 @param args: the argument string
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
136
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
137 @returns: numbers, start, step, attrdict
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
138 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
139 nums, start, step = 1, 1, 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
140 attrs, msg = wikiutil.parseAttributes(request, args)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
141 if not msg:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
142 try:
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
143 start = int(attrs.get('start', '"1"')[1:-1])
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
144 except ValueError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
145 pass
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
146 try:
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
147 step = int(attrs.get('step', '"1"')[1:-1])
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
148 except ValueError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
149 pass
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
150 if attrs.get('numbers', '"on"')[1:-1].lower() in ('off', 'false', 'no'):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
151 nums = 0
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
152 elif attrs.get('numbers', '"on"')[1:-1].lower() in ('none', 'disable'):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
153 nums = -1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
154 return nums, start, step, attrs
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
155
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
156
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
157 class ParserBase:
4384
09328e4aab45 deprecate ParserBase syntax highlighter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4363
diff changeset
158 """ DEPRECATED highlighting parser - please use/extend pygments library """
09328e4aab45 deprecate ParserBase syntax highlighter
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4363
diff changeset
159 logging.warning('Using ParserBase is deprecated - please use/extend pygments syntax highlighting library.')
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
160
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
161 parsername = 'ParserBase'
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
162 tabwidth = 4
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
163
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
164 # for dirty tricks, see comment in format():
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
165 STARTL, STARTL_RE = u"^\n", ur"\^\n"
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
166 ENDL, ENDL_RE = u"\n$", ur"\n\$"
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
167 LINESEP = ENDL + STARTL
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
168
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
169 def __init__(self, raw, request, **kw):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
170 self.raw = raw
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
171 self.request = request
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
172 self.show_nums, self.num_start, self.num_step, attrs = parse_start_step(request, kw.get('format_args', ''))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
173
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
174 self._ignore_case = False
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
175 self._formatting_rules = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
176 self._formatting_rules_n2r = {}
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
177 self._formatting_rule_index = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
178 self.rule_fmt = {}
3912
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
179 #self.line_count = len(raw.split('\n')) + 1
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
180
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
181 def setupRules(self):
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
182 self.addRuleFormat("BEGINLINE", FormatBeginLine())
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
183 self.addRuleFormat("ENDLINE", FormatEndLine())
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
184 # we need a little dirty trick here, see comment in format():
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
185 self.addRule("BEGINLINE", self.STARTL_RE)
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
186 self.addRule("ENDLINE", self.ENDL_RE)
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
187
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
188 self.def_format = FormatText('Default')
3912
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
189 self.reserved_word_format = FormatText('ResWord')
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
190 self.constant_word_format = FormatText('ConsWord')
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
191 self.ID_format = FormatTextID('ID', self._ignore_case)
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
192 self.addRuleFormat("ID", self.ID_format)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
193 self.addRuleFormat("Operator")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
194 self.addRuleFormat("Char")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
195 self.addRuleFormat("Comment")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
196 self.addRuleFormat("Number")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
197 self.addRuleFormat("String")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
198 self.addRuleFormat("SPChar")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
199 self.addRuleFormat("ResWord")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
200 self.addRuleFormat("ResWord2")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
201 self.addRuleFormat("ConsWord")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
202 self.addRuleFormat("Special")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
203 self.addRuleFormat("Preprc")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
204 self.addRuleFormat("Error")
3912
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
205
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
206 def _addRule(self, name, fmt):
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
207 self._formatting_rule_index += 1
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
208 name = "%s_%s" % (name, self._formatting_rule_index) # create unique name
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
209 self._formatting_rules.append((name, fmt))
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
210 self._formatting_rules_n2r[name] = fmt
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
211
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
212 def addRule(self, name, str_re):
3912
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
213 self._addRule(name, FormattingRuleSingle(name, str_re, self._ignore_case))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
214
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
215 def addRulePair(self, name, start_re, end_re):
3912
01ba9b2e96f6 ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3911
diff changeset
216 self._addRule(name, FormattingRulePair(name, start_re, end_re, self._ignore_case))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
217
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
218 def addWords(self, words, fmt):
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
219 if not isinstance(fmt, FormatTextBase):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
220 fmt = FormatText(fmt)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
221 for w in words:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
222 self.ID_format.addFormat(w, fmt)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
223
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
224 def addReserved(self, words):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
225 self.addWords(words, self.reserved_word_format)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
226
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
227 def addConstant(self, words):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
228 self.addWords(words, self.constant_word_format)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
229
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
230 def addRuleFormat(self, name, fmt=None):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
231 if fmt is None:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
232 fmt = FormatText(name)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
233 self.rule_fmt[name] = fmt
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
234
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2274
diff changeset
235 def format(self, formatter, form=None):
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
236 """ Send the text.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
237 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
238
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
239 self.setupRules()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
240
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
241 formatting_regexes = ["(?P<%s>%s)" % (n, f.getStartRe())
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
242 for n, f in self._formatting_rules]
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
243 re_flags = re.M
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
244 if self._ignore_case:
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
245 re_flags |= re.I
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
246 scan_re = re.compile("|".join(formatting_regexes), re_flags)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
247
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
248 self.text = self.raw
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
249
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
250 # dirty little trick to work around re lib's limitations (it can't have
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
251 # zero length matches at line beginning for ^ and at the same time match
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
252 # something else at the beginning of the line):
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
253 self.text = self.LINESEP.join([line.replace('\r', '') for line in self.text.splitlines()])
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
254 self.text = self.STARTL + self.text + self.ENDL
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
255 self.text_len = len(self.text)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
256
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
257 result = [] # collects output
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
258
4363
817d99d715fe remove direct usage of deprecated sha module - use hashlib, if possible
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3915
diff changeset
259 self._code_id = hash_new('sha1', self.raw.encode(config.charset)).hexdigest()
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
260 result.append(formatter.code_area(1, self._code_id, self.parsername, self.show_nums, self.num_start, self.num_step))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
261
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
262 self.lastpos = 0
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
263 match = scan_re.search(self.text)
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
264 while match and self.lastpos < self.text_len:
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
265 # add the rendering of the text left of the match we found
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
266 text = self.text[self.lastpos:match.start()]
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
267 if text:
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
268 result.extend(self.format_normal_text(formatter, text))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
269 self.lastpos = match.end() + (match.end() == self.lastpos)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
270
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
271 # add the rendering of the match we found
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
272 result.extend(self.format_match(formatter, match))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
273
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
274 # search for the next one
3913
ab09415de20b ParserBase: use less confusing names, avoid calculating len(text) repeatedly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3912
diff changeset
275 match = scan_re.search(self.text, self.lastpos)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
276
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
277 # add the rendering of the text right of the last match we found
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
278 text = self.text[self.lastpos:]
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
279 if text:
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
280 result.extend(self.format_normal_text(formatter, text))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
281
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
282 result.append(formatter.code_area(0, self._code_id))
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
283 self.request.write(''.join(result))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
284
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
285 def format_normal_text(self, formatter, text):
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
286 return [formatter.text(text.expandtabs(self.tabwidth))]
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
287
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
288 def format_match(self, formatter, match):
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
289 result = []
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
290 for n, hit in match.groupdict().items():
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
291 if hit is None:
2274
01f6172dabd0 remove tabs from source files, remove excludes from test_tab
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
292 continue
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
293 r = self._formatting_rules_n2r[n]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
294 s = r.getText(self, hit)
1920
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
295 c = self.rule_fmt.get(r.name, None)
b06ef2a53efa 'make pylint', fixed lots of minor stuff found by pylint (and there is still lots left to do)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
296 if not c:
2274
01f6172dabd0 remove tabs from source files, remove excludes from test_tab
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1920
diff changeset
297 c = self.def_format
3915
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
298 if s:
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
299 lines = s.expandtabs(self.tabwidth).split(self.LINESEP)
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
300 for line in lines[:-1]:
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
301 result.append(c.formatString(formatter, line))
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
302 result.append(FormatEndLine().formatString(formatter, ''))
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
303 result.append(FormatBeginLine().formatString(formatter, ''))
b4bdfaaa79b0 ParserBase: do code_line() open and close calls correctly
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3914
diff changeset
304 result.append(c.formatString(formatter, lines[-1]))
3914
bd0aa9e93ed6 ParserBase / text_python parser: only use a single request.write
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3913
diff changeset
305 return result
3911
348c9b6e0033 ParserBase (and other parsers deriving from it): cleanup / refactorings:
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
306