Changeset 1771

Show
Ignore:
Timestamp:
09/22/08 15:40:45 (18 months ago)
Author:
JensDiemer
Message:

add macro function to creole markup

Location:
trunk/pylucid_project
Files:
2 added
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid_project/media/PyLucid/internal_page/page_admin/markup_help_creole.html

    r1769 r1771  
    1515<hr /> 
    1616 
    17 <p>Changes to the original creole specs:<br /> 
     17<h3>Creole Additions/Macros</h3> 
     18<ul> 
     19<li><b>source code:</b><br /> 
     20    You can insert sourcecode: 
     21    Use <strong><tt>&lt;&lt;code ext=<i>.EXT</i>&gt;&gt;...&lt;&lt;/code&gt;&gt;</tt></strong> 
     22    and PyLucid used Pygments (if available) to highlight it.<br />  
     23    '<strong>.EXT</strong>' is the typical <a href="http://pygments.pocoo.org/docs/lexers/">fileextension/alias</a> of the code. 
     24</p> 
     25    <p>example:</p> 
     26<pre> 
     27&lt;&lt;code ext=.css&gt;&gt; 
     28/* CSS Stylesheet */ 
     29.xs {font-family:verdana,arial,helvetica,sans-serif;font-size: x-small} 
     30.m {font-size: medium} 
     31&lt;&lt;/code&gt;&gt; 
     32</pre> 
     33</li> 
     34<li><b>html code:</b><br /> 
     35    You can insert html code. But it will only pass-through without extra 
     36    decoration if all lines starts and ends with a normal html tag! Otherwise the code would be escaped.<br /> 
     37    There exist also a macro to pass everything throught: <strong><tt>&lt;&lt;html&gt;&gt;...&lt;&lt;/html&gt;&gt;</tt></strong> 
     38</li> 
     39</ul> 
     40 
     41<h3>Changes to the original creole specs:</h3> 
    1842<ul> 
    1943<li><b>image tag:</b><br /> 
     
    3256    (every \n would be convertet into &lt;br /&gt;) 
    3357</li> 
    34 <li><b>html code:</b><br /> 
    35     You can insert html code. But every line must start with &lt; and end with &gt;<br /> 
    36     If the line starts or ends with a other character, the complete line would 
    37     be interpreted as text, so every html characters would be escaped. 
    38 </li> 
    3958<li><b>django tags/blocktags:</b><br /> 
    4059    You can insert django tags and blocktags. No extra decoration is not needed. 
  • trunk/pylucid_project/PyLucid/system/markups/creole.py

    r1769 r1771  
    7777    #-------------------------------------------------------------------------- 
    7878 
     79    macro_block = r'''(?P<macro_block> 
     80            \s* << (?P<macro_block_start>\w+) \s* (?P<macro_block_args>.*?) >> 
     81            (?P<macro_block_text>(.|\n)+?) 
     82            <</(?P=macro_block_start)>> \s* 
     83        )''' 
     84         
    7985    macro = r'''(?P<macro> 
    8086            << 
    81             (?P<macro_name> \w+) 
    82             (\( (?P<macro_args> .*?) \))? \s* 
    83             ([|] \s* (?P<macro_text> .+?) \s* )? 
     87            (?P<macro_name> \w+) (?P<macro_args>.*?) 
    8488            >> 
    8589        )''' 
     
    9599    # For the block elements: 
    96100    separator = r'(?P<separator> ^ \s* ---- \s* $ )' # horizontal line 
    97     line = r'(?P<line> ^ \s* $ )' # empty line that separates paragraphs 
     101    line = r'''(?P<line> ^\s*$ )''' # empty line that separates paragraphs 
    98102    head = r'''(?P<head> 
    99103            ^ 
    100104            (?P<head_head>=+) \s* 
    101105            (?P<head_text> .*? ) 
    102             =* \s*$ 
    103         )\n*''' 
     106            =*$ 
     107        )''' 
    104108    text = r'(?P<text> .+ ) (?P<break> (?<!\\)$\n(?!\s*$) )?' 
    105109    list = r'''(?P<list> 
     
    124128    pre_escape = r' ^(?P<indent>\s*) ~ (?P<rest> \}\}\} \s*) $' 
    125129 
    126     # Pass-through all django template blocktags 
    127     passthrough_block = r'''^ \s*(?P<passthrough_block> 
    128             {%.*?%} 
    129             (.|\n)+? 
    130             {%.*?%} 
    131         ) \s*$ 
    132         ''' 
    133     passthrough_line = r'''(?P<passthrough_line> 
     130    # Pass-through all django template blocktags        
     131    pass_block = r'''(?P<pass_block> 
     132            {% \s* (?P<pass_block_start>.+?) \s* (?P<pass_block_args>.*?) \s* %} 
     133            (\n|.)*? 
     134            {% \s* end(?P=pass_block_start) \s* %} 
     135        )''' 
     136         
     137    pass_line = r'''\n(?P<pass_line> 
     138            (\n|\s)* 
    134139            ({%.*?%})| 
    135140            ({{.*?}}) 
    136         )''' 
     141            (\n|\s)* 
     142        )''' 
     143    pass_inline = r'''(?P<pass_inline> 
     144            ({%.*?%})| 
     145            ({{.*?}}) 
     146        )''' 
     147         
    137148    #Pass-through html code lines 
    138149    html = r'''(?P<html> 
    139             ^ \s*<.*?> \s*$ 
     150            ^[ \t]*<[a-zA-Z].*?<(/[a-zA-Z ]+?)>[ \t]*$ 
    140151        )''' 
    141152 
     
    153164            ) \s* 
    154165        ''' % '|'.join([link, macro, image, code]) 
    155          
     166 
    156167    #-------------------------------------------------------------------------- 
    157     blockelements = ( 
    158         "head", "list", "pre", "code", "table", "separator", "macro", 
    159         "passthrough_block", "html" 
    160     ) 
     168#    blockelements = ( 
     169#        "head", "list", "pre", "code", "table", "separator", "macro", 
     170#        "pass_block", "pass_line", "html" 
     171#    ) 
    161172 
    162173class Parser: 
     
    177188    block_re = re.compile( 
    178189        '|'.join([ 
    179             Rules.passthrough_block, 
     190            Rules.pass_block, 
     191            Rules.pass_line, 
     192            Rules.macro_block, 
    180193            Rules.html, 
    181194            Rules.line, Rules.head, Rules.separator, Rules.pre, Rules.list, 
     
    189202            Rules.link, Rules.url, Rules.macro, 
    190203            Rules.code, Rules.image, 
    191             Rules.passthrough_line, 
     204            Rules.pass_inline, 
    192205            Rules.strong, Rules.emph, Rules.linebreak, 
    193206            Rules.escape, Rules.char 
     
    201214        self.cur = self.root        # The most recent document node 
    202215        self.text = None            # The node to add inline characters to 
     216        self.last_text_break = None # Last break node, inserted by _text_repl() 
    203217 
    204218    #-------------------------------------------------------------------------- 
     219 
     220    def cleanup_break(self, old_cur): 
     221        """ 
     222        remove unused end line breaks. 
     223        Should be called before a new block element. 
     224        e.g.: 
     225          <p>line one<br /> 
     226          line two<br />     <--- remove this br-tag 
     227          </p> 
     228        """ 
     229        if self.cur.children: 
     230            last_child = self.cur.children[-1] 
     231            if last_child.kind == "break": 
     232                del(self.cur.children[-1]) 
    205233 
    206234    def _upto(self, node, kinds): 
     
    210238        Start at the node node. 
    211239        """ 
     240        self.cleanup_break(node) # remove unused end line breaks. 
    212241        while node.parent is not None and not node.kind in kinds: 
    213242            node = node.parent 
     243         
    214244        return node 
    215245 
     
    221251    # same method needs several names, because of group names in regexps. 
    222252 
    223     def _passthrough_block_repl(self, groups): 
    224         """ Pass-through all django template blocktags """ 
     253    def _pass_block_repl(self, groups): 
     254        """ Pass-through all django template blocktags """           
    225255        self._upto_block() 
    226         DocNode("passthrough_block", self.root, groups["passthrough_block"]) 
     256        self.cur = self.root 
     257        DocNode("pass_block", self.cur, groups["pass_block"]) 
     258        self.text = None 
     259    _pass_block_start_repl = _pass_block_repl 
     260    _pass_block_end_repl = _pass_block_repl 
     261 
     262    def _pass_line_repl(self, groups): 
     263        """ Pass-through all django tags witch is alone in a code line """ 
     264        self._upto_block() 
     265        self.cur = self.root 
     266        DocNode("pass_line", self.cur, groups["pass_line"]) 
    227267        self.text = None 
    228268         
    229     def _passthrough_line_repl(self, groups): 
    230         """ Pass-through all django tags """ 
    231         DocNode("passthrough_line", self.cur, groups["passthrough_line"]) 
    232         self.text = None 
    233          
     269    def _pass_inline_repl(self, groups): 
     270        """ Pass-through all inline django tags""" 
     271        DocNode("pass_inline", self.cur, groups["pass_inline"]) 
     272        self.text = None 
     273 
    234274    def _html_repl(self, groups): 
    235275        """ Pass-through html code """ 
     
    239279 
    240280    def _text_repl(self, groups): 
    241 #        print "_text_repl()", self.cur.kind, groups.get('break') != None            
     281#        print "_text_repl()", self.cur.kind, groups.get('break') != None 
    242282        if self.cur.kind in ('table', 'table_row', 'bullet_list', 
    243283                                                                'number_list'): 
    244284            self._upto_block() 
    245              
     285 
    246286        if self.cur.kind in ('document', 'section', 'blockquote'): 
    247287            self.cur = DocNode('paragraph', self.cur) 
    248              
    249         self.parse_inline(groups.get('text', '')) 
    250          
     288 
     289        self.parse_inline(groups.get('text', u"")) 
     290 
    251291        if groups.get('break') and self.cur.kind in ('paragraph', 
    252292            'emphasis', 'strong', 'code'): 
    253             DocNode('break', self.cur, '') 
    254              
     293            self.last_text_break = DocNode('break', self.cur, u"") 
     294 
    255295        self.text = None 
    256296    _break_repl = _text_repl 
     
    260300        if not groups.get('escaped_url'): 
    261301            # this url is NOT escaped 
    262             target = groups.get('url_target', '') 
     302            target = groups.get('url_target', u"") 
    263303            node = DocNode('link', self.cur) 
    264304            node.content = target 
     
    268308            # this url is escaped, we render it as text 
    269309            if self.text is None: 
    270                 self.text = DocNode('text', self.cur, u'') 
     310                self.text = DocNode('text', self.cur, u"") 
    271311            self.text.content += groups.get('url_target') 
    272312    _url_target_repl = _url_repl 
     
    276316    def _link_repl(self, groups): 
    277317        """Handle all kinds of links.""" 
    278         target = groups.get('link_target', '') 
    279         text = (groups.get('link_text', '') or '').strip() 
     318        target = groups.get('link_target', u"") 
     319        text = (groups.get('link_text', u"") or u"").strip() 
    280320        parent = self.cur 
    281321        self.cur = DocNode('link', self.cur) 
     
    288328    _link_text_repl = _link_repl 
    289329 
     330    def _add_macro(self, macro_name, macro_args, macro_text=u""): 
     331#        self._upto_block() 
     332        node = DocNode("macro", self.cur, macro_text.strip()) 
     333        node.macro_name = macro_name 
     334        node.macro_args = macro_args.strip() 
     335        self.text = None 
     336 
     337    def _macro_block_repl(self, groups): 
     338        """Handles macros using the placeholder syntax.""" 
     339        #self.debug_groups(groups) 
     340        self._upto_block() 
     341        self.cur = self.root 
     342        self._add_macro( 
     343            macro_name = groups['macro_block_start'], 
     344            macro_text = groups.get('macro_block_text', u""), 
     345            macro_args = groups.get('macro_block_args', u""), 
     346        ) 
     347        self.text = None 
     348    _macro_block_start_repl = _macro_block_repl 
     349    _macro_block_args_repl = _macro_block_repl 
     350    _macro_block_text_repl = _macro_block_repl 
     351 
    290352    def _macro_repl(self, groups): 
    291353        """Handles macros using the placeholder syntax.""" 
    292         name = groups.get('macro_name', '') 
    293         text = (groups.get('macro_text', '') or '').strip() 
    294         node = DocNode('macro', self.cur, name) 
    295         node.args = groups.get('macro_args', '') or '' 
    296         DocNode('text', node, text or name) 
    297         self.text = None 
     354        macro_name = groups.get('macro_name', u"") 
     355        macro_args = groups.get('macro_args', u"") 
     356        self._add_macro(macro_name, macro_args) 
     357        self.text = None 
     358 
     359#        text = (groups.get('macro_text', u"") or u"").strip() 
     360#        node = DocNode('macro', self.cur, name) 
     361#        node.args = groups.get('macro_args', u"") or '' 
     362#        DocNode('text', node, text or name) 
     363#        self.text = None 
    298364    _macro_name_repl = _macro_repl 
    299365    _macro_args_repl = _macro_repl 
    300     _macro_text_repl = _macro_repl 
     366#    _macro_text_repl = _macro_repl 
    301367 
    302368    def _image_repl(self, groups): 
    303369        """Handles images and attachemnts included in the page.""" 
    304         target = groups.get('image_target', '').strip() 
    305         text = (groups.get('image_text', '') or '').strip() 
     370        target = groups.get('image_target', u"").strip() 
     371        text = (groups.get('image_text', u"") or u"").strip() 
    306372        node = DocNode("image", self.cur, target) 
    307373        DocNode('text', node, text or node.content) 
     
    315381 
    316382    def _item_repl(self, groups): 
    317         bullet = groups.get('item_head', u'') 
    318         text = groups.get('item_text', u'') 
     383        bullet = groups.get('item_head', u"") 
     384        text = groups.get('item_text', u"") 
    319385        if bullet[-1] == '#': 
    320386            kind = 'number_list' 
     
    371437                self.text = None 
    372438            else: 
    373                 text = m.group('head').strip('= ')  
     439                text = m.group('head').strip('= ') 
    374440                self.cur = DocNode('table_head', tr) 
    375                 self.text = DocNode('text', self.cur, u'') 
     441                self.text = DocNode('text', self.cur, u"") 
    376442            self.parse_inline(text) 
    377              
     443 
    378444        self.cur = tb 
    379445        self.text = None 
     
    382448        self._upto_block() 
    383449        kind = groups.get('pre_kind', None) 
    384         text = groups.get('pre_text', u'') 
     450        text = groups.get('pre_text', u"") 
    385451        def remove_tilde(m): 
    386452            return m.group('indent') + m.group('rest') 
     
    396462        """ Transfer newline from the original markup into the html code """ 
    397463        self._upto_block() 
    398         DocNode('line', self.cur, "") 
     464        DocNode('line', self.cur, u"") 
    399465 
    400466    def _code_repl(self, groups): 
    401         DocNode('code', self.cur, groups.get('code_text', u'').strip()) 
     467        DocNode('code', self.cur, groups.get('code_text', u"").strip()) 
    402468        self.text = None 
    403469    _code_text_repl = _code_repl 
     
    424490    def _escape_repl(self, groups): 
    425491        if self.text is None: 
    426             self.text = DocNode('text', self.cur, u'') 
    427         self.text.content += groups.get('escaped_char', u'') 
     492            self.text = DocNode('text', self.cur, u"") 
     493        self.text.content += groups.get('escaped_char', u"") 
    428494 
    429495    def _char_repl(self, groups): 
    430496        if self.text is None: 
    431             self.text = DocNode('text', self.cur, u'') 
    432         self.text.content += groups.get('char', u'') 
    433  
    434     #-------------------------------------------------------------------------- 
    435      
    436     def check_break(self): 
    437         """ 
    438         remove unused end line breaks. Should be called on every block element. 
    439         e.g.: 
    440           <p>line one<br /> 
    441           line two<br />     <--- remove this br-tag 
    442           </p> 
    443         """ 
    444         if self.cur.children == []: 
    445             return 
    446         if self.cur.children[-1].kind == "break": 
    447             del(self.cur.children[-1]) 
    448      
     497            self.text = DocNode('text', self.cur, u"") 
     498        self.text.content += groups.get('char', u"") 
     499 
    449500    #-------------------------------------------------------------------------- 
    450501 
     
    455506            if text is not None: 
    456507                #if name != "char": print "%15s: %r" % (name, text) 
    457                 if name in Rules.blockelements: 
    458                     self.check_break() 
     508                #print "%15s: %r" % (name, text) 
    459509                replace = getattr(self, '_%s_repl' % name) 
    460510                replace(groups) 
     
    476526 
    477527    #-------------------------------------------------------------------------- 
    478     def debug(self): 
     528    def debug(self, start_node=None): 
    479529        """ 
    480530        Display the current document tree 
    481531        """ 
    482532        print "_"*80 
    483         print "  document tree:" 
     533                 
     534        if start_node == None: 
     535            start_node = self.root 
     536            print "  document tree:" 
     537        else: 
     538            print "  tree from %s:" % start_node 
     539             
    484540        print "="*80 
    485541        def emit(node, ident=0): 
    486542            for child in node.children: 
    487                 print u"%s%s" % (u" "*ident, child) 
     543                print u"%s%s: %r" % (u" "*ident, child.kind, child.content) 
    488544                emit(child, ident+4) 
    489         emit(self.root) 
     545        emit(start_node) 
    490546        print "*"*80 
    491      
     547 
    492548    def debug_groups(self, groups): 
    493549        print "_"*80 
     
    511567        self.parent = parent 
    512568        self.kind = kind 
     569 
     570        if content: 
     571            content = unicode(content) 
    513572        self.content = content 
     573 
    514574        if self.parent is not None: 
    515575            self.parent.children.append(self) 
     
    517577    def __str__(self): 
    518578#        return "DocNode kind '%s', content: %r" % (self.kind, self.content) 
    519         return "%s: %r" % (self.kind, self.content) 
     579        return "<DocNode %s: %r>" % (self.kind, self.content) 
     580    def __repr__(self): 
     581        return u"<DocNode %s: %r>" % (self.kind, self.content) 
     582 
     583    def debug(self): 
     584        print "_"*80 
     585        print "\tDocNode - debug:" 
     586        print "str(): %s" % self 
     587        print "attributes:" 
     588        for i in dir(self): 
     589            if i.startswith("_"): 
     590                continue 
     591            print "%20s: %r" % (i, getattr(self, i, "---")) 
    520592 
    521593 
     
    549621picture [[www.domain.tld | {{ foo.JPG | Foo }} ]] as a link 
    550622 
    551 END""" 
    552  
     623END 
     624 
     625==== Headline 1 
     626 
     627{% a tag 1 %} 
     628 
     629==== Headline 2 
     630 
     631{% a tag 2 %} 
     632 
     633the end 
     634""" 
     635 
     636    txt = r""" 
     637==== Headline 1 
     638 
     639The current page name: >{{ PAGE.name }}< great? 
     640 
     641{% a tag 1 %} 
     642 
     643==== Headline 2 
     644 
     645{% a tag 2 %} 
     646 
     647some text 
     648 
     649{% something arg1="foo" arg2="bar" arg2=3 %} 
     650foobar 
     651{% endsomething %} 
     652 
     653the end 
     654""" 
     655 
     656    txt = r"""A {% lucidTag page_update_list count=10 %} PyLucid plugin 
     657 
     658{% sourcecode py %} 
     659import sys 
     660 
     661sys.stdout("Hello World!") 
     662{% endsourcecode %} 
     663A [[www.domain.tld|link]].""" 
     664 
     665    txt = r""" 
     666==== Headline 1 
     667 
     668On {% a tag 1 %} line 
     669line two 
     670 
     671==== Headline 2 
     672 
     673{% a tag 2 %} 
     674 
     675A block: 
     676{% block %} 
     677<Foo:> {{ Bar }} 
     678{% endblock %} 
     679end block 
     680 
     681{% block1 arg="jo" %} 
     682eofjwqp 
     683{% endblock1 %} 
     684 
     685A block without the right end block: 
     686{% block1 %} 
     687111 
     688{% endblock2 %} 
     689BBB 
     690 
     691A block without endblock: 
     692{% block3 %} 
     693222 
     694{% block3 %} 
     695CCC 
     696 
     697the end""" 
     698#    txt = r''' 
     699#<<jojo>> 
     700#owrej 
     701#<<code>> 
     702#some code 
     703#<</code>> 
     704#a macro: 
     705#<<code ext=.css>> 
     706#/* Stylesheet */ 
     707#form * { 
     708#  vertical-align:middle; 
     709#} 
     710#<</code>> 
     711#the end 
     712#<<code>> 
     713#<<code>> 
     714#jup 
     715#<</code>> 
     716#''' 
     717 
     718 
     719    print "-"*80 
    553720    p = Parser(txt) 
    554721    document = p.parse() 
    555722    p.debug() 
    556  
     723     
    557724    def test_rules(rules, txt): 
    558725        def display_match(match): 
     
    563730        re.sub(rules, display_match, txt) 
    564731 
    565     print "_"*80 
    566     print "plain block rules match:" 
    567     test_rules(Parser("").block_re, txt) 
    568  
    569     print "_"*80 
    570     print "plain inline rules match:" 
    571     test_rules(Parser("").inline_re, txt) 
     732#    print "_"*80 
     733#    print "plain block rules match:" 
     734#    test_rules(Parser("").block_re, txt) 
     735# 
     736#    print "_"*80 
     737#    print "plain inline rules match:" 
     738#    test_rules(Parser("").inline_re, txt) 
    572739 
    573740    print "---END---" 
  • trunk/pylucid_project/PyLucid/system/markups/creole2html.py

    r1769 r1771  
    4141""" 
    4242 
    43 import re 
     43import sys, re, traceback 
     44 
    4445from creole import Parser 
     46 
     47import macros 
     48 
     49from PyLucid.tools.utils import escape 
    4550 
    4651class Rules: 
     
    5358        ''' 
    5459 
    55 class Macro(object): 
    56 #    def __init__(self): 
    57     def source_emit(self, node): 
    58         print node 
    59  
    60 from PyLucid.tools.utils import escape 
    6160class HtmlEmitter: 
    6261    """ 
     
    7069        ]), re.X | re.U) # for addresses 
    7170 
    72     def __init__(self, root): 
     71    def __init__(self, root, verbose=1, stderr=sys.stderr): 
    7372        self.root = root 
    74         self.macro = Macro() 
     73        self.verbose = verbose 
     74        self.stderr = stderr 
    7575 
    7676    def get_text(self, node): 
     
    188188 
    189189    def macro_emit(self, node): 
    190 #        try: 
    191 #            return getattr(self.macro,  
    192         raise NotImplementedError("Node: %r" % node.content) 
     190        #print node.debug() 
     191        macro_name = node.macro_name 
     192        try: 
     193            macro = getattr(macros, macro_name) 
     194        except AttributeError, e: 
     195            return self.error( 
     196                u"Macro '%s' doesn't exist" % macro_name, 
     197                handle_traceback = True 
     198            ) 
     199         
     200        try: 
     201            result = macro(args=node.macro_args, text=node.content) 
     202        except Exception, err: 
     203            return self.error( 
     204                u"Macro '%s' error: %s" % (macro_name, err), 
     205                handle_traceback = True 
     206            ) 
     207         
     208        if not isinstance(result, unicode): 
     209            msg = u"Macro '%s' doesn't return a unicode string!" % macro_name 
     210            if self.verbose>1: 
     211                msg += " - returns: %r, type %r" % (result, type(result)) 
     212            return self.error(msg) 
     213         
     214        return result 
    193215 
    194216    def break_emit(self, node): 
     
    206228        return u"<pre>\n%s\n</pre>\n" % self.html_escape(node.content) 
    207229 
    208     def passthrough_block_emit(self, node): 
     230    def pass_block_emit(self, node): 
    209231        """ Pass-through all django template blocktags and html code lines """ 
    210232        return node.content + "\n" 
    211     html_emit = passthrough_block_emit 
    212  
    213     def passthrough_line_emit(self, node): 
     233    pass_line_emit = pass_block_emit 
     234    html_emit = pass_block_emit 
     235 
     236    def pass_inline_emit(self, node): 
    214237        """ Pass-through all django template tags """ 
    215238        return node.content 
     
    217240    def default_emit(self, node): 
    218241        """Fallback function for emitting unknown nodes.""" 
    219         raise TypeError 
     242        raise NotImplementedError("Node '%s' unknown" % node.kind) 
    220243 
    221244    def emit_children(self, node): 
     
    233256        return self.emit_node(self.root) 
    234257 
     258    def error(self, text, handle_traceback=False): 
     259        """ 
     260        Error Handling. 
     261        """ 
     262        if self.verbose>1 and handle_traceback: 
     263            self.stderr.write( 
     264                "<pre>%s</pre>" % traceback.format_exc() 
     265            ) 
     266         
     267        if self.verbose>0: 
     268            return u"[Error: %s]" % text 
     269        else: 
     270            # No error output 
     271            return u"" 
     272 
    235273if __name__=="__main__": 
    236274    txt = r"""== a headline 
     
    260298 
    261299END""" 
     300 
     301     
     302    txt = r""" 
     303==== Headline 1 
     304 
     305On {% a tag 1 %} line 
     306line two 
     307 
     308==== Headline 2 
     309 
     310{% a tag 2 %} 
     311 
     312A block: 
     313{% block %} 
     314<Foo:> {{ Bar }} 
     315{% endblock %} 
     316end block 
     317 
     318{% block1 arg="jo" %} 
     319eofjwqp 
     320{% endblock1 %} 
     321 
     322A block without the right end block: 
     323{% block1 %} 
     324111 
     325{% endblock2 %} 
     326BBB 
     327 
     328A block without endblock: 
     329{% block3 %} 
     330222 
     331{% block3 %} 
     332CCC 
     333 
     334the end""" 
    262335 
    263336    print "-"*80 
  • trunk/pylucid_project/tests/markup_creole.py

    r1769 r1771  
    5858        """ 
    5959        document = Parser(txt).parse() 
    60         out_string = HtmlEmitter(document).emit() 
     60        out_string = HtmlEmitter(document, verbose=1).emit() 
    6161        #print ">>>%r<<<" % out_string 
    6262        return out_string 
     
    138138        """) 
    139139             
    140  
    141  
    142140    def test_django1(self): 
    143141        """ 
     
    149147            The current page name: >{{ PAGE.name }}< great? 
    150148            A {% lucidTag page_update_list count=10 %} PyLucid plugin 
    151             {% sourcecode py %} 
    152             import sys 
    153  
    154             sys.stdout("Hello World!") 
    155             {% endsourcecode %} 
     149            {% block %} 
     150            FooBar 
     151            {% endblock %} 
    156152            A [[www.domain.tld|link]]. 
    157153            a {{/image.jpg|My Image}} image 
     
    162158            <p>The current page name: &gt;{{ PAGE.name }}&lt; great?<br /> 
    163159            A {% lucidTag page_update_list count=10 %} PyLucid plugin</p> 
    164             {% sourcecode py %} 
    165             import sys 
    166  
    167             sys.stdout("Hello World!") 
    168             {% endsourcecode %} 
     160            {% block %} 
     161            FooBar 
     162            {% endblock %} 
    169163            <p>A <a href="www.domain.tld">link</a>.<br /> 
    170164            a <img src="/image.jpg" alt="My Image"> image</p> 
     
    172166            <p>no image: {{ foo|bar }}!<br /> 
    173167            picture <a href="www.domain.tld"><img src="foo.JPG" alt="Foo"></a> as a link</p> 
     168        """) 
     169 
     170    def test_django2(self): 
     171        self.assertCreole(r""" 
     172            ==== Headline 1 
     173 
     174            On {% a tag 1 %} line 
     175            line two 
     176             
     177            ==== Headline 2 
     178             
     179            {% a tag 2 %} 
     180             
     181            Right block with a end tag: 
     182            {% block %} 
     183            <Foo:> {{ Bar }} 
     184            {% endblock %} 
     185            end block 
     186             
     187            A block without the right end block: 
     188            {% block1 %} 
     189            not matched 
     190            {% endblock2 %} 
     191            BBB 
     192             
     193            A block without endblock: 
     194            {% noblock3 %} 
     195            not matched 
     196            {% noblock3 %} 
     197            CCC 
     198        """, """ 
     199            <h4>Headline 1</h4> 
     200             
     201            <p>On {% a tag 1 %} line<br /> 
     202            line two</p> 
     203             
     204            <h4>Headline 2</h4> 
     205             
     206            {% a tag 2 %} 
     207             
     208            <p>Right block with a end tag:</p> 
     209            {% block %} 
     210            <Foo:> {{ Bar }} 
     211            {% endblock %} 
     212            <p>end block</p> 
     213             
     214            <p>A block without the right end block:<br /> 
     215            {% block1 %}<br /> 
     216            not matched<br /> 
     217            {% endblock2 %}<br /> 
     218            BBB</p> 
     219             
     220            <p>A block without endblock:<br /> 
     221            {% noblock3 %}<br /> 
     222            not matched<br /> 
     223            {% noblock3 %}<br /> 
     224            CCC</p> 
    174225        """) 
    175226 
     
    477528            This is a normal Text block witch would 
    478529            escape html chars like < and > ;) 
     530             
     531            html code must start and end with a tag: 
    479532            <p>this <strong class="my">html code</strong> line pass-through</p> 
    480             end 
     533            this works. 
     534 
     535            this: 
     536            <p>didn't<br /> 
     537            match</p> 
     538             
     539            <p> 
     540                didn't match 
     541            </p> 
     542             
     543            <p>didn't match,too.< p > 
    481544        """, """ 
    482545            <p>This is a normal Text block witch would<br /> 
    483546            escape html chars like &lt; and &gt; ;)</p> 
     547             
     548            <p>html code must start and end with a tag:</p> 
    484549            <p>this <strong class="my">html code</strong> line pass-through</p> 
    485             <p>end</p> 
     550            <p>this works.</p> 
     551             
     552            <p>this:<br /> 
     553            &lt;p&gt;didn\'t&lt;br /&gt;<br /> 
     554            match&lt;/p&gt;</p> 
     555             
     556            <p>&lt;p&gt;<br /> 
     557                didn\'t match<br /> 
     558            &lt;/p&gt;</p> 
     559             
     560            <p>&lt;p&gt;didn\'t match,too.&lt; p &gt;</p> 
     561        """) 
     562         
     563    def test_macro_html1(self): 
     564        self.assertCreole(r""" 
     565            <<a_not_existing_macro>> 
     566             
     567            <<code>> 
     568            some code 
     569            <</code>> 
     570             
     571            a macro: 
     572            <<code>> 
     573            <<code>> 
     574            the sourcecode 
     575            <</code>> 
     576        """, r""" 
     577            <p>[Error: Macro 'a_not_existing_macro' doesn't exist]</p> 
     578            <fieldset class="pygments_code"> 
     579            <legend class="pygments_code"><small title="no lexer matching the text found">unknown type</small></legend> 
     580            <pre><code>some code</code></pre> 
     581            </fieldset> 
     582            <p>a macro:</p> 
     583            <fieldset class="pygments_code"> 
     584            <legend class="pygments_code"><small title="no lexer matching the text found">unknown type</small></legend> 
     585            <pre><code>&lt;&lt;code&gt;&gt; 
     586            the sourcecode</code></pre> 
     587            </fieldset> 
     588        """) 
     589         
     590    def test_macro_html2(self): 
     591        self.assertCreole(r""" 
     592            html macro: 
     593            <<html>> 
     594            <p><<this is 'html'>></p> 
     595            <</html>> 
     596        """, r""" 
     597            <p>html macro:</p> 
     598            <p><<this is 'html'>></p> 
     599        """) 
     600 
     601    def test_macro_pygments_code(self): 
     602        self.assertCreole(r""" 
     603            a macro: 
     604            <<code ext=.css>> 
     605            /* Stylesheet */ 
     606            form * { 
     607              vertical-align:middle; 
     608            } 
     609            <</code>> 
     610            the end 
     611        """, """ 
     612            <p>a macro:</p> 
     613            <fieldset class="pygments_code"> 
     614            <legend class="pygments_code">CSS</legend><table class="pygmentstable"><tr><td class="linenos"><pre>1 
     615            2 
     616            3 
     617            4</pre></td><td class="code"><div class="pygments"><pre><span class="c">/* Stylesheet */</span> 
     618            <span class="nt">form</span> <span class="o">*</span> <span class="p">{</span> 
     619              <span class="k">vertical-align</span><span class="o">:</span><span class="k">middle</span><span class="p">;</span> 
     620            <span class="p">}</span> 
     621            </pre></div> 
     622            </td></tr></table></fieldset> 
     623            <p>the end</p> 
    486624        """) 
    487625 
  • trunk/pylucid_project/tests/utils/unittest_addons.py

    r1761 r1771  
    4848        try: 
    4949            msg = self.args[0] 
     50             
     51            """ 
     52            Get the right split_string is not easy. There are three kinds: 
     53            u"foo" != "bar" 
     54            u'foo' != "bar" 
     55            u"foo" != 'bar' 
     56            u'foo' != 'bar' 
     57            """ 
     58            msg = msg.lstrip("u") # u"foobar" -> "foobar" 
     59            first_quote = msg[0] 
     60            last_quote = msg[-1] 
     61            split_string = "%s != %s" % (first_quote, last_quote) 
     62            msg = msg.strip(first_quote + last_quote) 
    5063 
    51             # strip ' out 
    52             if msg.startswith("u'"): 
    53                 msg = msg[2:-1] 
    54             else: 
    55                 msg = msg[1:-1] 
    56  
     64#            # strip ' out 
     65#            if msg.startswith("u'"): 
     66#                msg = msg[2:-1] 
     67#            else: 
     68#                msg = msg[1:-1] 
     69# 
     70#            if "' != '" in msg: 
     71#                split_string = "' != '" 
     72#            elif '" != "' in msg: 
     73#                split_string = '" != "' 
     74#            else: 
     75#                msg = self._format_output(msg) 
     76#                return "Unknwon error output: %r" % msg 
     77                 
    5778            try: 
    58                 block1, block2 = msg.split("' != '") 
    59             except ValueError: 
     79                block1, block2 = msg.split(split_string) 
     80            except ValueError, err: 
    6081                msg = self._format_output(msg) 
    6182                return ( 
    62                     "Format error in output\n" 
     83                    "Can't split error output: %r\n" 
    6384                    "Info:\n%s" 
    64                 ) % msg 
     85                ) % (err, msg) 
    6586 
    6687            #~ block1 = block1.rstrip("\\n")