Skip to content

Commit

Permalink
Followed review suggestions: real test, use list of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed May 7, 2017
1 parent 5b73c6b commit af11c7d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
29 changes: 4 additions & 25 deletions nikola/shortcodes.py
Expand Up @@ -209,35 +209,14 @@ def _new_sc_id():
return str('SHORTCODE{0}REPLACEMENT'.format(str(uuid.uuid4()).replace('-', '')))


def extract_shortcodes(data, _new_sc_id=_new_sc_id):
def extract_shortcodes(data):
"""
Return data with replaced shortcodes, shortcodes.
data is the original data, with the shortcodes replaced by UUIDs.
a dictionary of shortcodes, where the keys are UUIDs and the values
are the shortcodes themselves ready to process.
>>> c = 0
>>> def _new_sc_id():
... global c
... c += 1
... return 'SC%d' % c
>>> extract_shortcodes('{{% foo %}}', _new_sc_id)
(u'SC1', {u'SC1': u'{{% foo %}}'})
>>> extract_shortcodes('{{% foo %}} bar {{% /foo %}}', _new_sc_id)
(u'SC2', {u'SC2': u'{{% foo %}} bar {{% /foo %}}'})
>>> extract_shortcodes('AAA{{% foo %}} bar {{% /foo %}}BBB', _new_sc_id)
(u'AAASC3BBB', {u'SC3': u'{{% foo %}} bar {{% /foo %}}'})
>>> extract_shortcodes('AAA{{% foo %}} {{% bar %}} {{% /foo %}}BBB', _new_sc_id)
(u'AAASC4BBB', {u'SC4': u'{{% foo %}} {{% bar %}} {{% /foo %}}'})
>>> extract_shortcodes('AAA{{% foo %}} {{% /bar %}} {{% /foo %}}BBB', _new_sc_id)
(u'AAASC5BBB', {u'SC5': u'{{% foo %}} {{% /bar %}} {{% /foo %}}'})
>>> extract_shortcodes('AAA{{% foo %}} {{% bar %}} quux {{% /bar %}} {{% /foo %}}BBB', _new_sc_id)
(u'AAASC6BBB', {u'SC6': u'{{% foo %}} {{% bar %}} quux {{% /bar %}} {{% /foo %}}'})
>>> extract_shortcodes('AAA{{% foo %}} BBB {{% bar %}} quux {{% /bar %}} CCC', _new_sc_id)
(u'AAASC7 BBB SC8 CCC', {u'SC7': u'{{% foo %}}', u'SC8': u'{{% bar %}} quux {{% /bar %}}'})
"""
shortcodes = {}
splitted = _split_shortcodes(data)
Expand Down Expand Up @@ -268,14 +247,14 @@ def extract_data_chunk(data):
elif token[0] == 'SHORTCODE_END': # This is malformed
raise Exception('Closing unopened shortcode {}'.format(token[3]))

text = ''
text = []
tail = splitted
while True:
new_text, tail = extract_data_chunk(tail)
text += new_text
text.append(new_text)
if not tail:
break
return text, shortcodes
return ''.join(text), shortcodes


def _split_shortcodes(data):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_shortcodes.py
Expand Up @@ -74,3 +74,20 @@ def test_errors(self):
self.assertRaisesRegexp(shortcodes.ParsingError, "^Found shortcode ending '{{% / %}}' which isn't closing a started shortcode", shortcodes.apply_shortcodes, '{{% / %}}', self.fakesite.shortcode_registry, raise_exceptions=True)
self.assertRaisesRegexp(shortcodes.ParsingError, "^Syntax error: '{{% /' must be followed by ' %}}'", shortcodes.apply_shortcodes, '{{% / a %}}', self.fakesite.shortcode_registry, raise_exceptions=True)
self.assertRaisesRegexp(shortcodes.ParsingError, "^Shortcode '<==' starting at .* is not terminated correctly with '%}}'!", shortcodes.apply_shortcodes, '==> {{% <==', self.fakesite.shortcode_registry, raise_exceptions=True)


@pytest.mark.parametrize("input, expected", [
('{{% foo %}}', (u'SC1', {u'SC1': u'{{% foo %}}'})),
('{{% foo %}} bar {{% /foo %}}', (u'SC1', {u'SC1': u'{{% foo %}} bar {{% /foo %}}'})),
('AAA{{% foo %}} bar {{% /foo %}}BBB', (u'AAASC1BBB', {u'SC1': u'{{% foo %}} bar {{% /foo %}}'})),
('AAA{{% foo %}} {{% bar %}} {{% /foo %}}BBB', (u'AAASC1BBB', {u'SC1': u'{{% foo %}} {{% bar %}} {{% /foo %}}'})),
('AAA{{% foo %}} {{% /bar %}} {{% /foo %}}BBB', (u'AAASC1BBB', {u'SC1': u'{{% foo %}} {{% /bar %}} {{% /foo %}}'})),
('AAA{{% foo %}} {{% bar %}} quux {{% /bar %}} {{% /foo %}}BBB', (u'AAASC1BBB', {u'SC1': u'{{% foo %}} {{% bar %}} quux {{% /bar %}} {{% /foo %}}'})),
('AAA{{% foo %}} BBB {{% bar %}} quux {{% /bar %}} CCC', (u'AAASC1 BBB SC2 CCC', {u'SC1': u'{{% foo %}}', u'SC2': u'{{% bar %}} quux {{% /bar %}}'})),
])
def test_extract_shortcodes(input, expected, monkeypatch):

i = iter('SC%d' % i for i in range(1, 100))
monkeypatch.setattr(shortcodes, '_new_sc_id', i.next)
extracted = shortcodes.extract_shortcodes(input)
assert extracted == expected

0 comments on commit af11c7d

Please sign in to comment.