Skip to content

Commit c580479

Browse files
authored
Merge pull request #1 from yuyu2172/merge
merge
2 parents e2ac6c7 + 28a425c commit c580479

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ You can use this to include lines in a different order than the original file. B
5858
also means that if you want to preserve the original order, you have to pay attention
5959
to the order in which you specify the lines.
6060

61+
If there are leading tabs and spaces before the include statement,
62+
all the lines of the included file get prepended the same number of tabs,
63+
so includes to indented sections get automatically indented.
64+
6165
## Configuration
6266

6367
The following settings can be specified when initialising the plugin.

markdown_include/include.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
from markdown.extensions import Extension
3030
from markdown.preprocessors import Preprocessor
3131

32-
INC_SYNTAX = re.compile(r'{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}')
32+
INC_SYNTAX = re.compile(r'([ \t]*)\{!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}')
33+
# INC_SYNTAX = re.compile(r'([ \t]*)\{!\s*(.+?)\s*!\}')
3334
HEADING_SYNTAX = re.compile( '^#+' )
3435

3536

@@ -83,7 +84,8 @@ def run(self, lines):
8384
m = INC_SYNTAX.search(line)
8485

8586
if m:
86-
filename = m.group(1)
87+
tabs = m.group(1)
88+
filename = m.group(2)
8789
filename = os.path.expanduser(filename)
8890
if not os.path.isabs(filename):
8991
filename = os.path.normpath(
@@ -92,6 +94,8 @@ def run(self, lines):
9294
try:
9395
with open(filename, 'r', encoding=self.encoding) as r:
9496
original_text = r.readlines()
97+
if len(tabs):
98+
original_text = [tabs+line for line in original_text]
9599

96100
except Exception as e:
97101
if not self.throwException:
@@ -101,10 +105,10 @@ def run(self, lines):
101105
break
102106
else:
103107
raise e
104-
if m.group(2) is None:
108+
if m.group(3) is None:
105109
text = original_text
106110
else:
107-
lines_str = m.group(4)
111+
lines_str = m.group(5)
108112
lines_blocks = lines_str.split()
109113
wanted_lines = []
110114
for block in lines_blocks:
@@ -158,7 +162,8 @@ def run(self, lines):
158162
text[i] = text[i].rstrip('\r\n')
159163

160164
text[0] = line_split[0] + text[0]
161-
text[-1] = text[-1] + line_split[5]
165+
# text[-1] = text[-1] + line_split[5]
166+
text[-1] = text[-1] + line_split[-1]
162167
lines = lines[:loc] + text + lines[loc+1:]
163168
break
164169

0 commit comments

Comments
 (0)