sourcecode.py :  » Content-Management-Systems » PyLucid » PyLucid_standalone » pylucid_project » apps » pylucid » defaulttags » 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 » defaulttags » sourcecode.py
# coding: utf-8

"""
    PyLucid {% sourcecode %} block tag
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Last commit info:
    ~~~~~~~~~
    $LastChangedDate: $
    $Rev: $
    $Author: JensDiemer $

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

from django import template

from pylucid_project.apps.pylucid.markup.hightlighter import make_html


class SourcecodeNode(template.Node):
    def __init__(self, raw_content, source_type):
        self.raw_content = raw_content
        self.source_type = source_type

    def render(self, context):
        """ put head data into pylucid_plugin.head_files.context_middleware """
        source_html = make_html(
            sourcecode=self.raw_content, source_type=self.source_type,
            django_escape=True
        )
        return source_html


def do_sourcecode(parser, token):
    """
    example:
        {% sourcecode ext=".py" %}
        print "This python script is pygmentize"
        {% endsourcecode %}
    
    With ext argument, get_lexer_by_name used.
    Without it, guess_lexer used. 
    """
    args = token.contents
    try:
        source_type = args.rsplit("=", 1)[-1]
    except (ValueError, IndexError):
        source_type = ""

    # assembles the original content
    raw_content = ""
    while parser.tokens:
        token = parser.next_token()
        if token.token_type == template.TOKEN_TEXT:
            raw_content += token.contents
        elif token.token_type == template.TOKEN_VAR:
            raw_content += "{{ %s }}" % token.contents
        elif token.token_type == template.TOKEN_BLOCK:
            if "endsourcecode" in token.contents:
                # put token back on token list so calling code knows why it terminated
                parser.prepend_token(token)
                break

            raw_content += "{%% %s %%}" % token.contents

    parser.delete_first_token()
    return SourcecodeNode(raw_content, source_type)
do_sourcecode.is_safe = True
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.