~ubuntu-branches/ubuntu/oneiric/moin/oneiric

« back to all changes in this revision

Viewing changes to MoinMoin/parser/highlight.py

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-08-11 12:35:34 UTC
  • mfrom: (0.1.19 sid)
  • Revision ID: james.westby@ubuntu.com-20100811123534-q8zu7qrwqul6cvec
Tags: 1.9.3-1ubuntu1
* Merge from Debian unstable (LP: #586518). Based on work by Stefan Ebner.
  Remaining changes:
 - Remove python-xml from Suggests field, the package isn't anymore in
   sys.path.
 - Demote fckeditor from Recommends to Suggests; the code was previously
   embedded in moin, but it was also disabled, so there's no reason 
   for us to pull this in by default currently. Note: fckeditor has a
   number of security problems and so this change probably needs to be
   carried indefinitely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
95
95
            # ... or use the token's name when nothing apropriate
96
96
            # return str(ttype).replace(".", " ")
97
97
 
 
98
    def add_next_line(self, line_parts):
 
99
        fmt = self.formatter
 
100
        self.lineno += 1
 
101
        self.result.append(fmt.code_line(1))
 
102
        self.result.append(fmt.line_anchordef(self.lineno))
 
103
        self.result += line_parts
 
104
        self.result.append(fmt.code_line(0))
 
105
 
98
106
    def format(self, tokensource, outfile):
99
 
        line_ready = False
100
107
        fmt = self.formatter
101
 
        result = self.result
102
108
        self.lineno = self.start_line
103
 
 
104
 
        for lineno in range(1, self.start_line + 1):
105
 
            result.append(fmt.line_anchordef(lineno))
106
 
 
 
109
        line_parts = []
107
110
        for ttype, value in tokensource:
108
111
            class_ = self.get_class(ttype)
109
112
            if value:
110
113
                for line in self.line_re.split(value):
111
 
                    if not line_ready:
112
 
                        self.lineno += 1
113
 
                        result.append(fmt.code_line(1))
114
 
                        result.append(fmt.line_anchordef(self.lineno))
115
 
                        line_ready = True
116
114
                    if line == '\n':
117
 
                        result.append(fmt.code_line(0))
118
 
                        line_ready = False
119
 
                    else:
120
 
                        if class_:
121
 
                            result.append(fmt.code_token(1, class_))
122
 
                        result.append(fmt.text(line))
123
 
                        if class_:
124
 
                            result.append(fmt.code_token(0, class_))
125
 
        result[-2] = ''
126
 
        result[-1] = ''
127
 
#        if line_ready:
128
 
#            result.append(fmt.code_line(0))
 
115
                        self.add_next_line(line_parts)
 
116
                        line_parts = []
 
117
                        continue
 
118
                    if class_:
 
119
                        line_parts.append(fmt.code_token(1, class_))
 
120
                    line_parts.append(fmt.text(line))
 
121
                    if class_:
 
122
                        line_parts.append(fmt.code_token(0, class_))
 
123
        if line_parts and line_parts != [u'']: # Don't output an empty line at the end.
 
124
            self.add_next_line(line_parts)
129
125
 
130
126
 
131
127
class Parser:
135
131
 
136
132
    def __init__(self, raw, request, filename=None, format_args='', **kw):
137
133
        self.request = request
138
 
        self.raw = raw.strip('\n')
 
134
        self.raw = raw
139
135
        self.filename = filename
140
136
        self.start_line = kw.get('start_line', 0)
141
137
 
159
155
    def format(self, formatter):
160
156
        _ = self.request.getText
161
157
        fmt = PygmentsFormatter(formatter, start_line=self.start_line)
 
158
 
 
159
        # adding line number anchors for process instruction lines
 
160
        for lineno in range(1, self.num_start + 1):
 
161
            fmt.result.append(formatter.line_anchordef(lineno))
 
162
 
162
163
        fmt.result.append(formatter.div(1, css_class="highlight %s" % self.syntax))
163
164
        self._code_id = hash_new('sha1', self.raw.encode(config.charset)).hexdigest()
164
165
        msg = None
181
182
                                                                                                               "highlight_help_page": url
182
183
                                                                                                              }
183
184
                lexer = pygments.lexers.TextLexer()
 
185
 
184
186
        fmt.result.append(formatter.code_area(1, self._code_id, self.parsername, self.show_nums, self.num_start, self.num_step, msg))
185
187
        pygments.highlight(self.raw, lexer, fmt)
186
188
        fmt.result.append(formatter.code_area(0, self._code_id))