Skip to content

Commit 54632b9

Browse files
committed
ENH/TST: format html helper + explicit html tests
1 parent 53754d5 commit 54632b9

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

blark/format.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import sys
99
from typing import Any, List, Optional, Tuple, Union
1010

11-
from blark import output
12-
11+
from . import output
1312
from . import transform as tf
1413
from .output import OutputBlock
1514
from .parse import ParseResult

blark/html.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

blark/tests/test_html.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import pathlib
5+
6+
import pytest
7+
8+
from ..html import format_file_as_html
9+
from . import conftest
10+
11+
parse_filenames = conftest.twincat_pou_filenames + conftest.structured_text_filenames
12+
13+
14+
@pytest.fixture(params=parse_filenames)
15+
def input_filename(request) -> pathlib.Path:
16+
if not os.path.exists(request.param):
17+
pytest.skip(f"File missing: {request.param}")
18+
return pathlib.Path(request.param)
19+
20+
21+
def test_format_html(input_filename: pathlib.Path) -> None:
22+
print(format_file_as_html(input_filename))

0 commit comments

Comments
 (0)