Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
lint
  • Loading branch information
ralsina committed Dec 23, 2015
1 parent 8719341 commit eaf1d30
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion nikola/nikola.py
Expand Up @@ -1302,7 +1302,7 @@ def register_shortcode(self, name, f):

def apply_shortcodes(self, data):
"""Apply shortcodes from the registry into data."""

return shortcodes.apply_shortcodes(data, self.shortcode_registry)

def generic_rss_renderer(self, lang, title, link, description, timeline, output_path,
Expand Down
1 change: 1 addition & 0 deletions nikola/plugins/compile/markdown/__init__.py
Expand Up @@ -43,6 +43,7 @@
from nikola.utils import makedirs, req_missing, write_metadata
from nikola.shortcodes import apply_shortcodes


class CompileMarkdown(PageCompiler):
"""Compile Markdown into HTML."""

Expand Down
12 changes: 6 additions & 6 deletions nikola/plugins/compile/rest/post_list.py
Expand Up @@ -150,14 +150,14 @@ def run(self):
sort = self.options.get('sort')

output = _do_post_list(start, stop, reverse, tags, categories, slugs, show_all,
lang, template, sort, state=self.state, site=self.site)
lang, template, sort, state=self.state, site=self.site)
self.state.document.settings.record_dependencies.add("####MAGIC####TIMELINE")
return [nodes.raw('', output, format='html')]
def _do_post_list(start=None, stop=None, reverse = False, tags=None, categories=None,
slugs=None, show_all=False, lang=None, template='post_list_directive.tmpl',
sort=None, id=None, data=None, state=None, site=None):


def _do_post_list(start=None, stop=None, reverse=False, tags=None, categories=None,
slugs=None, show_all=False, lang=None, template='post_list_directive.tmpl',
sort=None, id=None, data=None, state=None, site=None):
if lang is None:
lang = utils.LocaleBorg().current_lang
if site.invariant: # for testing purposes
Expand Down
2 changes: 1 addition & 1 deletion nikola/post.py
Expand Up @@ -483,7 +483,7 @@ def wrap_encrypt(path, password):
self.translated_source_path(lang),
dest,
self.is_two_file),

if self.meta('password'):
# TODO: get rid of this feature one day (v8?; warning added in v7.3.0.)
LOGGER.warn("The post {0} is using the `password` attribute, which may stop working in the future.")
Expand Down
38 changes: 18 additions & 20 deletions nikola/shortcodes.py
Expand Up @@ -34,20 +34,20 @@

def apply_shortcodes(data, registry, site=None):
"""Support for hugo-style shortcodes.
{{% name parameters %}} will end up calling the registered "name" function with the given parameters.
{{% name parameters %}} something {{% /name %}} will call name with the parameters and
{{% name parameters %}} something {{% /name %}} will call name with the parameters and
one extra "data" parameter containing " something ".
The site parameter is passed with the same name to the shortcodes so they can access Nikola state.
>>> apply_shortcodes('==> {{% foo bar=baz %}} <==', {'foo': lambda *a, **k: k['bar']})
'==> baz <=='
>>> apply_shortcodes('==> {{% foo bar=baz %}}some data{{% /foo %}} <==', {'foo': lambda *a, **k: k['bar']+k['data']})
'==> bazsome data <=='
"""

shortcodes = list(_find_shortcodes(data))
# Calculate shortcode results
for sc in shortcodes:
Expand All @@ -56,26 +56,27 @@ def apply_shortcodes(data, registry, site=None):
kw['site'] = site
result = registry[name](*a, **kw)
sc.append(result)

# Replace all shortcodes with their output
for sc in shortcodes[::-1]:
_, _, start, end, result = sc
data = data[:start] + result + data[end:]
return data


def _find_shortcodes(data):
""" Find starta and end of shortcode markers.
>>> list(_find_shortcodes('{{% foo %}}{{% bar %}}'))
[['foo', ([], {}), 0, 11], ['bar', ([], {}), 11, 22]]
>>> list(_find_shortcodes('{{% foo bar baz=bat fee=fi fo fum %}}'))
[['foo', (['bar', 'fo', 'fum'], {'fee': 'fi', 'baz': 'bat'}), 0, 37]]
>>> list(_find_shortcodes('{{% foo bar bat=baz%}}some data{{% /foo %}}'))
[['foo', (['bar'], {'bat': 'baz', 'data': 'some data'}), 0, 43]]
"""

# FIXME: this is really space-intolerant

parser = SCParser()
Expand All @@ -86,24 +87,23 @@ def _find_shortcodes(data):
break
# Get the whole shortcode tag
end = data.find('%}}', start + 1)
name, args = parser.parse_sc('<{}>'.format(data[start + 3:end].strip()))
name, args = parser.parse_sc('<{}>'.format(data[start + 3:end].strip()))
# Check if this start has a matching close
close_tag = '{{% /{} %}}'.format(name)
close = data.find(close_tag, end + 3)
if close == -1: # No closer
end = end + 3
else: # Tag with closer
args[1]['data'] = data[end + 3:close-1]
args[1]['data'] = data[end + 3:close - 1]
end = close + len(close_tag) + 1
pos = end
yield [name, args, start, end]
class SCParser(HTMLParser):


class SCParser(HTMLParser):
"""Because shortcode attributes are HTML-like abusing the HTML parser parser."""

def parse_sc(self, data):
#print('====> ', data)
self.name = None
self.attrs = {}
self.feed(data)
Expand All @@ -115,9 +115,7 @@ def parse_sc(self, data):
else:
kwargs[a] = b
return self.name, (args, kwargs)

def handle_starttag(self, tag, attrs):
self.name = tag
self.attrs = attrs


0 comments on commit eaf1d30

Please sign in to comment.