1515# be outside of A4 page. So limit it to 700px.
1616MAX_SVG_HEIGHT = 700
1717
18+
1819class Index :
1920 """
2021 For storing the section number, image number and table number
@@ -116,8 +117,6 @@ def append_abstract(elem, doc):
116117 return res
117118
118119
119-
120-
121120def get_svg_height (filename : str ) -> int :
122121 """
123122 Get the height of svg file. If the height is greater then
@@ -131,13 +130,13 @@ def get_svg_height(filename: str) -> int:
131130 return int (MAX_SVG_HEIGHT )
132131
133132
134- def insert_cxx2flow_svg (code_block : pf .CodeBlock , title :str )-> Union [pf .Para ,None ]:
133+ def insert_cxx2flow_svg (code_block : pf .CodeBlock , title : str ) -> Union [pf .Para , None ]:
135134 """
136135 all cxx2flow and dot to get the svg flow chart. Then insert
137136 it to the document.
138137 """
139- if len (title )== 0 :
140- title = "程序流程图"
138+ if len (title ) == 0 :
139+ title = "程序流程图"
141140 elif title [0 ] == ":" :
142141 title = title [1 :]
143142 Index .image += 1
@@ -153,6 +152,9 @@ def insert_cxx2flow_svg(code_block: pf.CodeBlock, title:str)->Union[pf.Para,None
153152 stderr = PIPE ,
154153 )
155154 p .communicate (input = dot_data )
155+
156+ if not os .path .exists (output_file ):
157+ return None
156158 # limit svg's height
157159 height = get_svg_height (output_file )
158160 img = pf .Image (
@@ -162,49 +164,53 @@ def insert_cxx2flow_svg(code_block: pf.CodeBlock, title:str)->Union[pf.Para,None
162164 attributes = {"height" : f"{ height } px" },
163165 )
164166 img .walk (add_image_capition )
165- if os . path . exists ( output_file ):
166- return pf . Para ( img )
167+ return pf . Para ( img )
168+
167169
168170def process_report (elem , doc ):
169171 """
170172 Traverse the JSON AST provided by pandoc, and apply
171173 modifications such as styles, captions, reference, etc.
172174 """
173- if type (elem ) == pf .MetaMap :
174- meta : pf .MetaMap = cast (pf .MetaMap , elem )
175- meta .content ["subtitle" ].walk (append_abstract ) # type: ignore
176- elif type (elem ) == pf .Image :
177- img : pf .Image = cast (pf .Image , elem )
178- Index .image += 1
179- img .walk (add_image_capition )
180- return img
181- elif type (elem ) == pf .Table :
182- table : pf .Table = cast (pf .Table , elem )
183- Index .table += 1
184- number = pf .RawInline (
185- DocxTemplates .TABLE_NUMBER .format (
186- section_id = Index .get_section_id (level = 1 ), table_id = Index .table
187- ),
188- format = "openxml" ,
189- )
190- table .caption = pf .Caption (pf .Para (number , pf .Str (DocState .table_title )))
191- DocState .table_title = ""
192- return table
193- elif type (elem ) == pf .Para :
194- elem_str : pf .Para = cast (pf .Para , elem )
195- content = pf .stringify (elem_str )
196- # store the table caption and remove it from the doc.
197- if content .startswith ("#table " ):
198- DocState .table_title = content [7 :]
199- return pf .Para ()
200- elif type (elem ) == pf .Header :
201- header : pf .Header = cast (pf .Header , elem )
202- Index .add (header .level )
203- elif type (elem ) == pf .CodeBlock :
204- code_block : pf .CodeBlock = cast (pf .CodeBlock , elem )
205- lang = code_block .to_json ()["c" ][0 ][1 ]
206- if len (lang ) == 1 and lang [0 ].startswith ("cxx2flow" ):
207- return insert_cxx2flow_svg (code_block = code_block ,title = lang [0 ][8 :])
175+ try :
176+ if type (elem ) == pf .MetaMap :
177+ meta : pf .MetaMap = cast (pf .MetaMap , elem )
178+ meta .content ["subtitle" ].walk (append_abstract ) # type: ignore
179+ elif type (elem ) == pf .Image :
180+ img : pf .Image = cast (pf .Image , elem )
181+ Index .image += 1
182+ img .walk (add_image_capition )
183+ return img
184+ elif type (elem ) == pf .Table :
185+ table : pf .Table = cast (pf .Table , elem )
186+ Index .table += 1
187+ number = pf .RawInline (
188+ DocxTemplates .TABLE_NUMBER .format (
189+ section_id = Index .get_section_id (level = 1 ), table_id = Index .table
190+ ),
191+ format = "openxml" ,
192+ )
193+ table .caption = pf .Caption (pf .Para (number , pf .Str (DocState .table_title )))
194+ DocState .table_title = ""
195+ return table
196+ elif type (elem ) == pf .Para :
197+ elem_str : pf .Para = cast (pf .Para , elem )
198+ content = pf .stringify (elem_str )
199+ # store the table caption and remove it from the doc.
200+ if content .startswith ("#table " ):
201+ DocState .table_title = content [7 :]
202+ return pf .Para ()
203+ elif type (elem ) == pf .Header :
204+ header : pf .Header = cast (pf .Header , elem )
205+ Index .add (header .level )
206+ elif type (elem ) == pf .CodeBlock :
207+ code_block : pf .CodeBlock = cast (pf .CodeBlock , elem )
208+ lang = code_block .to_json ()["c" ][0 ][1 ]
209+ if len (lang ) == 1 and lang [0 ].startswith ("cxx2flow" ):
210+ return insert_cxx2flow_svg (code_block = code_block , title = lang [0 ][8 :])
211+ except Exception as e :
212+ pf .debug (f"Exception in general filter: { e } " )
213+
208214
209215
210216if __name__ == "__main__" :
0 commit comments