2929from markdown .extensions import Extension
3030from markdown .preprocessors import Preprocessor
3131
32- INC_SYNTAX = re .compile (r' {!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}' )
33- HEADING_SYNTAX = re .compile ( ' ^#+' )
32+ INC_SYNTAX = re .compile (r" {!\s*(.+?)\s*!((\blines\b)=([0-9 -]+))?\}" )
33+ HEADING_SYNTAX = re .compile (" ^#+" )
3434
3535
3636class MarkdownInclude (Extension ):
3737 def __init__ (self , configs = {}):
3838 self .config = {
39- 'base_path' : ['.' , 'Default location from which to evaluate ' \
40- 'relative paths for the include statement.' ],
41- 'encoding' : ['utf-8' , 'Encoding of the files used by the include ' \
42- 'statement.' ],
43- 'inheritHeadingDepth' : [False , 'Increases headings on included ' \
44- 'file by amount of previous heading (combines with ' \
45- 'headingOffset option).' ],
46- 'headingOffset' : [0 , 'Increases heading depth by a specific ' \
47- 'amount (and the inheritHeadingDepth option). Defaults to 0.' ],
48- 'throwException' : [False , 'When true, if the extension is unable ' \
49- 'to find an included file it will throw an ' \
50- 'exception which the user can catch. If false ' \
51- '(default), a warning will be printed and ' \
52- 'Markdown will continue parsing the file.' ]
39+ "base_path" : [
40+ "." ,
41+ "Default location from which to evaluate "
42+ "relative paths for the include statement." ,
43+ ],
44+ "encoding" : [
45+ "utf-8" ,
46+ "Encoding of the files used by the include " "statement." ,
47+ ],
48+ "inheritHeadingDepth" : [
49+ False ,
50+ "Increases headings on included "
51+ "file by amount of previous heading (combines with "
52+ "headingOffset option)." ,
53+ ],
54+ "headingOffset" : [
55+ 0 ,
56+ "Increases heading depth by a specific "
57+ "amount (and the inheritHeadingDepth option). Defaults to 0." ,
58+ ],
59+ "throwException" : [
60+ False ,
61+ "When true, if the extension is unable "
62+ "to find an included file it will throw an "
63+ "exception which the user can catch. If false "
64+ "(default), a warning will be printed and "
65+ "Markdown will continue parsing the file." ,
66+ ],
5367 }
5468 for key , value in configs .items ():
5569 self .setConfig (key , value )
5670
5771 def extendMarkdown (self , md ):
58- md .preprocessors .register (IncludePreprocessor (md ,self .getConfigs ()), 'include' , 101 )
72+ md .preprocessors .register (
73+ IncludePreprocessor (md , self .getConfigs ()), "include" , 101
74+ )
5975
6076
6177class IncludePreprocessor (Preprocessor ):
62- '''
78+ """
6379 This provides an "include" function for Markdown, similar to that found in
6480 LaTeX (also the C pre-processor and Fortran). The syntax is {!filename!},
6581 which will be replaced by the contents of filename. Any such statements in
6682 filename will also be replaced. This replacement is done prior to any other
6783 Markdown processing. All file-names are evaluated relative to the location
6884 from which Markdown is being called.
69- '''
85+ """
86+
7087 def __init__ (self , md , config ):
7188 super (IncludePreprocessor , self ).__init__ (md )
72- self .base_path = config [' base_path' ]
73- self .encoding = config [' encoding' ]
74- self .inheritHeadingDepth = config [' inheritHeadingDepth' ]
75- self .headingOffset = config [' headingOffset' ]
76- self .throwException = config [' throwException' ]
89+ self .base_path = config [" base_path" ]
90+ self .encoding = config [" encoding" ]
91+ self .inheritHeadingDepth = config [" inheritHeadingDepth" ]
92+ self .headingOffset = config [" headingOffset" ]
93+ self .throwException = config [" throwException" ]
7794
7895 def run (self , lines ):
7996 done = False
80- bonusHeading = ''
97+ bonusHeading = ""
8198 while not done :
8299 for loc , line in enumerate (lines ):
83100 m = INC_SYNTAX .search (line )
@@ -87,17 +104,19 @@ def run(self, lines):
87104 filename = os .path .expanduser (filename )
88105 if not os .path .isabs (filename ):
89106 filename = os .path .normpath (
90- os .path .join (self .base_path ,filename )
107+ os .path .join (self .base_path , filename )
91108 )
92109 try :
93- with open (filename , 'r' , encoding = self .encoding ) as r :
110+ with open (filename , "r" , encoding = self .encoding ) as r :
94111 original_text = self .run (r .readlines ())
95-
112+
96113 except Exception as e :
97114 if not self .throwException :
98- print ('Warning: could not find file {}. Ignoring '
99- 'include statement. Error: {}' .format (filename , e ))
100- lines [loc ] = INC_SYNTAX .sub ('' ,line )
115+ print (
116+ "Warning: could not find file {}. Ignoring "
117+ "include statement. Error: {}" .format (filename , e )
118+ )
119+ lines [loc ] = INC_SYNTAX .sub ("" , line )
101120 break
102121 else :
103122 raise e
@@ -127,8 +146,10 @@ def run(self, lines):
127146 f"smaller than the end line: { current_end } "
128147 f"using start: { current_start } "
129148 )
130-
131- wanted_lines .extend (original_text [current_start - 1 :current_end ])
149+
150+ wanted_lines .extend (
151+ original_text [current_start - 1 : current_end ]
152+ )
132153 else :
133154 wanted_line = int (block .strip ())
134155 current_line = wanted_line
@@ -138,39 +159,38 @@ def run(self, lines):
138159 f"Warning: line: { wanted_line } is larger than "
139160 f"file: { filename } using end: { current_line } "
140161 )
141- wanted_lines .append (original_text [current_line - 1 ])
162+ wanted_lines .append (original_text [current_line - 1 ])
142163 text = wanted_lines
143164
144-
145165 if len (text ) == 0 :
146- text .append ('' )
166+ text .append ("" )
147167 for i in range (len (text )):
148168 # Strip the newline, and optionally increase header depth
149169 if self .inheritHeadingDepth or self .headingOffset :
150170 if HEADING_SYNTAX .search (text [i ]):
151- text [i ] = text [i ].rstrip (' \r \n ' )
171+ text [i ] = text [i ].rstrip (" \r \n " )
152172 if self .inheritHeadingDepth :
153173 text [i ] = bonusHeading + text [i ]
154174 if self .headingOffset :
155- text [i ] = '#' * self .headingOffset + text [i ]
175+ text [i ] = "#" * self .headingOffset + text [i ]
156176 else :
157- text [i ] = text [i ].rstrip (' \r \n ' )
158- text_to_insert = ' \r \n ' .join (text )
159- line = line [:m .start ()] + text_to_insert .strip () + line [m .end ():]
177+ text [i ] = text [i ].rstrip (" \r \n " )
178+ text_to_insert = " \r \n " .join (text )
179+ line = line [: m .start ()] + text_to_insert .strip () + line [m .end () :]
160180 del lines [loc ]
161- lines [loc :loc ] = line .split (' \r \n ' )
181+ lines [loc :loc ] = line .split (" \r \n " )
162182 m = INC_SYNTAX .search (line )
163183
164184 else :
165185 h = HEADING_SYNTAX .search (line )
166186 if h :
167187 headingDepth = len (h .group (0 ))
168- bonusHeading = '#' * headingDepth
169-
188+ bonusHeading = "#" * headingDepth
189+
170190 else :
171191 done = True
172192 return lines
173193
174194
175- def makeExtension (* args ,** kwargs ):
195+ def makeExtension (* args , ** kwargs ):
176196 return MarkdownInclude (kwargs )
0 commit comments