Skip to content

Commit

Permalink
Merge pull request #21 from livio/0.2.5
Browse files Browse the repository at this point in the history
Fix whitespace issues with back to back tags
  • Loading branch information
jemerick authored Jun 12, 2019
2 parents f3df9d4 + 7230b79 commit ac868c3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

0.2.5 (2019-06-12)
------------------

* Fix whitespace issues with back to back tags

0.2.4 (2019-06-11)
------------------

Expand Down
2 changes: 1 addition & 1 deletion docdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__author__ = """Jason Emerick"""
__email__ = '[email protected]'
__version__ = '0.2.4'
__version__ = '0.2.5'
36 changes: 11 additions & 25 deletions docdown/platform_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,38 @@

class PlatformSectionPreprocessor(Preprocessor):

BLOCK_RE = re.compile(r'''
^@!\[(?P<sections>[\w, ]+)\]\W*\n
(?P<content>.*?)(?<=\n)
!@\W*$''', re.MULTILINE | re.DOTALL | re.VERBOSE)
PLATFORM_SECTION_RE = re.compile(r'''@!\[(?P<sections>[\w, ]+)\](?P<content>.*?)!@''', re.DOTALL | re.VERBOSE)

INLINE_RE = re.compile(r'''@!\[(?P<sections>[\w, ]+)\](?P<content>.*?)!@''', re.DOTALL | re.VERBOSE)
STARTSWITH_WHITESPACE_RE = re.compile(r'^\W+(@!\[|\n)')

def __init__(self, platform_section, **kwargs):
self.platform_section = platform_section.lower().strip()
super(PlatformSectionPreprocessor, self).__init__(**kwargs)

def run(self, lines):
text = "\n".join(lines)
text = self.process_inline(text)
text = self.process_block(text)
text = self.process_platform_sections(text)
return text.split("\n")

def split_sections(self, sections_group):
return [section.lower().strip() for section in sections_group.split(',')]

def process_inline(self, text):
def process_platform_sections(self, text):
while 1:
m = self.INLINE_RE.search(text)
m = self.PLATFORM_SECTION_RE.search(text)
if m:
sections = self.split_sections(m.group('sections'))

if self.platform_section in sections:
content = m.group('content')
text = '%s%s%s' % (text[:m.start()], content, text[m.end():])
else:
text = '%s%s' % (text[:m.start()], text[m.end():].lstrip())
else:
break
return text

def process_block(self, text):
while 1:
m = self.BLOCK_RE.search(text)
if m:
sections = self.split_sections(m.group('sections'))
start = text[:m.start()]
end = text[m.end():]
if self.STARTSWITH_WHITESPACE_RE.match(end):
end = end.lstrip()

if self.platform_section in sections:
content = m.group('content')
text = '%s\n%s\n%s' % (text[:m.start()], content, text[m.end():])
text = '%s%s%s' % (start, content, end)
else:
text = '%s\n%s' % (text[:m.start()], text[m.end():])
text = '%s%s' % (start, end)
else:
break
return text
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.4
current_version = 0.2.5
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setup(
name='docdown',
version='0.2.4',
version='0.2.5',
description="DocDown is a Markdown extension for source code documentation.",
long_description=readme + '\n\n' + history,
author="Jason Emerick, Justin Michalicek",
Expand Down
28 changes: 24 additions & 4 deletions tests/test_platform_section_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_section_with_code_snippet(self):
extensions=self.MARKDOWN_EXTENSIONS,
output_format='html5'
)
expected_output = '<p>some Android content shown</p>\n<p><code>java\nString java = "asdf";</code></p>'
expected_output = '<p>some Android content shown</p>\n<p><code>java\nString java = "asdf";</code>\n</p>'
self.assertEqual(expected_output, html)

def test_multiple_sections_with_code_snippet(self):
Expand All @@ -181,7 +181,7 @@ def test_multiple_sections_with_code_snippet(self):
extensions=self.MARKDOWN_EXTENSIONS,
output_format='html5'
)
expected_output = '<p>some Android content shown</p>\n<p><code>java\nString java = "asdf";</code></p>'
expected_output = '<p>some Android content shown</p>\n<p><code>java\nString java = "asdf";</code>\n</p>'
self.assertEqual(expected_output, html)

def test_inline_platform_section(self):
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_inline_platform_section(self):
output_format='html5'
)
expected_output = '<p>This is just some inline text for the Android platform.</p>'
print(html)

self.assertEqual(expected_output, html)

def test_table_row_platform_section(self):
Expand Down Expand Up @@ -286,4 +286,24 @@ def test_table_row_platform_section(self):
)

self.assertEqual(expected_output, html)
print(html)

def test_back_to_back_platform_section_tags(self):
text = 'Back to back @![ios]iOS!@@![android]Android!@ tags!'
html = markdown.markdown(
text,
extension_configs=self.build_config_for_platform_section('iOS'),
extensions=['markdown.extensions.tables'] + self.MARKDOWN_EXTENSIONS,
output_format='html5'
)

expected_output = '<p>Back to back iOS tags!</p>'
self.assertEqual(expected_output, html)

html = markdown.markdown(
text,
extension_configs=self.build_config_for_platform_section('Android'),
extensions=['markdown.extensions.tables'] + self.MARKDOWN_EXTENSIONS,
output_format='html5'
)
expected_output = '<p>Back to back Android tags!</p>'
self.assertEqual(expected_output, html)

0 comments on commit ac868c3

Please sign in to comment.