Description

Some parsers, based on preformatted text generation, miss line numbers in it, which makes some features (like links in diff) unusable.

Steps to reproduce

  1. Make a page with a formatter which generates preformatted text
  2. Check generated HTML page on abscence of line number anchors

Example

http://master19.moinmo.in/4ct10n/diff/MoinI18n/ru?action=diff#line-1313

Component selection

Details

MoinMoin Version

1.9.0 and before

OS and Version

Python Version

Server Setup

Server Details

Language you are using the wiki in (set in the browser/UserPreferences)

Workaround

   1 diff -r 63df8c7b82b2 MoinMoin/parser/highlight.py
   2 --- a/MoinMoin/parser/highlight.py      Tue Jan 12 22:27:07 2010 +0100
   3 +++ b/MoinMoin/parser/highlight.py      Thu Jan 14 21:44:13 2010 +0300
   4 @@ -41,10 +41,11 @@
   5      """ a formatter for Pygments that uses the moin formatter for creating output """
   6      line_re = re.compile(r'(\n)')
   7  
   8 -    def __init__(self, formatter):
   9 +    def __init__(self, formatter, **kw):
  10          pygments.formatter.Formatter.__init__(self)
  11          self.result = []
  12          self.formatter = formatter
  13 +        self.start_line = kw.get('start_line', 0)
  14  
  15      def get_class(self, ttype):
  16          if ttype in Token.Text:
  17 @@ -98,12 +99,16 @@
  18          line_ready = False
  19          fmt = self.formatter
  20          result = self.result
  21 +        self.lineno = self.start_line
  22 +
  23          for ttype, value in tokensource:
  24              class_ = self.get_class(ttype)
  25              if value:
  26                  for line in self.line_re.split(value):
  27                      if not line_ready:
  28 +                        self.lineno += 1
  29                          result.append(fmt.code_line(1))
  30 +                        result.append(fmt.line_anchordef(self.lineno))
  31                          line_ready = True
  32                      if line == '\n':
  33                          result.append(fmt.code_line(0))
  34 @@ -129,6 +134,7 @@
  35          self.request = request
  36          self.raw = raw.strip('\n')
  37          self.filename = filename
  38 +        self.start_line = kw.get('start_line', 0)
  39  
  40          if self.parsername == 'highlight':
  41              # user is directly using the highlight parser
  42 @@ -149,7 +155,7 @@
  43  
  44      def format(self, formatter):
  45          _ = self.request.getText
  46 -        fmt = PygmentsFormatter(formatter)
  47 +        fmt = PygmentsFormatter(formatter, start_line=self.start_line)
  48          fmt.result.append(formatter.div(1, css_class="highlight %s" % self.syntax))
  49          self._code_id = hash_new('sha1', self.raw.encode(config.charset)).hexdigest()
  50          msg = None
  51 diff -r 63df8c7b82b2 MoinMoin/parser/text.py
  52 --- a/MoinMoin/parser/text.py   Tue Jan 12 22:27:07 2010 +0100
  53 +++ b/MoinMoin/parser/text.py   Thu Jan 14 21:47:43 2010 +0300
  54 @@ -27,9 +27,22 @@
  55          self.request = request
  56          self.form = request.form
  57          self._ = request.getText
  58 +        self.start_line = kw.get('start_line', 0)
  59  
  60      def format(self, formatter):
  61          """ Send the text. """
  62 +
  63 +        self.lines = self.raw.expandtabs().split('\n')
  64 +        if self.lines[-1] == '':
  65 +            del self.lines[-1]
  66 +
  67 +        self.lineno = self.start_line
  68 +
  69          self.request.write(formatter.preformatted(1))
  70 -        self.request.write(formatter.text(self.raw.expandtabs()))
  71 +
  72 +        for line in self.lines:
  73 +            self.lineno += 1
  74 +            self.request.write(formatter.line_anchordef(self.lineno))
  75 +            self.request.write(formatter.text(line + '\n'))
  76 +
  77          self.request.write(formatter.preformatted(0))

Discussion

Plan


CategoryMoinMoinBugFixed

MoinMoin: MoinMoinBugs/LineNumberAnchorsInPreformattedText (last edited 2010-01-15 15:12:11 by EugeneSyromyatnikov)