Skip to content

Indented included source gets indented #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Markdown is being called. If you would like to change the directory relative to
which paths are evaluated, then this can be done by specifying the extension
setting ``base_path``.

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.
Expand Down
9 changes: 6 additions & 3 deletions markdown_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor

INC_SYNTAX = re.compile(r'\{!\s*(.+?)\s*!\}')
INC_SYNTAX = re.compile(r'([ \t]*)\{!\s*(.+?)\s*!\}')
HEADING_SYNTAX = re.compile( '^#+' )


Expand Down Expand Up @@ -83,7 +83,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(
Expand All @@ -92,6 +93,8 @@ def run(self, lines):
try:
with open(filename, 'r', encoding=self.encoding) as r:
text = r.readlines()
if len(tabs):
text = [tabs+line for line in text]

except Exception as e:
if not self.throwException:
Expand All @@ -118,7 +121,7 @@ def run(self, lines):
text[i] = text[i].rstrip('\r\n')

text[0] = line_split[0] + text[0]
text[-1] = text[-1] + line_split[2]
text[-1] = text[-1] + line_split[-1]
lines = lines[:loc] + text + lines[loc+1:]
break

Expand Down