From 66e67ea71d8dca72fa969248f9f226973530498b Mon Sep 17 00:00:00 2001 From: Daniel Maljovec Date: Mon, 15 Jul 2019 15:20:20 -0600 Subject: [PATCH 1/3] Adding sphinx-style line limiting directives See those listed [here](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html?highlight=start-after#directive-literalinclude) Specifically, this should add support for `lines`, `start-after` and `end-before` --- m2r.py | 74 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/m2r.py b/m2r.py index a4e43c2..1dfe3aa 100644 --- a/m2r.py +++ b/m2r.py @@ -58,17 +58,17 @@ def parse_options(): class RestBlockGrammar(mistune.BlockGrammar): directive = re.compile( - r'^( *\.\..*?)\n(?=\S)', - re.DOTALL | re.MULTILINE, - ) + r'^( *\.\..*?)\n(?=\S)', + re.DOTALL | re.MULTILINE, + ) oneline_directive = re.compile( - r'^( *\.\..*?)$', - re.DOTALL | re.MULTILINE, - ) + r'^( *\.\..*?)$', + re.DOTALL | re.MULTILINE, + ) rest_code_block = re.compile( - r'^::\s*$', - re.DOTALL | re.MULTILINE, - ) + r'^::\s*$', + re.DOTALL | re.MULTILINE, + ) class RestBlockLexer(mistune.BlockLexer): @@ -483,6 +483,7 @@ def footnotes(self, text): return '' """Below outputs are for rst.""" + def image_link(self, url, target, alt): return '\n'.join([ '', @@ -575,6 +576,9 @@ class MdInclude(rst.Directive): option_spec = { 'start-line': int, 'end-line': int, + 'lines': str, + 'start-after': str, + 'end-before': str, } def run(self): @@ -614,19 +618,55 @@ def run(self): raise self.severe('Problems with "%s" directive path:\n%s.' % (self.name, ErrorString(error))) - # read from the file - startline = self.options.get('start-line', None) - endline = self.options.get('end-line', None) + # get all of the lines and reduce as needed try: - if startline or (endline is not None): - lines = include_file.readlines() - rawtext = ''.join(lines[startline:endline]) - else: - rawtext = include_file.read() + lines = include_file.readlines() + line_count = len(lines) except UnicodeError as error: raise self.severe('Problem with "%s" directive:\n%s' % (self.name, ErrorString(error))) + # read from the file + start_line = self.options.get('start-line', 1) - 1 + end_line = self.options.get('end-line', line_count) + line_ranges = self.options.get('lines', None) + start_marker = self.options.get('start-after', None) + end_marker = self.options.get('end-before', None) + + if start_marker: + for i, line in enumerate(lines[start_line:end_line], start_line): + if start_marker in line: + if i > start_line: + start_line = i + break + if i == len(lines): + start_line = i + + if end_marker: + for i, line in enumerate(lines[start_line:end_line], start_line): + if end_marker in line: + if i < end_line: + end_line = i + break + + keep = set(range(start_line, end_line)) + + if line_ranges: + keep_line_ranges = set() + line_range_list = line_ranges.split(',') + for line_range in line_range_list: + endpoints = [int(number) if len(number) else len(lines) + for number in line_range.split('-', 1)] + if len(endpoints) > 1: + keep_line_ranges.update( + set(range(endpoints[0], endpoints[1]+1))) + else: + keep_line_ranges.add(endpoints[0]) + keep = keep.intersection(keep_line_ranges) + + lines = [lines[i] for i in sorted(keep)] + rawtext = ''.join(lines) + config = self.state.document.settings.env.config converter = M2R( no_underscore_emphasis=config.no_underscore_emphasis, From 3561601cae74ea8decbc1f4febdd7135507cdbd8 Mon Sep 17 00:00:00 2001 From: Daniel Maljovec Date: Mon, 15 Jul 2019 15:58:50 -0600 Subject: [PATCH 2/3] Adding examples of markdown inclusion directives --- docs/example.md | 48 +++++++++++++++++++++++++++++++++++++----- docs/line_inclusion.md | 9 ++++++++ docs/text_inclusion.md | 14 ++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 docs/line_inclusion.md create mode 100644 docs/text_inclusion.md diff --git a/docs/example.md b/docs/example.md index a9e4518..dd8ac02 100644 --- a/docs/example.md +++ b/docs/example.md @@ -142,13 +142,13 @@ The below is reST-style math-block. ### Include Markdown file -To include markdown file: +To include a markdown file: ```rest .. mdinclude:: path-to-file.md ``` -To include markdown file with specific lines: +To include a markdown file with a specific start and end line: ```rest .. mdinclude:: included.md @@ -156,23 +156,61 @@ To include markdown file with specific lines: :end-line: -2 ``` -Original ``included.md`` file is: +The original ``included.md`` file is: .. include:: included.md :code: md -This file included as: +This file is included as: ```md #### Include this line ``` -and results in HTML as below: +and the results in HTML are as shown below: .. mdinclude:: included.md :start-line: 2 :end-line: -2 +To include a markdown file with specific line ranges: + +```rest +.. mdinclude:: line_inclusion.md + :lines: 1, 3-4, 7- +``` + +The original ``line_inclusion.md`` file is: + +.. include:: line_inclusion.md + :code: md + +And the resulting HTML output is: + +.. mdinclude:: line_inclusion.md + :lines: 1, 3-4, 7- + + +To include a markdown file by specific text: + +```rest +.. mdinclude:: text_inclusion.md + :start-after: Start Here + :end-before: End Here +``` + +The original ``text_inclusion.md`` file is: + +.. include:: text_inclusion.md + :code: md + +And the resulting HTML output is: + +.. mdinclude:: text_inclusion.md + :start-after: Start Here + :end-before: End Here + + ### Footnote Footnote[^1] and footnote[^key] with markdown. diff --git a/docs/line_inclusion.md b/docs/line_inclusion.md new file mode 100644 index 0000000..36c1184 --- /dev/null +++ b/docs/line_inclusion.md @@ -0,0 +1,9 @@ +#### Include +#### Skip +#### Include +#### Include +#### Skip +#### Skip +#### Include +#### Include +#### Include \ No newline at end of file diff --git a/docs/text_inclusion.md b/docs/text_inclusion.md new file mode 100644 index 0000000..1bfe6cb --- /dev/null +++ b/docs/text_inclusion.md @@ -0,0 +1,14 @@ +#### Skip +#### Skip + +#### Start Here +#### Include + +#### Include +#### Include +#### Include +#### End Here (Skip) + +#### Skip +#### Skip +#### Skip \ No newline at end of file From 29d6c114f016cb76a1a69682d17740e508ad1b1d Mon Sep 17 00:00:00 2001 From: Daniel Maljovec Date: Mon, 15 Jul 2019 16:30:43 -0600 Subject: [PATCH 3/3] Fixing off by one indices. --- m2r.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/m2r.py b/m2r.py index 1dfe3aa..7832093 100644 --- a/m2r.py +++ b/m2r.py @@ -659,9 +659,9 @@ def run(self): for number in line_range.split('-', 1)] if len(endpoints) > 1: keep_line_ranges.update( - set(range(endpoints[0], endpoints[1]+1))) + set(range(endpoints[0]-1, endpoints[1]))) else: - keep_line_ranges.add(endpoints[0]) + keep_line_ranges.add(endpoints[0]-1) keep = keep.intersection(keep_line_ranges) lines = [lines[i] for i in sorted(keep)]