Edgewall Software

Ticket #2232: oneliner_simplify_blocks.patch

File oneliner_simplify_blocks.patch, 4.3 KB (added by cboos, 7 years ago)

Also simplify blocks in the one liner formatter output. Originally meant for removing the #!comment blocks, but now works on all kind of blocks. A little bit "hackish" though, so tips for improvements are welcomed. Patch on r2403.

  • trac/wiki/tests/wiki-tests.txt

     
    394394<pre class="wiki">Preformatted text. 
    395395</pre> 
    396396------------------------------ 
     397{{{...}}} 
     398============================== 
    397399{{{ 
    398 Preformatted text. 
     400Outer block. 
     401{{{ 
     402Inner block. 
    399403}}} 
     404}}} 
     405------------------------------ 
     406<pre class="wiki">Outer block. 
     407{{{ 
     408Inner block. 
     409}}} 
     410</pre> 
     411------------------------------ 
     412{{{...}}} 
    400413============================== 
     414Block  
    401415{{{ 
    402 #!default 
    403 Preformatted text. 
     416number one 
    404417}}} 
     418and block 
     419{{{ 
     420number two 
     421}}} 
    405422------------------------------ 
    406 <pre class="wiki">Preformatted text. 
     423<p> 
     424Block  
     425</p> 
     426<pre class="wiki">number one 
    407427</pre> 
     428<p> 
     429and block 
     430</p> 
     431<pre class="wiki">number two 
     432</pre> 
    408433------------------------------ 
     434Block {{{...}}} 
     435and block{{{...}}} 
     436============================== 
    409437{{{ 
    410438#!default 
    411439Preformatted text. 
    412440}}} 
     441------------------------------ 
     442<pre class="wiki">Preformatted text. 
     443</pre> 
     444------------------------------ 
     445{{{#!default ...}}} 
    413446============================== 
    414447{{{ 
    415448#!/bin/sh 
     
    420453echo &#34;foo&#34; 
    421454</pre> 
    422455------------------------------ 
    423 {{{ 
    424 #!/bin/sh 
    425 echo "foo" 
    426 }}} 
     456{{{#!/bin/sh ...}}} 
    427457============================== 
    428458{{{ 
    429459#!html 
     
    432462------------------------------ 
    433463<p>Hello World</p> 
    434464------------------------------ 
    435 {{{ 
    436 #!html 
    437 &lt;p&gt;Hello World&lt;/p&gt; 
    438 }}} 
     465{{{#!html ...}}} 
    439466============================== 
    440467{{{ 
    441468#!html 
     
    448475</pre> 
    449476</div> 
    450477------------------------------ 
    451 {{{ 
    452 #!html 
    453 &lt;script&gt;alert("");&lt;/script&gt; 
    454 }}} 
     478{{{#!html ...}}} 
    455479============================== 
    456480{{{ 
    457481#!html 
     
    464488</pre> 
    465489</div> 
    466490------------------------------ 
    467 {{{ 
    468 #!html 
    469 &lt;div onclick="alert(<i>)"&gt;Click me&lt;/div&gt; 
    470 }}}</i> 
     491{{{#!html ...}}} 
    471492============================== 
    472493^superscript^, ,,subscript,,, normal. 
    473494------------------------------ 
     
    536557</p> 
    537558------------------------------ 
    538559Test comment blocks 
    539 {{{ 
    540 #!comment 
    541 This is simply removed from the output 
    542 }}} 
    543560============================== 
    544561Inline [[comment(This should not be seen)]] comment 
    545562------------------------------ 
  • trac/wiki/formatter.py

     
    637637    """ 
    638638    flavor = 'oneliner' 
    639639 
     640    _non_nested_block_re = re.compile(r"(?:^|\n)\{\{\{(?:\n(#![\w+-/]+))?" 
     641                                      r"(?:\n([^{}]|\{(?!\{\{)|\}(?!\}\}))+)+" 
     642                                      r"\}\}\}") 
     643    _final_block_re = re.compile(r"(?:^|\n)\{\|\{(.*?)\}\|\}") 
     644     
    640645    def __init__(self, env, absurls=0, db=None): 
    641646        Formatter.__init__(self, env, None, absurls, db) 
    642647 
    643648    # Override a few formatters to disable some wiki syntax in "oneliner"-mode 
     649    def _inlinecode_formatter(self, match, fullmatch): return match 
    644650    def _list_formatter(self, match, fullmatch): return match 
    645651    def _indent_formatter(self, match, fullmatch): return match 
    646652    def _heading_formatter(self, match, fullmatch): return match 
     
    664670        self.out = out 
    665671        self._open_tags = [] 
    666672 
    667         result = re.sub(self.rules, self.replace, util.escape(text.strip(), False)) 
     673        result = text.strip() 
     674 
     675        # Simplify code blocks 
     676        def simplify(fullmatch): 
     677            processor = fullmatch.group(1) 
     678            if processor == '#!comment': 
     679                return '' 
     680            elif processor: 
     681                return '\n{|{%s ...}|}' % processor 
     682            elif '\n' in fullmatch.group(): 
     683                return '\n{|{...}|}' 
     684            else: 
     685                return '<tt>%s</tt>' % match[3:-3] 
     686 
     687        old = '' 
     688        while old != result: 
     689            old = result 
     690            result = re.sub(self._non_nested_block_re, simplify, old) 
     691        result = re.sub(self._final_block_re, lambda m: '{{{%s}}}' % m.group(1), 
     692                        old) 
     693        result = re.sub(self.rules, self.replace, util.escape(result, False)) 
     694 
    668695        # Close all open 'one line'-tags 
    669696        result += self.close_tag(None) 
     697 
    670698        out.write(result) 
    671699 
    672700