Show
Ignore:
Timestamp:
05/13/08 16:26:55 (22 months ago)
Author:
JensDiemer
Message:

add creole markup:

    @copyright: 2007 MoinMoin:RadomirDopieralski (creole 0.5 implementation),
                2007 MoinMoin:ThomasWaldmann (updates)
    @license: GNU GPL, see COPYING for details.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/tools/content_processors.py

    r1508 r1561  
    44""" 
    55    PyLucid content processors 
    6     ~~~~~~~~~ 
     6    ~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     7 
     8    Tools around content: 
    79 
    810    - apply a markup to a content 
    911    - render a django template 
     12    - redirect warnings 
    1013 
    1114    Last commit info: 
     
    1518    $Author: $ 
    1619 
    17     :copyright: 2007 by Jens Diemer 
    18     :license: GNU GPL v2 or above, see LICENSE for more details 
     20    :copyleft: 2007-2008 by Jens Diemer 
     21    :license: GNU GPL v3 or above, see LICENSE for more details 
    1922""" 
    2023 
     
    2730 
    2831from PyLucid.system.response import SimpleStringIO 
    29 from PyLucid.models import MARKUPS 
    3032 
    3133# use the undocumented django function to add the "lucidTag" to the tag library. 
     
    3436add_to_builtins('PyLucid.template_addons') 
    3537 
     38#______________________________________________________________________________ 
     39# MARKUP 
     40 
     41def fallback_markup(content): 
     42    """ 
     43    A simplest markup, build only paragraphs. 
     44    """ 
     45    import re 
     46    content = content.replace("\r\n", "\n").replace("\r","\n") 
     47    blocks = re.split("\n{2,}", content) 
     48    blocks = [line.replace("\n", "<br />\n") for line in blocks] 
     49    content = "<p>" + "</p>\n\n<p>".join(blocks) + "</p>" 
     50    return content 
     51 
     52def apply_tinytextile(content, context): 
     53    """ tinyTextile markup """ 
     54    from PyLucid.system.markups.tinyTextile import TinyTextileParser 
     55    out_obj = SimpleStringIO() 
     56    markup_parser = TinyTextileParser(out_obj, context) 
     57    markup_parser.parse(content) 
     58    return out_obj.getvalue() 
     59 
     60 
     61def apply_textile(content, page_msg): 
     62    """ Original textile markup """ 
     63    try: 
     64        import textile 
     65    except ImportError: 
     66        page_msg( 
     67            "Markup error: The Python textile library isn't installed." 
     68            " Download: http://cheeseshop.python.org/pypi/textile" 
     69        ) 
     70        return fallback_markup(content) 
     71    else: 
     72        return force_unicode(textile.textile( 
     73            smart_str(content), 
     74            encoding=settings.DEFAULT_CHARSET, 
     75            output=settings.DEFAULT_CHARSET 
     76        )) 
     77 
     78def apply_markdown(content, page_msg): 
     79    """ Markdown markup """ 
     80    try: 
     81        import markdown 
     82    except ImportError: 
     83        page_msg( 
     84            "Markup error: The Python markdown library isn't installed." 
     85            " Download: http://sourceforge.net/projects/python-markdown/" 
     86        ) 
     87        return fallback_markup(content) 
     88    else: 
     89        # unicode support only in markdown v1.7 or above. 
     90        # version_info exist only in markdown v1.6.2rc-2 or above. 
     91        if getattr(markdown, "version_info", None) < (1,7): 
     92            return force_unicode(markdown.markdown(smart_str(content))) 
     93        else: 
     94            return markdown.markdown(content) 
     95 
     96 
     97def apply_restructuretext(content, page_msg): 
     98    try: 
     99        from docutils.core import publish_parts 
     100    except ImportError: 
     101        page_msg( 
     102            "Markup error: The Python docutils library isn't installed." 
     103            " Download: http://docutils.sourceforge.net/" 
     104        ) 
     105        return fallback_markup(content) 
     106    else: 
     107        docutils_settings = getattr( 
     108            settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {} 
     109        ) 
     110        parts = publish_parts( 
     111            source=content, writer_name="html4css1", 
     112            settings_overrides=docutils_settings 
     113        ) 
     114        return parts["fragment"] 
     115 
     116 
     117 
     118def apply_creole(content): 
     119    from PyLucid.system.markups.creole2html import Parser, HtmlEmitter 
     120    document = Parser(content).parse() 
     121    return HtmlEmitter(document).emit() 
     122 
    36123 
    37124def apply_markup(content, context, markup_no): 
    38125    """ 
    39     appy to the content the given markup 
    40     The Markups names are from the _install Dump: 
    41         ./PyLucid/db_dump_datadir/PyLucid_markup.py 
     126    Apply to the content the given markup. 
     127    Makrups IDs defined in 
     128        ./PyLucid/models.py 
    42129    """ 
    43130    page_msg = context["page_msg"] 
    44131 
    45132    if markup_no == 2: # textile 
    46         from PyLucid.system.tinyTextile import TinyTextileParser 
    47         out_obj = SimpleStringIO() 
    48         markup_parser = TinyTextileParser(out_obj, context) 
    49         markup_parser.parse(content) 
    50         content = out_obj.getvalue() 
    51     elif markup_no == 3: #Textile (original) 
    52         try: 
    53             import textile 
    54         except ImportError: 
    55             page_msg( 
    56                 "Markup error: The Python textile library isn't installed." 
    57                 " Download: http://cheeseshop.python.org/pypi/textile" 
    58             ) 
    59         else: 
    60             content = force_unicode(textile.textile( 
    61                 smart_str(content), 
    62                 encoding=settings.DEFAULT_CHARSET, 
    63                 output=settings.DEFAULT_CHARSET 
    64             )) 
     133        content = apply_tinytextile(content, context) 
     134    elif markup_no == 3: # Textile (original) 
     135        content = apply_textile(content, page_msg) 
    65136    elif markup_no == 4: # Markdown 
    66         try: 
    67             import markdown 
    68         except ImportError: 
    69             page_msg( 
    70                 "Markup error: The Python markdown library isn't installed." 
    71                 " Download: http://sourceforge.net/projects/python-markdown/" 
    72             ) 
    73         else: 
    74             # unicode support only in markdown v1.7 or above. 
    75             # version_info exist only in markdown v1.6.2rc-2 or above. 
    76             if getattr(markdown, "version_info", None) < (1,7): 
    77                 content = force_unicode(markdown.markdown(smart_str(content))) 
    78             else: 
    79                 content = markdown.markdown(content) 
     137        content = apply_markdown(content, page_msg) 
    80138    elif markup_no == 5: # ReStructuredText 
    81         try: 
    82             from docutils.core import publish_parts 
    83         except ImportError: 
    84             page_msg( 
    85                 "Markup error: The Python docutils library isn't installed." 
    86                 " Download: http://docutils.sourceforge.net/" 
    87             ) 
    88         else: 
    89             docutils_settings = getattr( 
    90                 settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {} 
    91             ) 
    92             parts = publish_parts( 
    93                 source=content, writer_name="html4css1", 
    94                 settings_overrides=docutils_settings 
    95             ) 
    96             content = parts["fragment"] 
     139        content = apply_restructuretext(content, page_msg) 
     140    elif markup_no == 6: # Creole wiki markup 
     141        content = apply_creole(content) 
    97142 
    98143    return mark_safe(content) # turn djngo auto-escaping off 
     144 
     145 
     146#______________________________________________________________________________ 
    99147 
    100148 
     
    109157 
    110158 
    111  
    112  
     159#______________________________________________________________________________ 
    113160 
    114161