Browse Source

Combine text processing plugins into a text module.

* blockdown
 * markings
 * syntext
 * textlinks
main
Elliott Sales de Andrade 13 years ago
committed by Lakshmi Vyasarajan
parent
commit
6ccff655b8
13 changed files with 229 additions and 241 deletions
  1. +0
    -45
      hyde/ext/plugins/blockdown.py
  2. +0
    -85
      hyde/ext/plugins/markings.py
  3. +0
    -57
      hyde/ext/plugins/syntext.py
  4. +210
    -0
      hyde/ext/plugins/text.py
  5. +0
    -35
      hyde/ext/plugins/textlinks.py
  6. +3
    -3
      hyde/tests/ext/test_auto_extend.py
  7. +2
    -2
      hyde/tests/ext/test_blockdown.py
  8. +4
    -4
      hyde/tests/ext/test_markings.py
  9. +2
    -2
      hyde/tests/ext/test_syntext.py
  10. +2
    -2
      hyde/tests/ext/test_textlinks.py
  11. +1
    -1
      hyde/tests/sites/test_sorter/site.yaml
  12. +2
    -2
      hyde/tests/sites/test_tagger/site.yaml
  13. +3
    -3
      hyde/tests/ssp/site.yaml

+ 0
- 45
hyde/ext/plugins/blockdown.py View File

@@ -1,45 +0,0 @@
# -*- coding: utf-8 -*-
"""
Blockdown plugin
"""

from hyde.plugin import TextyPlugin

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

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

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

@property
def default_close_pattern(self):
"""
The default pattern for block close text.
"""
return '^\s*===+/+\s*=*/*([A-Za-z0-9_\-\.]*)[\s=/]*$'

def text_to_tag(self, match, start=True):
"""
Replace open pattern (default:===[====]blockname[===========])
with
{% block blockname %} or equivalent and
Replace close pattern (default===[====]/[blockname][===========])
with
{% endblock blockname %} or equivalent
"""
return super(BlockdownPlugin, self).text_to_tag(match, start)

+ 0
- 85
hyde/ext/plugins/markings.py View File

@@ -1,85 +0,0 @@
# -*- coding: utf-8 -*-
"""
Markings plugin
"""

from hyde.plugin import TextyPlugin

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

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

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

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

def text_to_tag(self, match, start=True):
"""
Replace open pattern (default:§§ CSS)
with
{% mark CSS %} or equivalent and
Replace close pattern (default: §§ /CSS)
with
{% endmark %} or equivalent
"""
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)

+ 0
- 57
hyde/ext/plugins/syntext.py View File

@@ -1,57 +0,0 @@
# -*- coding: utf-8 -*-
"""
Syntext plugin
"""

from hyde.plugin 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*~~~+\s*([A-Za-z0-9_\-\.:\']+)\s*~*\s*$'

@property
def default_close_pattern(self):
"""
The default pattern for block close text.
"""
return '^\s*~~~+\s*$'


def get_params(self, match, start=True):
"""
~~~css~~~ will return css
~~~css/style.css will return css,style.css
"""
params = super(SyntextPlugin, self).get_params(match, start)
if ':' in params:
(lex, _, filename) = params.rpartition(':')
params = 'lex=\'%(lex)s\',filename=\'%(filename)s\'' % locals()
return params

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)

+ 210
- 0
hyde/ext/plugins/text.py View File

@@ -0,0 +1,210 @@
# -*- coding: utf-8 -*-
"""
Text processing plugins
"""

from hyde.plugin import Plugin,TextyPlugin


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

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

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

@property
def default_close_pattern(self):
"""
The default pattern for block close text.
"""
return '^\s*===+/+\s*=*/*([A-Za-z0-9_\-\.]*)[\s=/]*$'

def text_to_tag(self, match, start=True):
"""
Replace open pattern (default:===[====]blockname[===========])
with
{% block blockname %} or equivalent and
Replace close pattern (default===[====]/[blockname][===========])
with
{% endblock blockname %} or equivalent
"""
return super(BlockdownPlugin, self).text_to_tag(match, start)


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

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

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

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

def text_to_tag(self, match, start=True):
"""
Replace open pattern (default:§§ CSS)
with
{% mark CSS %} or equivalent and
Replace close pattern (default: §§ /CSS)
with
{% endmark %} or equivalent
"""
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)


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*~~~+\s*([A-Za-z0-9_\-\.:\']+)\s*~*\s*$'

@property
def default_close_pattern(self):
"""
The default pattern for block close text.
"""
return '^\s*~~~+\s*$'


def get_params(self, match, start=True):
"""
~~~css~~~ will return css
~~~css/style.css will return css,style.css
"""
params = super(SyntextPlugin, self).get_params(match, start)
if ':' in params:
(lex, _, filename) = params.rpartition(':')
params = 'lex=\'%(lex)s\',filename=\'%(filename)s\'' % locals()
return params

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)


import re

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

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.
"""
if not resource.uses_template:
return text
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


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

@@ -1,35 +0,0 @@
# -*- 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 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.
"""
if not resource.uses_template:
return text
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

+ 3
- 3
hyde/tests/ext/test_auto_extend.py View File

@@ -43,7 +43,7 @@ class TestAutoExtend(object):
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.auto_extend.AutoExtendPlugin',
'hyde.ext.plugins.blockdown.BlockdownPlugin']
'hyde.ext.plugins.text.BlockdownPlugin']
txt ="This template tests to make sure blocks can be replaced with markdownish syntax."
templ = """
---
@@ -60,7 +60,7 @@ extends: base.html
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.auto_extend.AutoExtendPlugin',
'hyde.ext.plugins.blockdown.BlockdownPlugin']
'hyde.ext.plugins.text.BlockdownPlugin']
txt ="This template tests to make sure blocks can be replaced with markdownish syntax."
templ = """
---
@@ -69,4 +69,4 @@ default_block: title
---
%s
"""
self.assert_extended(s, txt, templ)
self.assert_extended(s, txt, templ)

+ 2
- 2
hyde/tests/ext/test_blockdown.py View File

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

def test_can_parse_blockdown(self):
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.blockdown.BlockdownPlugin']
s.config.plugins = ['hyde.ext.plugins.text.BlockdownPlugin']
txt ="This template tests to make sure blocks can be replaced with markdownish syntax."
templ = """
{%% extends "base.html" %%}
@@ -43,4 +43,4 @@ class TestBlockdown(object):
assert target.exists
text = target.read_all()
q = PyQuery(text)
assert q('title').text().strip() == txt.strip()
assert q('title').text().strip() == txt.strip()

+ 4
- 4
hyde/tests/ext/test_markings.py View File

@@ -65,7 +65,7 @@ Hyde & Jinja
site = Site(TEST_SITE)
site.config.plugins = [
'hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.markings.MarkingsPlugin']
'hyde.ext.plugins.text.MarkingsPlugin']
inc = File(TEST_SITE.child('content/inc.md'))
inc.write(text)
site.load()
@@ -104,8 +104,8 @@ Hyde & Jinja
site = Site(TEST_SITE)
site.config.plugins = [
'hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.markings.MarkingsPlugin',
'hyde.ext.plugins.markings.ReferencePlugin']
'hyde.ext.plugins.text.MarkingsPlugin',
'hyde.ext.plugins.text.ReferencePlugin']
inc = File(site.content.source_folder.child('inc.md'))
inc.write(text.strip())
src = File(site.content.source_folder.child('src.html'))
@@ -115,4 +115,4 @@ Hyde & Jinja
f = File(site.config.deploy_root_path.child(src.name))
assert f.exists
html = f.read_all()
assert_valid_conversion(html)
assert_valid_conversion(html)

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

@@ -36,7 +36,7 @@ class TestSyntext(object):
site = Site(TEST_SITE)
site.config.plugins = [
'hyde.ext.plugins.meta.MetaPlugin',
'hyde.ext.plugins.syntext.SyntextPlugin']
'hyde.ext.plugins.text.SyntextPlugin']
syn = File(site.content.source_folder.child('syn.html'))
syn.write(text)
gen = Generator(site)
@@ -46,4 +46,4 @@ class TestSyntext(object):
html = f.read_all()
assert html
q = PyQuery(html)
assert q('figure.code').length == 1
assert q('figure.code').length == 1

+ 2
- 2
hyde/tests/ext/test_textlinks.py View File

@@ -44,7 +44,7 @@ class TestTextlinks(object):
{%% endmarkdown %%}
"""
site = Site(TEST_SITE)
site.config.plugins = ['hyde.ext.plugins.textlinks.TextlinksPlugin']
site.config.plugins = ['hyde.ext.plugins.text.TextlinksPlugin']
site.config.base_url = 'http://example.com/'
site.config.media_url = '/media'
tlink = File(site.content.source_folder.child('tlink.html'))
@@ -57,4 +57,4 @@ class TestTextlinks(object):
assert html
for name, path in d.items():
assert quote(site.config.base_url + path) in html
assert '/media/img/hyde-logo.png' in html
assert '/media/img/hyde-logo.png' in html

+ 1
- 1
hyde/tests/sites/test_sorter/site.yaml View File

@@ -6,7 +6,7 @@ plugins:
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.textlinks.TextlinksPlugin
- hyde.ext.plugins.text.TextlinksPlugin
meta:
nodemeta: meta.yaml
created: !!timestamp 2010-01-01 00:00:00


+ 2
- 2
hyde/tests/sites/test_tagger/site.yaml View File

@@ -7,7 +7,7 @@ plugins:
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.tagger.TaggerPlugin
- hyde.ext.plugins.textlinks.TextlinksPlugin
- hyde.ext.plugins.text.TextlinksPlugin
meta:
nodemeta: meta.yaml
created: !!timestamp 2010-01-01 00:00:00
@@ -27,4 +27,4 @@ tagger:
template: tagged_posts.j2
source: blog
target: blog/tags
extension: html
extension: html

+ 3
- 3
hyde/tests/ssp/site.yaml View File

@@ -7,8 +7,8 @@ plugins:
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.tagger.TaggerPlugin
- hyde.ext.plugins.syntext.SyntextPlugin
- hyde.ext.plugins.textlinks.TextlinksPlugin
- hyde.ext.plugins.text.SyntextPlugin
- hyde.ext.plugins.text.TextlinksPlugin
- ext.banner.BannerPlugin
context:
data:
@@ -57,4 +57,4 @@ tagger:
source: blog
target: blog/tags
template: tagged_posts.j2
archive_extension: html
archive_extension: html

Loading…
Cancel
Save