Short description

It would be nice if the line numbers in the output of the highlight parser could become links, so that one can easily copy the link e.g. to irc to point to some specific line.

Patch for 1.9

   1 diff -r a75a71cacf69 MoinMoin/formatter/text_html.py
   2 --- a/MoinMoin/formatter/text_html.py   Sun Mar 07 06:30:28 2010 +0300
   3 +++ b/MoinMoin/formatter/text_html.py   Tue Mar 09 16:42:18 2010 +0300
   4 @@ -189,6 +189,11 @@
   5          self._in_code_line = 0
   6          self._code_area_js = 0
   7          self._code_area_state = ['', 0, -1, -1, 0]
   8 +
   9 +        # code format string. id - code block id, num - line number.
  10 +        # XXX upon changing, also check line numbers hide/show js.
  11 +        self._code_id_format = "%(id)s_%(num)d"
  12 +
  13          self._show_section_numbers = None
  14          self.pagelink_preclosed = False
  15          self._is_included = kw.get('is_included', False)
  16 @@ -817,7 +822,7 @@
  17    while (nlen>0) { res += ' '; nlen-- }
  18    return res+num+add;
  19  }
  20 -function addnumber(did, nstart, nstep) {
  21 +function addnumber(did, nstart, nstep, code_id) {
  22    var c = document.getElementById(did), l = c.firstChild, n = 1;
  23    if (!isnumbered(c))
  24      if (typeof nstart == 'undefined') nstart = 1;
  25 @@ -826,8 +831,12 @@
  26      while (l != null) {
  27        if (l.tagName == 'SPAN') {
  28          var s = document.createElement('SPAN');
  29 +        var a = document.createElement('A');
  30          s.className = 'LineNumber'
  31 -        s.appendChild(document.createTextNode(nformat(n,4,' ')));
  32 +        a.appendChild(document.createTextNode(nformat(n,4,'')));
  33 +        a.href = '#' + code_id + '_' + n
  34 +        s.appendChild(a);
  35 +        s.appendChild(document.createTextNode(' '));
  36          n += nstep;
  37          if (l.childNodes.length)
  38            l.insertBefore(s, l.firstChild)
  39 @@ -847,12 +856,12 @@
  40      }
  41    return false;
  42  }
  43 -function togglenumber(did, nstart, nstep) {
  44 +function togglenumber(did, nstart, nstep, code_id) {
  45    var c = document.getElementById(did);
  46    if (isnumbered(c)) {
  47      remnumber(did);
  48    } else {
  49 -    addnumber(did,nstart,nstep);
  50 +    addnumber(did,nstart,nstep,code_id);
  51    }
  52    return false;
  53  }
  54 @@ -885,7 +894,7 @@
  55              self._in_code_area = 1
  56              self._in_code_line = 0
  57              # id in here no longer used
  58 -            self._code_area_state = [None, show, start, step, start]
  59 +            self._code_area_state = [None, show, start, step, start, code_id]
  60  
  61              if msg:
  62                  attr = {'class': 'codemsg'}
  63 @@ -906,11 +915,11 @@
  64              if self._code_area_state[1] >= 0:
  65                  toggleLineNumbersLink = r'''
  66  <script type="text/javascript">
  67 -document.write('<a href="#" onclick="return togglenumber(\'%s\', %d, %d);" \
  68 +document.write('<a href="#" onclick="return togglenumber(\'%s\', %d, %d, \'%s\');" \
  69                  class="codenumbers">%s<\/a>');
  70  </script>
  71  ''' % (ci, self._code_area_state[2], self._code_area_state[3],
  72 -       _("Toggle line numbers"))
  73 +       self._code_area_state[5], _("Toggle line numbers"), )
  74                  res.append(toggleLineNumbersLink)
  75  
  76              # Open pre - using left to right always!
  77 @@ -936,7 +945,10 @@
  78          if on:
  79              res += '<span class="line">'
  80              if self._code_area_state[1] > 0:
  81 -                res += '<span class="LineNumber">%4d </span>' % (self._code_area_state[4], )
  82 +                res += ('<span class="LineNumber"><a href="#%(fmt)s">%%(num)4d</a> </span><span class="LineAnchor" id="%(fmt)s"></span>' % {'fmt': self._code_id_format, }) % {
  83 +                    'id': self._code_area_state[5],
  84 +                    'num': self._code_area_state[4],
  85 +                    }
  86                  self._code_area_state[4] += self._code_area_state[3]
  87          self._in_code_line = on != 0
  88          return res

Cool! :)

Minor fixes todo:


Added in http://hg.moinmo.in/moin/1.9/rev/b86af2e26465


CategoryFeatureImplemented

MoinMoin: FeatureRequests/HighlighterLineLinks (last edited 2010-03-09 15:11:15 by EugeneSyromyatnikov)