django_tags.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » pylucid_project » apps » pylucid » markup » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Content Management Systems » PyLucid 
PyLucid » PyLucid_standalone » pylucid_project » apps » pylucid » markup » django_tags.py
# coding: utf-8

"""
    django tag assembler
    ~~~~~~~~~~~~~~~~~~~~

    -cut out django template tags from text.
    -reassembly cut out parts into text.

    Last commit info:
    ~~~~~~~~~~~~~~~~~
    $LastChangedDate$
    $Rev$
    $Author$

    :copyleft: 2009 by the PyLucid team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""

import re
import unittest


CUT_OUT_RE = re.compile(r'''
    (?P<creole_image>
        \{\{
        .+?(\.jpg|\.jpeg|\.gif|\.png) \s*
        (\| \s* .+? \s*)?
        \}\}
    )
    |
    (?P<creole_pre_inline> {{{ .*? }}} )
    |
    (?P<block>
        \{% \s* (?P<pass_block_start>.+?) .*? %\}
        (\n|.)*?
        \{% \s* end(?P=pass_block_start) \s* %\}
    )
    |
    (?P<tag>
        \{% [^\{\}]*? %\}
    )
    |
    (?P<variable>
        \{\{ [^\{\}]*? \}\}
    )
''', re.VERBOSE | re.UNICODE | re.MULTILINE)

# Ignore this re groups:
LEAVE_KEYS = ("creole_image", "creole_pre_inline")

# Cut out this re groups:
CUT_OUT_KEYS = ("block", "tag", "variable")

ALL_KEYS = LEAVE_KEYS + CUT_OUT_KEYS

PLACEHOLDER_CUT_OUT = u"DjangoTagAassembly%i"


class DjangoTagAssembler(object):

    def cut_out(self, text):
        cut_data = []

        def cut(match):
            groups = match.groupdict()

            for key in ALL_KEYS:
                if groups[key] != None:
                    data = groups[key]
                    if key in LEAVE_KEYS:
                        # Don't replace this re match
                        return data
                    cut_out_pos = len(cut_data)
                    cut_data.append(data)
                    return PLACEHOLDER_CUT_OUT % cut_out_pos

        new_text = CUT_OUT_RE.sub(cut, text)
        return new_text, cut_data

    def reassembly(self, text, cut_data):
        for cut_out_pos, data in enumerate(cut_data):
            placeholder = PLACEHOLDER_CUT_OUT % cut_out_pos
            text = text.replace(placeholder, data)
        return text




class TestDjangoTagAssembler(unittest.TestCase):

    test_text = """{% extends "base_generic.html" %}

{% block title %}
The page title: {{ section.title }}
{% endblock %}

<h1>{{ section.title }}</h1>

Don't match {{{ **this** }}} stuff.
Or {{/image.jpg| **that** }} it's from creole markup!

<h2>
  <a href="{{ story.get_absolute_url }}">
    {{ story.headline|upper }}
  </a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>"""

    def setUp(self):
        self.assembler = DjangoTagAssembler()

    def test(self):
        text2, cut_data = self.assembler.cut_out(self.test_text)
#        from pprint import pprint
#        pprint(cut_data)
#        print text2
        self.failUnlessEqual(cut_data, ['{% extends "base_generic.html" %}',
             '{% block title %}\nThe page title: {{ section.title }}\n{% endblock %}',
             '{{ section.title }}',
             '{{ story.get_absolute_url }}',
             '{{ story.headline|upper }}',
             '{{ story.tease|truncatewords:"100" }}'
        ])
        self.failUnlessEqual(text2, """DjangoTagAassembly0

DjangoTagAassembly1

<h1>DjangoTagAassembly2</h1>

Don't match {{{ **this** }}} stuff.
Or {{/image.jpg| **that** }} it's from creole markup!

<h2>
  <a href="DjangoTagAassembly3">
    DjangoTagAassembly4
  </a>
</h2>
<p>DjangoTagAassembly5</p>""")
        text = self.assembler.reassembly(text2, cut_data)
        self.failUnlessEqual(self.test_text, text)

    def test_unicode(self):
        input_text = u" {{ test }} "
        text2, cut_data = self.assembler.cut_out(input_text)
        self.failUnlessEqual(cut_data, [u'{{ test }}'])
        self.failUnlessEqual(text2, u" DjangoTagAassembly0 ")

        text3 = self.assembler.reassembly(text2, cut_data)
        self.failUnlessEqual(text3, input_text)

if __name__ == '__main__':
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.