From ef2c5ababb4b13db47c308295f666a0293d0d0d1 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Sat, 22 Jan 2011 19:58:54 +0530 Subject: [PATCH] Added code fencing syntax --- hyde/ext/plugins/blockdown.py | 2 +- hyde/ext/plugins/syntext.py | 45 +++++++++++++++++++++++++++++++ hyde/ext/plugins/texty.py | 4 +-- hyde/ext/templates/jinja.py | 22 ++++++++------- hyde/tests/ext/test_syntext.py | 49 ++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 hyde/ext/plugins/syntext.py create mode 100644 hyde/tests/ext/test_syntext.py diff --git a/hyde/ext/plugins/blockdown.py b/hyde/ext/plugins/blockdown.py index f94946b..12c71f1 100644 --- a/hyde/ext/plugins/blockdown.py +++ b/hyde/ext/plugins/blockdown.py @@ -40,6 +40,6 @@ class BlockdownPlugin(TextyPlugin): {% block blockname %} or equivalent and Replace close pattern (default===[====]/[blockname][===========]) with - {% block blockname %} or equivalent + {% endblock blockname %} or equivalent """ return super(BlockdownPlugin, self).text_to_tag(match, start) diff --git a/hyde/ext/plugins/syntext.py b/hyde/ext/plugins/syntext.py new file mode 100644 index 0000000..f4c3c0a --- /dev/null +++ b/hyde/ext/plugins/syntext.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +""" +Syntext plugin +""" + +from hyde.ext.plugins.texty import TextyPlugin + +class SyntextPlugin(TextyPlugin): + """ + The plugin class for syntax text replacement. + """ + def __init__(self, site): + super(SyntextPlugin, self).__init__(site) + + @property + def tag_name(self): + """ + The syntax tag. + """ + return 'syntax' + + @property + def default_open_pattern(self): + """ + The default pattern for block open text. + """ + return '^~~~+\s*([A-Za-z0-9_\-\.]+)\s*~*\s*$' + + @property + def default_close_pattern(self): + """ + The default pattern for block close text. + """ + return '^\s*~~~+\s*$' + + def text_to_tag(self, match, start=True): + """ + Replace open pattern (default:~~~~~css~~~~~~) + with + {% syntax css %} or equivalent and + Replace close pattern (default: ~~~~~~) + with + {% endsyntax %} or equivalent + """ + return super(SyntextPlugin, self).text_to_tag(match, start) diff --git a/hyde/ext/plugins/texty.py b/hyde/ext/plugins/texty.py index fdbd6d4..f8e679f 100644 --- a/hyde/ext/plugins/texty.py +++ b/hyde/ext/plugins/texty.py @@ -77,9 +77,7 @@ class TextyPlugin(Plugin): Replaces the matched text with tag statement given by the template. """ - if not match.lastindex: - return '' - params = match.groups(1)[0] + params = match.groups(1)[0] if match.lastindex else '' return (self.template.get_open_tag(self.tag_name, params) if start else self.template.get_close_tag(self.tag_name, params)) diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index 22e5076..c8649d1 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -12,6 +12,7 @@ from jinja2 import environmentfilter, Markup, Undefined, nodes from jinja2.ext import Extension from jinja2.exceptions import TemplateError +logger = getLoggerWithNullHandler('Jinja2') class SilentUndefined(Undefined): """ @@ -70,23 +71,22 @@ def syntax(env, value, lexer=None): try: import pygments from pygments import lexers - self.lexers = lexers from pygments import formatters except ImportError: logger.error(u"pygments library is required to use syntax highlighting tags.") raise TemplateError("Cannot load pygments") - pyg = (self.lexers.get_lexer_by_name(lexer) + pyg = (lexers.get_lexer_by_name(lexer) if lexer else - self.lexers.guess_lexer(value)) + lexers.guess_lexer(value)) settings = {} if hasattr(env.config, 'syntax'): settings = getattr(env.config.syntax, 'options', {}) formatter = formatters.HtmlFormatter(**settings) - code = pygments.highlight(value, lexer, formatter) + code = pygments.highlight(value, pyg, formatter) code = code.replace('\n\n', '\n \n').replace('\n', '
') - return safestring.mark_safe('\n\n
%s
\n\n' % code) + return Markup('\n\n
%s
\n\n' % code) class Markdown(Extension): """ @@ -125,11 +125,14 @@ class Syntax(Extension): Parses the statements and defers to the callback for pygments processing. """ lineno = parser.stream.next().lineno - lex = parser.stream.next_if('name') + lex = None + if parser.stream.current.type != 'block_end': + lex = parser.stream.next().value + body = parser.parse_statements(['name:endsyntax'], drop_needle=True) return nodes.CallBlock( - self.call_method('_render_syntax', nodes.Const(lex)), + self.call_method('_render_syntax', args=[nodes.Const(lex)]), [], [], body).set_lineno(lineno) def _render_syntax(self, lex, caller=None): @@ -273,9 +276,8 @@ class HydeLoader(FileSystemLoader): """ Calls the plugins to preprocess prior to returning the source. """ - print "Loading Template %s" % template - logger = getLoggerWithNullHandler('HydeLoader') - logger.debug("Loading template [%s] and preprocessing" % template) + hlogger = getLoggerWithNullHandler('HydeLoader') + hlogger.debug("Loading template [%s] and preprocessing" % template) (contents, filename, date) = super(HydeLoader, self).get_source( diff --git a/hyde/tests/ext/test_syntext.py b/hyde/tests/ext/test_syntext.py new file mode 100644 index 0000000..0db61d9 --- /dev/null +++ b/hyde/tests/ext/test_syntext.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +Use nose +`$ pip install nose` +`$ nosetests` +""" +from hyde.fs import File, Folder +from hyde.generator import Generator +from hyde.site import Site + +from pyquery import PyQuery + +TEST_SITE = File(__file__).parent.parent.child_folder('_test') + + +class TestSyntext(object): + + def setUp(self): + TEST_SITE.make() + TEST_SITE.parent.child_folder( + 'sites/test_jinja').copy_contents_to(TEST_SITE) + + def tearDown(self): + TEST_SITE.delete() + + + + def test_mark(self): + text = u""" +~~~~~~~~css~~~~~~~ +.body{ + background-color: white; +} +~~~~~~~~~~~~~~~~~~ +""" + site = Site(TEST_SITE) + site.config.plugins = [ + 'hyde.ext.plugins.meta.MetaPlugin', + 'hyde.ext.plugins.syntext.SyntextPlugin'] + syn = File(site.content.source_folder.child('syn.html')) + syn.write(text) + gen = Generator(site) + gen.generate_all() + f = File(site.config.deploy_root_path.child(syn.name)) + assert f.exists + html = f.read_all() + assert html + q = PyQuery(html) + assert q('div.code').length == 1 \ No newline at end of file