Modify

Opened 3 years ago

Last modified 3 years ago

#9696 new enhancement

[Patch] Don't force the trailing hashes to be added

Reported by: rjollos Owned by: martin_s
Priority: normal Component: NumberedHeadlinesPlugin
Severity: normal Keywords:
Cc: hasienda Trac Release: 0.12

Description (last modified by rjollos)

As described in t:WikiFormatting#Headings, it is no longer required that trailing = be added (since 0.12.0, as far as I can tell). The same should be true for the hash symbols used by the NumberedHeadlinesPlugin.

I'm investigating whether a quick patch can be produced.

Attachments (2)

t9696-r10976-rev2.patch (2.5 KB) - added by rjollos 3 years ago.
t9696-r10976.patch (1.1 KB) - added by rjollos 3 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by rjollos

  • Description modified (diff)

comment:2 Changed 3 years ago by rjollos

  • Summary changed from Don't force the trailing hashes to be added to [Patch] Don't force the trailing hashes to be added

The patch below is based on formatter.py in 0.12.2. I'll also attach this as a patch file.

I'd propose making a 0.12 branch for the plugin. Let me know if I need to make any changes to the patch. I'll follow up quickly. In the meantime, I'll be using this on my production system and let you know if I find any issues.

  • 0.11/tracnumberedheadlines/plugin.py

     
    5353 
    5454    NUM_HEADLINE = \ 
    5555        r"(?P<nheading>^\s*(?P<nhdepth>#+)\s" \ 
    56         r"(?P<nheadnum>\s*[0-9.]+\s)?.*\s(?P=nhdepth)\s*" \ 
     56        r"(?P<nheadnum>\s*[0-9.]+\s)?.*\s*" \ 
    5757        r"(?P<nhanchor>=%s)?(?:\s|$))" % XML_NAME 
    5858 
    5959    outline_counters = WeakKeyDictionary() 
     
    6969        shorten = False 
    7070        match = match.strip() 
    7171 
     72        hdepth = fullmatch.group('nhdepth') 
    7273        depth = min(len(fullmatch.group('nhdepth')), 6) 
    7374 
    7475        try: 
     
    100101 
    101102        num    = fullmatch.group('nheadnum') or '' 
    102103        anchor = fullmatch.group('nhanchor') or '' 
    103         heading_text = match[depth+1+len(num):-depth-1-len(anchor)].strip() 
    104  
     104        heading_text = match[depth+1+len(num):].strip() 
     105        if heading_text.endswith(hdepth): 
     106            heading_text = heading_text[:-depth] 
     107             
    105108        num = num.strip() 
    106109        if num and num[-1] == '.': 
    107110          num = num[:-1] 

comment:3 Changed 3 years ago by rjollos

  • Cc hasienda added

Also, I have commit access to all of the repository, so if you approve of this change, I can push it to the repository to save you some time.

comment:4 Changed 3 years ago by rjollos

Here is a patch that includes the previous patch and also makes some additional changes to mirror the implementation in formatter.py.

  • A level 7 heading ======= heading ======== isn't recognized by Trac. Previously, the NumberedHeadlinesPlugin would treat a level 7 numbered heading as a level 6 numbered heading. With the change below, it isn't recognized, which I think leads to more predicable behavior for the editor of a wiki page.
  • A regular expression is used to capture the heading text.
  • Renamed variable heading_text to htext.
  • 0.11/tracnumberedheadlines/plugin.py

     
    5252    XML_NAME = r"[\w:](?<!\d)[\w:.-]*?" # See http://www.w3.org/TR/REC-xml/#id  
    5353 
    5454    NUM_HEADLINE = \ 
    55         r"(?P<nheading>^\s*(?P<nhdepth>#+)\s" \ 
    56         r"(?P<nheadnum>\s*[0-9.]+\s)?.*\s(?P=nhdepth)\s*" \ 
    57         r"(?P<nhanchor>=%s)?(?:\s|$))" % XML_NAME 
     55        r"(?P<nheading>^\s*(?P<nhdepth>#{1,6})\s" \ 
     56        r"(?P<nheadnum>\s*[0-9.]+\s)?(?P<nhtext>.*?)" \ 
     57        r"(?P<nhanchor>=%s)?\s*$)" % XML_NAME 
    5858 
    5959    outline_counters = WeakKeyDictionary() 
    6060 
     
    6868    def _parse_heading(self, formatter, match, fullmatch): 
    6969        shorten = False 
    7070        match = match.strip() 
    71  
    72         depth = min(len(fullmatch.group('nhdepth')), 6) 
     71         
     72        hdepth = fullmatch.group('nhdepth') 
     73        depth = len(hdepth) 
    7374 
    7475        try: 
    7576          formatter.close_table() 
     
    100101 
    101102        num    = fullmatch.group('nheadnum') or '' 
    102103        anchor = fullmatch.group('nhanchor') or '' 
    103         heading_text = match[depth+1+len(num):-depth-1-len(anchor)].strip() 
    104  
     104        htext  = fullmatch.group('nhtext').strip() 
     105        if htext.endswith(hdepth): 
     106            htext = htext[:-depth] 
     107             
    105108        num = num.strip() 
    106109        if num and num[-1] == '.': 
    107110          num = num[:-1] 
     
    117120              n = n + 1 
    118121            counters[depth-len(numbers[n:]):] = numbers[n:] 
    119122 
    120         if not heading_text: 
     123        if not htext: 
    121124          return tag() 
    122125 
    123126        heading = format_to_oneliner(formatter.env, formatter.context,  
    124             heading_text, False) 
     127            htext, False) 
    125128 
    126129        if anchor: 
    127130            anchor = anchor[1:] 
     
    144147        while s < len(counters) and counters[s] == 0: 
    145148          s = s + 1 
    146149 
    147         oheading_text = heading_text 
    148         heading_text = '.'.join(map(str, counters[s:]) + [" "]) + heading_text 
     150        oheading_text = htext 
     151        htext = '.'.join(map(str, counters[s:]) + [" "]) + htext 
    149152 
    150153        if self.number_outline: 
    151           oheading_text = heading_text 
     154          oheading_text = htext 
    152155 
    153156        heading = format_to_oneliner(formatter.env, formatter.context,  
    154             heading_text, False) 
     157            htext, False) 
    155158        oheading = format_to_oneliner(formatter.env, formatter.context,  
    156159            oheading_text, False) 

Changed 3 years ago by rjollos

Changed 3 years ago by rjollos

comment:5 Changed 3 years ago by rjollos

Just wanted to checkin and see if the author of the plugin is still around, and if there would be any objections to pushing this patch to the repository.

Add Comment

Modify Ticket

Action
as new .
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.