@@ -212,3 +212,38 @@ def _register():
212212 register_output_handler ("html" , HtmlWriter .save )
213213 register_output_handler (".htm" , HtmlWriter .save )
214214 register_output_handler (".html" , HtmlWriter .save )
215+
216+
217+ def format_file_as_html (
218+ input_filename : pathlib .Path | str ,
219+ * ,
220+ header : str = "<html><body>" ,
221+ footer : str = "</body></html>" ,
222+ ) -> str :
223+ """
224+ Helper for formatting a file as HTML.
225+
226+ Parameters
227+ ----------
228+ input_filename : pathlib.Path or str
229+ The source code filename. Any supported by `blark.parse`.
230+ header : str, optional
231+ HTML header to include in the output.
232+ Defaults to html and body opening tags.
233+ footer : str, optional
234+ HTML footer to include in the output.
235+ Defaults to html and body closing tags.
236+
237+ Note
238+ ----
239+ Users would typically format a file through blark's CLI by way of
240+ `blark format --output-format html`.
241+ """
242+ from .format import get_reformatted_code_blocks
243+ from .parse import parse
244+
245+ input_filename = pathlib .Path (input_filename )
246+ results = parse (input_filename )
247+ blocks = get_reformatted_code_blocks (list (results ), filename = input_filename )
248+ body = HtmlWriter .save (user = None , source_filename = input_filename , parts = blocks )
249+ return "" .join ((header , body , footer ))
0 commit comments