Browse Source

Added reference syntactic sugar

main
Lakshmi Vyasarajan 14 years ago
parent
commit
7588609d83
5 changed files with 119 additions and 21 deletions
  1. +41
    -3
      hyde/ext/plugins/markings.py
  2. +6
    -7
      hyde/ext/plugins/texty.py
  3. +5
    -0
      hyde/ext/templates/jinja.py
  4. +11
    -2
      hyde/generator.py
  5. +56
    -9
      hyde/tests/ext/test_markings.py

+ 41
- 3
hyde/ext/plugins/markings.py View File

@@ -42,6 +42,44 @@ class MarkingsPlugin(TextyPlugin):
with
{% endmark %} or equivalent
"""
text = super(MarkingsPlugin, self).text_to_tag(match, start)
print text
return text
return super(MarkingsPlugin, self).text_to_tag(match, start)


class ReferencePlugin(TextyPlugin):
"""
The plugin class for reference text replacement.
"""
def __init__(self, site):
super(ReferencePlugin, self).__init__(site)

@property
def tag_name(self):
"""
The refer tag.
"""
return 'refer to'

@property
def default_open_pattern(self):
"""
The default pattern for mark open text.
"""
return u'^※\s*([^\s]+)\s*as\s*([A-Za-z0-9_\-]+)\s*$'

@property
def default_close_pattern(self):
"""
No close pattern.
"""
return None

def text_to_tag(self, match, start=True):
"""
Replace open pattern (default: ※ inc.md as inc)
with
{% refer to "inc.md" as inc %} or equivalent.
"""
if not match.lastindex:
return ''
params = '"%s" as %s' % (match.groups(1)[0], match.groups(1)[1])
return self.template.get_open_tag(self.tag_name, params)

+ 6
- 7
hyde/ext/plugins/texty.py View File

@@ -25,7 +25,6 @@ class TextyPlugin(Plugin):

def __init__(self, site):
super(TextyPlugin, self).__init__(site)

self.open_pattern = self.default_open_pattern
self.close_pattern = self.default_close_pattern
self.template = None
@@ -34,8 +33,8 @@ class TextyPlugin(Plugin):
if config and hasattr(config, 'open_pattern'):
self.open_pattern = config.open_pattern

if config and hasattr(config, 'close_pattern'):
self.open_pattern = config.close_pattern
if self.close_pattern and config and hasattr(config, 'close_pattern'):
self.close_pattern = config.close_pattern

@property
def plugin_name(self):
@@ -89,10 +88,10 @@ class TextyPlugin(Plugin):
"""
Replace a text base pattern with a template statement.
"""
text_open = re.compile(self.open_pattern, re.MULTILINE)
text_close = re.compile(self.close_pattern, re.MULTILINE)

text_open = re.compile(self.open_pattern, re.UNICODE|re.MULTILINE)
text = text_open.sub(self.text_to_tag, text)
text = text_close.sub(
if self.close_pattern:
text_close = re.compile(self.close_pattern, re.UNICODE|re.MULTILINE)
text = text_close.sub(
partial(self.text_to_tag, start=False), text)
return text

+ 5
- 0
hyde/ext/templates/jinja.py View File

@@ -5,6 +5,8 @@ Jinja template utilties

from hyde.fs import File, Folder
from hyde.template import HtmlWrap, Template
from hyde.util import getLoggerWithNullHandler

from jinja2 import contextfunction, Environment, FileSystemLoader
from jinja2 import environmentfilter, Markup, Undefined, nodes
from jinja2.ext import Extension
@@ -215,6 +217,9 @@ 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)
(contents,
filename,
date) = super(HydeLoader, self).get_source(


+ 11
- 2
hyde/generator.py View File

@@ -37,12 +37,15 @@ class Generator(object):

def __getattr__(self, method_name):
if hasattr(Plugin, method_name):

def __call_plugins__(*args):
#logger.debug("Calling plugin method [%s]", method_name)
res = None
if self.site.plugins:
for plugin in self.site.plugins:
if hasattr(plugin, method_name):
#logger.debug(
# "\tCalling plugin [%s]",
# plugin.__class__.__name__)
function = getattr(plugin, method_name)
res = function(*args)
if res:
@@ -264,7 +267,13 @@ class Generator(object):
text = self.events.begin_text_resource(resource, text) or text
if resource.uses_template:
logger.debug("Rendering [%s]", resource)
text = self.template.render(text, context)
try:
text = self.template.render(text, context)
except Exception:
logger.error("Error occurred when"
" processing template:[%s]" % resource)
raise

text = self.events.text_resource_complete(
resource, text) or text
target = File(self.site.config.deploy_root_path.child(


+ 56
- 9
hyde/tests/ext/test_markings.py View File

@@ -12,6 +12,16 @@ from pyquery import PyQuery

TEST_SITE = File(__file__).parent.parent.child_folder('_test')

def assert_valid_conversion(html):
assert html
q = PyQuery(html)
assert "is_processable" not in html
assert q("h1")
assert "This is a" in q("h1").text()
assert "heading" in q("h1").text()
assert q(".amp").length == 1
assert "mark" not in html
assert "reference" not in html

class TestMarkings(object):

@@ -23,7 +33,9 @@ class TestMarkings(object):
def tearDown(self):
TEST_SITE.delete()

def test_markings(self):


def test_mark(self):
text = u"""
===
is_processable: False
@@ -57,13 +69,48 @@ Hyde & Jinja.
site.load()
gen = Generator(site)
gen.load_template_if_needed()

template = gen.template
html = template.render(text2, {}).strip()
assert html
q = PyQuery(html)
assert "is_processable" not in html
assert "This is a" in q("h1").text()
assert "heading" in q("h1").text()
assert q(".amp").length == 1
assert "mark" not in html
assert "reference" not in html
assert_valid_conversion(html)

def test_reference(self):
text = u"""
===
is_processable: False
===
{% filter markdown|typogrify %}
§§ heading
This is a heading
=================
§§ heading.

§§ content
Hyde & Jinja.
§§ .

{% endfilter %}
"""

text2 = u"""
※ inc.md as inc
{% filter markdown|typogrify %}
{{ inc.heading }}
{{ inc.content }}
{% endfilter %}
"""
site = Site(TEST_SITE)
site.config.plugins = [
'hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.markings.MarkingsPlugin',
'hyde.ext.plugins.markings.ReferencePlugin']
inc = File(site.content.source_folder.child('inc.md'))
inc.write(text.strip())
src = File(site.content.source_folder.child('src.html'))
src.write(text2.strip())
gen = Generator(site)
gen.generate_all()
f = File(site.config.deploy_root_path.child(src.name))
assert f.exists
html = f.read_all()
assert_valid_conversion(html)

Loading…
Cancel
Save