Skip to content

Commit

Permalink
Merge pull request #2 from nasa9084/fix/1-in-inlinecode
Browse files Browse the repository at this point in the history
fix #1: inline html and org code shouldn't interpreted
  • Loading branch information
nasa9084 committed Dec 13, 2016
2 parents 26ecb0f + af7b038 commit 1d9e9cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pyorg/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def __init__(self, value, parent=None):
def _parse_value(self, value):
if value is None:
return ''
if self.regexps['link'].search(value):

if self.regexps['code'].search(value):
before, text, after = self.regexps['code'].split(value, 1)
parsed = InlineCodeText(text)
elif self.regexps['link'].search(value):
before, url, subject, after = self.regexps['link'].split(value, 1)
parsed = Link(url, subject)
elif self.regexps['image'].search(value):
Expand All @@ -98,9 +102,6 @@ def _parse_value(self, value):
elif self.regexps['linethrough'].search(value):
before, text, after = self.regexps['linethrough'].split(value, 1)
parsed = LinethroughText(text)
elif self.regexps['code'].search(value):
before, text, after = self.regexps['code'].split(value, 1)
parsed = InlineCodeText(text)
elif self.regexps['monospace'].search(value):
before, text, after = self.regexps['monospace'].split(value, 1)
parsed = MonospaceText(text)
Expand Down Expand Up @@ -191,6 +192,22 @@ def _get_close(self):

class InlineCodeText(Text):
'''Inline Code Text Class'''
less_than = compile(r'<')
greater_than = compile(r'>')
def _parse_value(self, value):
return [value]

def html(self, br=''):
content = ''
for value in self.values:
if isinstance(value, str):
content += value.strip()
else:
content += value.html(br)
content = self.less_than.sub('&lt;', content)
content = self.greater_than.sub('&gt;', content)
return self._get_open() + content + self._get_close()

def _get_open(self):
return '<code>'

Expand Down
10 changes: 10 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ def test_slide_heading_html(self):
o = Org(text, default_heading=2)
eq_(o.html(), '<h2>header1</h2><p>paraparapara</p><h3>header2-1</h3><p><img src="image">para<span style="font-weight: bold;">para</span>2<a href="http://example.com">hyperlink</a></p><h3>header2-2</h3><table><tr><td>a</td><td>b</td></tr><tr><td>1</td><td>2</td></tr></table><h4>header3</h4><blockquote>quoted</blockquote>')

def test_inlinecode(self):
text = '=inline text='
o = Org(text)
eq_(o.html(), '<p><code>inline text</code></p>')
text = '=/inline italic text/='
o = Org(text)
eq_(o.html(), '<p><code>/inline italic text/</code></p>')
text = '=<tag>='
o = Org(text)
eq_(o.html(), '<p><code>&lt;tag&gt;</code></p>')

class TestOrgToHTMLFunction(TestCase):
def test_html(self):
Expand Down

0 comments on commit 1d9e9cc

Please sign in to comment.