Changeset 1500

Show
Ignore:
Timestamp:
03/24/08 12:48:32 (8 months ago)
Author:
JensDiemer
Message:

tinyTextile: Add permalink and anchor to every headline.

Location:
trunk/pylucid
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/system/tinyTextile.py

    r1327 r1500  
    2828__version__ = "$Rev$" 
    2929 
    30 import sys, re 
     30import sys, re, string 
     31 
    3132from PyLucid.tools.utils import escape 
    3233from PyLucid.system.response import SimpleStringIO 
    3334 
    3435 
     36HEADLINE = ( 
     37    '<h%(no)s id="%(anchor)s">' 
     38    '<a title="Link to this section" href="%(link)s#%(anchor)s" class="anchor">' 
     39    '%(txt)s</a>' 
     40    '</h%(no)s>\n' 
     41) 
     42 
     43 
    3544class TinyTextileParser: 
    36     def __init__(self, out_obj, context): 
     45    def __init__(self, out_obj, permalink, context): 
    3746        self.out = out_obj 
     47        self.permalink = permalink 
    3848        self.context = context 
    3949        self.page_msg   = context["page_msg"] 
    4050 
    41         # Regeln für Blockelemente 
     51        # Blockelements 
    4252        self.block_rules = self._compile_rules([ 
    43             [ # <h1>-Überschriften 
    44                 r"\Ah(\d)\. +(.*)(?us)", 
    45                 r"<h\1>\2</h\1>" 
     53            [ # <h1>-Headlines 
     54                r"\Ah(\d)\. (.+)(?usm)", 
     55                self.headline 
    4656            ], 
    4757        ]) 
    4858 
    49         # Regeln für Inlineelemente 
     59        # Inlineelements 
    5060        self.inline_rules = self._compile_rules([ 
    5161            [ # HTML-Escaping 
     
    155165    def escaping(self, matchobj): 
    156166        return escape(matchobj.group(1)) 
     167 
     168    def headline(self, matchobj): 
     169        """ 
     170        Insert a html headline with a anchor. 
     171        """ 
     172        text = matchobj.group(2) 
     173        anchor = "".join( 
     174            [char for char in text if char in string.ascii_letters] 
     175        ) 
     176        result = HEADLINE % { 
     177            "no": matchobj.group(1), 
     178            "txt": text, 
     179            "link": self.permalink, 
     180            "anchor": anchor, 
     181        } 
     182        return result 
    157183 
    158184    def shortcutLink(self, matchobj): 
     
    422448 
    423449 
    424  
  • trunk/pylucid/PyLucid/tools/content_processors.py

    r1464 r1500  
    4646        from PyLucid.system.tinyTextile import TinyTextileParser 
    4747        out_obj = SimpleStringIO() 
    48         markup_parser = TinyTextileParser(out_obj, context) 
     48 
     49        current_page = context["PAGE"] 
     50        permalink = current_page.get_permalink() 
     51 
     52        markup_parser = TinyTextileParser(out_obj, permalink, context) 
    4953        markup_parser.parse(content) 
    5054        content = out_obj.getvalue() 
  • trunk/pylucid/tests/test_tinyTextile.py

    r1494 r1500  
    3636VERBOSE = 2 
    3737 
     38PERMALINK = "/permalink/" 
    3839 
    3940 
     
    112113        self.fake_context = get_fake_context() 
    113114        self.out = SimpleStringIO() 
    114         self.textile = TinyTextileParser(self.out, self.fake_context) 
     115        self.textile = TinyTextileParser(self.out, PERMALINK, self.fake_context) 
    115116 
    116117    def tearDown(self): 
     
    125126        """ 
    126127        txt = txt.splitlines() 
    127         assert txt[0]=="", "First must be empty!"  
     128        assert txt[0]=="", "First must be empty!" 
    128129        txt = txt[1:] # Skip the first line 
    129130 
     
    200201        self.textile.parse(content) 
    201202        self.assertEqual(self.out.getvalue(),out_test) 
    202          
     203 
    203204    def testTextile_text3(self): 
    204205        test_text = self._prepare_text(""" 
     
    233234    def testTextile_headline1(self): 
    234235        content = self._prepare_text(""" 
    235             h1. h-1 headline 
    236  
    237             h2. h-2 headline 
    238         """) 
    239         out_test = self._prepare_text(""" 
    240             <h1>h-1 headline</h1> 
    241             <h2>h-2 headline</h2> 
    242  
     236            h1. headline A 
     237             
     238            Text1 
     239 
     240            h2. headline B $%# 
     241             
     242            Text2 
     243        """) 
     244        out_test = self._prepare_text(""" 
     245            <h1 id="headlineA"><a title="Link to this section" href="/permalink/#headlineA" class="anchor">headline A</a></h1> 
     246             
     247            <p>Text1</p> 
     248            <h2 id="headlineB"><a title="Link to this section" href="/permalink/#headlineB" class="anchor">headline B $%#</a></h2> 
     249             
     250            <p>Text2</p> 
     251         
    243252        """) 
    244253 
     
    380389        self.textile.parse(content) 
    381390        self.assertEqual(self.out.getvalue(),out_test) 
    382          
     391 
    383392    def testTextile_escaping2(self): 
    384393        content = self._prepare_text(""" 
  • trunk/pylucid/tests/__init__.py

    r1499 r1500  
    192192    HREF_RE = re.compile('<a href="(.+?)".*?>(.+?)</a>') 
    193193 
    194     def assertResponse(self, response, must_contain, must_not_contain=()): 
     194    def assertResponse(self, response, must_contain=(), must_not_contain=()): 
    195195        """ 
    196196        Check the content of the response