Skip to content

Commit

Permalink
Merge pull request #19 from livio/inline-platform-sections
Browse files Browse the repository at this point in the history
Add Inline platform sections
  • Loading branch information
jemerick authored Jun 6, 2019
2 parents e7c7507 + e30bb5c commit b7ec40e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 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.3 (2019-06-06)
------------------

* Add support for inline platform section tags

0.2.2 (2019-06-05)
------------------

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.2'
__version__ = '0.2.3'
35 changes: 29 additions & 6 deletions docdown/platform_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,55 @@

class PlatformSectionPreprocessor(Preprocessor):

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

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

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)
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):
while 1:
m = self.RE.search(text)
m = self.INLINE_RE.search(text)
if m:
sections = [section.lower().strip() for section in m.group('sections').split(',')]
sections = self.split_sections(m.group('sections'))

content = m.group('content')
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():])
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'))

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

return text.split("\n")
return text


class PlatformSectionExtension(Extension):
Expand Down
7 changes: 7 additions & 0 deletions docs/extensions/platform_sections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ In documents
This section will be displayed for Java SE and EE builds.
!@

Inline platform section tags are also supported:

.. code-block:: html5
### 1. Creating an App Service Manifest
The first step to publishing is to create an @![iOS]`SDLAppServiceManifest`!@ @![Android, JavaSE, JavaEE]`AppServiceManifest`!@ object.
Python
--------------

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.2
current_version = 0.2.3
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.2',
version='0.2.3',
description="DocDown is a Markdown extension for source code documentation.",
long_description=readme + '\n\n' + history,
author="Jason Emerick, Justin Michalicek",
Expand Down
31 changes: 31 additions & 0 deletions tests/test_platform_section_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,34 @@ def test_multiple_sections_with_code_snippet(self):
)
expected_output = '<p>some Android content shown</p>\n<p><code>java\nString java = "asdf";</code></p>'
self.assertEqual(expected_output, html)

def test_inline_platform_section(self):
text = ('### 1. Creating an App Service Manifest\n'
'The first step to publishing is to create an @![iOS]`SDLAppServiceManifest`!@ @![Android, JavaSE, JavaEE]`AppServiceManifest`!@ object.\n')

html = markdown.markdown(
text,
extension_configs=self.build_config_for_platform_section('Android'),
extensions=self.MARKDOWN_EXTENSIONS,
output_format='html5'
)
expected_output = '<h3>1. Creating an App Service Manifest</h3>\n<p>The first step to publishing is to create an <code>AppServiceManifest</code> object.</p>'
self.assertEqual(expected_output, html)

html = markdown.markdown(
text,
extension_configs=self.build_config_for_platform_section('JavaEE'),
extensions=self.MARKDOWN_EXTENSIONS,
output_format='html5'
)
expected_output = '<h3>1. Creating an App Service Manifest</h3>\n<p>The first step to publishing is to create an <code>AppServiceManifest</code> object.</p>'
self.assertEqual(expected_output, html)

html = markdown.markdown(
text,
extension_configs=self.build_config_for_platform_section('iOS'),
extensions=self.MARKDOWN_EXTENSIONS,
output_format='html5'
)
expected_output = '<h3>1. Creating an App Service Manifest</h3>\n<p>The first step to publishing is to create an <code>SDLAppServiceManifest</code> object.</p>'
self.assertEqual(expected_output, html)

0 comments on commit b7ec40e

Please sign in to comment.