From 28a425cc66c5f4aa8eedc70e7123354c073f5dd0 Mon Sep 17 00:00:00 2001 From: Yusuke Niitani Date: Sat, 4 Dec 2021 11:25:47 +0900 Subject: [PATCH] merge --- README.md | 4 ++++ markdown_include/include.py | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6f576c5..d9c594a 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ You can use this to include lines in a different order than the original file. B also means that if you want to preserve the original order, you have to pay attention to the order in which you specify the lines. +If there are leading tabs and spaces before the include statement, +all the lines of the included file get prepended the same number of tabs, +so includes to indented sections get automatically indented. + ## Configuration The following settings can be specified when initialising the plugin. diff --git a/markdown_include/include.py b/markdown_include/include.py index fc60b37..907e36c 100644 --- a/markdown_include/include.py +++ b/markdown_include/include.py @@ -29,7 +29,8 @@ from markdown.extensions import Extension from markdown.preprocessors import Preprocessor -INC_SYNTAX = re.compile(r'{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}') +INC_SYNTAX = re.compile(r'([ \t]*)\{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}') +# INC_SYNTAX = re.compile(r'([ \t]*)\{!\s*(.+?)\s*!\}') HEADING_SYNTAX = re.compile( '^#+' ) @@ -83,7 +84,8 @@ def run(self, lines): m = INC_SYNTAX.search(line) if m: - filename = m.group(1) + tabs = m.group(1) + filename = m.group(2) filename = os.path.expanduser(filename) if not os.path.isabs(filename): filename = os.path.normpath( @@ -92,6 +94,8 @@ def run(self, lines): try: with open(filename, 'r', encoding=self.encoding) as r: original_text = r.readlines() + if len(tabs): + original_text = [tabs+line for line in original_text] except Exception as e: if not self.throwException: @@ -101,10 +105,10 @@ def run(self, lines): break else: raise e - if m.group(2) is None: + if m.group(3) is None: text = original_text else: - lines_str = m.group(4) + lines_str = m.group(5) lines_blocks = lines_str.split() wanted_lines = [] for block in lines_blocks: @@ -158,7 +162,8 @@ def run(self, lines): text[i] = text[i].rstrip('\r\n') text[0] = line_split[0] + text[0] - text[-1] = text[-1] + line_split[5] + # text[-1] = text[-1] + line_split[5] + text[-1] = text[-1] + line_split[-1] lines = lines[:loc] + text + lines[loc+1:] break