Browse Source

Added text links

main
Lakshmi Vyasarajan 14 years ago
parent
commit
005da79c4e
16 changed files with 195 additions and 24 deletions
  1. +0
    -0
      erview.html
  2. +0
    -0
      mmandline.html
  3. +0
    -0
      mplates.html
  4. +0
    -0
      tending.html
  5. +0
    -0
      youts.html
  6. +0
    -0
      dgets.html
  7. +0
    -0
      ugins.html
  8. +0
    -0
      gregators.html
  9. +0
    -0
      porters.html
  10. +0
    -0
      ving
  11. +40
    -0
      hyde/ext/plugins/textlinks.py
  12. +34
    -23
      hyde/ext/templates/jinja.py
  13. +14
    -0
      hyde/template.py
  14. +49
    -0
      hyde/tests/ext/test_sorter.py
  15. +1
    -1
      hyde/tests/ext/test_syntext.py
  16. +57
    -0
      hyde/tests/ext/test_textlinks.py

doc/1. → erview.html View File


doc/2. → mmandline.html View File


doc/3. → mplates.html View File


doc/4. → tending.html View File


doc/4.1. → youts.html View File


doc/4.2. → dgets.html View File


doc/4.3. → ugins.html View File


doc/4.4. → gregators.html View File


doc/4.5. → porters.html View File


doc/5. → ving View File


+ 40
- 0
hyde/ext/plugins/textlinks.py View File

@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""
Textlinks plugin
"""
import re

from hyde.plugin import Plugin

class TextlinksPlugin(Plugin):
"""
The plugin class for syntax text replacement.
"""
def __init__(self, site):
super(TextlinksPlugin, self).__init__(site)

def template_loaded(self, template):
"""
Handles the template loaded event to keep
a reference to the template object.
"""
self.template = template

def begin_text_resource(self, resource, text):
"""
Replace content url pattern [[/abc/def]])
with
{{ content_url('/abc/def') }} or equivalent and
Replace media url pattern [[!!/abc/def]]
with
{{ media_url('/abc/def') }} or equivalent.
"""
content_link = re.compile('\[\[([^\]]*)\]\]', re.UNICODE|re.MULTILINE)
media_link = re.compile('\[\[\!\!([^\]]*)\]\]', re.UNICODE|re.MULTILINE)
def replace_content(match):
return self.template.get_content_url_statement(match.groups(1)[0])
def replace_media(match):
return self.template.get_media_url_statement(match.groups(1)[0])
text = content_link.sub(replace_content, text)
text = media_link.sub(replace_media, text)
return text

+ 34
- 23
hyde/ext/templates/jinja.py View File

@@ -383,42 +383,53 @@ class Jinja2Template(Template):

@property
def patterns(self):
"""
The pattern for matching selected template statements.
"""
return {
"""
The pattern for matching selected template statements.
"""
return {
"block_open": '\s*\{\%\s*block\s*([^\s]+)\s*\%\}',
"block_close": '\s*\{\%\s*endblock\s*([^\s]*)\s*\%\}',
"include": '\s*\{\%\s*include\s*(?:\'|\")(.+?\.[^.]*)(?:\'|\")\s*\%\}',
"extends": '\s*\{\%\s*extends\s*(?:\'|\")(.+?\.[^.]*)(?:\'|\")\s*\%\}'
}
}

def get_include_statement(self, path_to_include):
"""
Returns an include statement for the current template,
given the path to include.
"""
return '{%% include \'%s\' %%}' % path_to_include
"""
Returns an include statement for the current template,
given the path to include.
"""
return '{%% include \'%s\' %%}' % path_to_include

def get_extends_statement(self, path_to_extend):
"""
Returns an extends statement for the current template,
given the path to extend.
"""
return '{%% extends \'%s\' %%}' % path_to_extend
"""
Returns an extends statement for the current template,
given the path to extend.
"""
return '{%% extends \'%s\' %%}' % path_to_extend

def get_open_tag(self, tag, params):
"""
Returns an open tag statement.
"""
return '{%% %s %s -%%}' % (tag, params)
"""
Returns an open tag statement.
"""
return '{%% %s %s -%%}' % (tag, params)

def get_close_tag(self, tag, params):
"""
Returns an open tag statement.
"""
return '{%%- end%s %%}' % tag
"""
Returns an open tag statement.
"""
return '{%%- end%s %%}' % tag

def get_content_url_statement(self, url):
"""
Returns the content url statement.
"""
return '{{ content_url(\'%s\') }}' % url

def get_media_url_statement(self, url):
"""
Returns the media url statement.
"""
return '{{ media_url(\'%s\') }}' % url

def render(self, text, context):
"""


+ 14
- 0
hyde/template.py View File

@@ -128,6 +128,20 @@ class Template(object):
"""
return '{%% end%s %%}' % tag

@abc.abstractmethod
def get_content_url_statement(self, url):
"""
Returns the content url statement.
"""
return '/' + url

@abc.abstractmethod
def get_media_url_statement(self, url):
"""
Returns the media url statement.
"""
return '/media/' + url

@staticmethod
def find_template(site):
"""


+ 49
- 0
hyde/tests/ext/test_sorter.py View File

@@ -136,6 +136,55 @@ class TestSorter(object):
File(p).parent.name]))]
assert pages == expected_sorted

# def test_walk_resources_sorted_with_grouping_one_level(self):
# s = Site(TEST_SITE)
# cfg = """
# plugins:
# - hyde.ext.sorter.SorterPlugin
# sorter:
# multi:
# groups: sections.yaml
# attr:
# - source_file.kind
# - node.name
#
# """
# sections = """
# group_name: section
# groups:
# -
# name: support
# description: All site support pages
# -
# name: media
# description: Media files
# """
# s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
# s.load()
# SorterPlugin(s).begin_site()
#
# assert hasattr(s.content, 'walk_resources_sorted_by_multi')
# expected = ["content/404.html",
# "content/about.html",
# "content/apple-touch-icon.png",
# "content/blog/2010/december/merry-christmas.html",
# "content/crossdomain.xml",
# "content/favicon.ico",
# "content/robots.txt",
# "content/site.css"
# ]
#
# pages = [page.name for page in s.content.walk_resources_sorted_by_multi()]
#
# expected_sorted = [File(page).name
# for page in
# sorted(expected,
# key=lambda p: tuple(
# [File(p).kind,
# File(p).parent.name]))]
# assert pages == expected_sorted


def test_walk_resources_sorted_no_default_is_processable(self):
s = Site(TEST_SITE)
cfg = """


+ 1
- 1
hyde/tests/ext/test_syntext.py View File

@@ -25,7 +25,7 @@ class TestSyntext(object):



def test_mark(self):
def test_syntext(self):
text = u"""
~~~~~~~~css~~~~~~~
.body{


+ 57
- 0
hyde/tests/ext/test_textlinks.py View File

@@ -0,0 +1,57 @@
# -*- 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 TestTextlinks(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_textlinks(self):
d = {
'objects': 'template/variables',
'plugins': 'plugins/metadata',
'sorter': 'plugins/sorter'
}
text = u"""
{%% markdown %%}
* [Rich object model][hyde objects] and
[overridable hierarchical metadata]([[%(plugins)s]]) thats available for use in
templates.
* Configurable [sorting][], filtering and grouping support.

[hyde objects]: [[%(objects)s]]
[sorting]: [[%(sorter)s]]
{%% endmarkdown %%}
"""
site = Site(TEST_SITE)
site.config.plugins = ['hyde.ext.plugins.textlinks.TextlinksPlugin']
site.config.base_url = 'http://example.com/'
tlink = File(site.content.source_folder.child('tlink.html'))
tlink.write(text % d)
gen = Generator(site)
gen.generate_all()
f = File(site.config.deploy_root_path.child(tlink.name))
assert f.exists
html = f.read_all()
assert html
for name, path in d.items():
assert site.config.base_url + path in html

Loading…
Cancel
Save