44Pandoc filter for including file in code block.
55"""
66
7- from panflute import CodeBlock , debug , run_filters
7+ from collections .abc import Iterable
8+ from typing import Any
89
10+ from panflute import CodeBlock , Doc , Element , debug , run_filters
911
10- def parse_include (parsed , name , value ):
12+
13+ def parse_include (parsed : dict [str , Any ], name : str , value : str ) -> None :
1114 """
1215 Extract include information from attributes.
1316
@@ -22,11 +25,11 @@ def parse_include(parsed, name, value):
2225 """
2326 if name == "include" :
2427 try :
25- with open (value , "r" , encoding = "utf-8" ) as stream :
28+ with open (value , encoding = "utf-8" ) as stream :
2629 file_content = stream .readlines ()
2730 parsed ["content" ] = file_content
2831 parsed ["include" ] = value
29- except IOError :
32+ except OSError :
3033 debug ("[WARNING] pandoc-codeblock-include: " + value + " not found" )
3134 except UnicodeDecodeError :
3235 debug (
@@ -36,7 +39,7 @@ def parse_include(parsed, name, value):
3639 )
3740
3841
39- def parse_start_from (parsed , name , value ) :
42+ def parse_start_from (parsed : dict [ str , Any ], name : str , value : str ) -> None :
4043 """
4144 Extract startFrom information from attributes.
4245
@@ -60,9 +63,9 @@ def parse_start_from(parsed, name, value):
6063 )
6164
6265
63- def parse_end_at (parsed , name , value ) :
66+ def parse_end_at (parsed : dict [ str , Any ], name : str , value : str ) -> None :
6467 """
65- Extract entAt information from attributes.
68+ Extract information from attributes.
6669
6770 Arguments
6871 ---------
@@ -84,7 +87,7 @@ def parse_end_at(parsed, name, value):
8487 )
8588
8689
87- def parse_attributes (items ) :
90+ def parse_attributes (items : Iterable [ tuple [ str , str ]]) -> dict [ str , str ] :
8891 """
8992 Extract usefull information from attributes.
9093
@@ -95,17 +98,18 @@ def parse_attributes(items):
9598
9699 Returns
97100 -------
101+ dict[str, str]
98102 a mapping containing possible 'content', 'start_from' and 'end_at'
99103 """
100- parsed = {}
104+ parsed : dict [ str , Any ] = {}
101105 for name , value in items :
102106 parse_include (parsed , name , value )
103107 parse_start_from (parsed , name , value )
104108 parse_end_at (parsed , name , value )
105109 return parsed
106110
107111
108- def inject_content (elem , parsed ) :
112+ def inject_content (elem : Element , parsed : dict [ str , Any ]) -> None :
109113 """
110114 Inject parsed attributes into element content.
111115
@@ -133,7 +137,7 @@ def inject_content(elem, parsed):
133137 )
134138
135139
136- def clear_latex_attributes (elem ) :
140+ def clear_latex_attributes (elem : Element ) -> None :
137141 """
138142 Clear LaTeX attributes.
139143
@@ -148,7 +152,7 @@ def clear_latex_attributes(elem):
148152 del elem .attributes [attribute ]
149153
150154
151- def include (elem , doc ) :
155+ def include (elem : Element , doc : Doc ) -> None :
152156 """
153157 Transform CodeBlock element.
154158
@@ -168,7 +172,7 @@ def include(elem, doc):
168172 clear_latex_attributes (elem )
169173
170174
171- def main (doc = None ):
175+ def main (doc : Doc | None = None ) -> Doc :
172176 """
173177 Convert the pandoc document.
174178
@@ -179,6 +183,7 @@ def main(doc=None):
179183
180184 Returns
181185 -------
186+ Doc
182187 The modified pandoc document.
183188 """
184189 return run_filters ([include ], doc = doc )
0 commit comments