Skip to content

Commit

Permalink
feat: add code including
Browse files Browse the repository at this point in the history
  • Loading branch information
DCsunset committed Mar 9, 2021
1 parent 484e75a commit b90177d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pandoc_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def is_include_line(elem):
# include header
return 2

def is_code_include(elem):
slices = elem.text.split(maxsplit=1)
if len(slices) <= 1:
return False
if slices[0] == "!include" or slices[0] == "$include":
return True
return False

def get_filename(elem, includeType):
fn = pf.stringify(elem, newlines=False).split(maxsplit=1)[1]
Expand Down Expand Up @@ -169,6 +176,21 @@ def action(elem, doc):
elements += new_elems

return elements

elif isinstance(elem, pf.CodeBlock):
if not is_code_include(elem):
return

# Single file
filename = elem.text.split(maxsplit=1)[1]
if not os.path.isfile(filename):
print('[Warn] Unable to open included file: ' + filename, file=sys.stderr)
return

with open(filename, encoding="utf-8") as f:
code = f.read()

elem.text = code


def main(doc=None):
Expand Down

0 comments on commit b90177d

Please sign in to comment.