Skip to content

Commit

Permalink
Add new params to print_code_block
Browse files Browse the repository at this point in the history
  • Loading branch information
karelvaculik authored Sep 17, 2023
1 parent f100fca commit 4b0166d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pyreball/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ def print_div(
Args:
*values: Zero or more values to be printed into the div.
cl: One or more class names to be added to the tag.
cl: One or more class names to be added to the <div> tag.
If string is provided, it is used as it is.
If a list of strings is provided, the strings are joined with space.
If None, no class is added.
If an empty list is provided, class attribute is added with an empty string.
attrs: Additional attributes to be added to the tag.
attrs: Additional attributes to be added to the <div> tag.
Dictionary `{"key1": "value1", ..., "keyN": "valueN"}`
is converted to `key1="value1" ... keyN="valueN"`.
To construct boolean HTML attributes, set None for given key.
Expand Down Expand Up @@ -422,6 +422,8 @@ def print_code_block(
numbered: Optional[bool] = None,
cl: ClParameter = None,
attrs: AttrsParameter = None,
pre_cl: ClParameter = None,
pre_attrs: AttrsParameter = None,
sep: str = "",
end: str = "\n",
syntax_highlight: Optional[Literal["python"]] = "python",
Expand Down Expand Up @@ -458,6 +460,8 @@ def print_code_block(
is converted to `key1="value1" ... keyN="valueN"`.
To construct boolean HTML attributes, set None for given key.
Any quotes in values are not escaped.
pre_cl: The same as `cl` parameter, but for the `<pre>` tag.
pre_attrs: The same as `attrs` parameter, but for the `<pre>` tag.
sep: String separator of the values inside the tag. Defaults to an empty string.
end: String appended after the tag. Defaults to a newline.
syntax_highlight: Syntax highlighting language.
Expand All @@ -470,6 +474,8 @@ class "<language>" is added to the `<code>` element.
*values,
cl=cl,
attrs=attrs,
pre_cl=pre_cl,
pre_attrs=pre_attrs,
sep=sep,
syntax_highlight=syntax_highlight,
)
Expand Down
7 changes: 6 additions & 1 deletion pyreball/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def code_block(
*values: Any,
cl: ClParameter = None,
attrs: AttrsParameter = None,
pre_cl: ClParameter = None,
pre_attrs: AttrsParameter = None,
sep: str = "",
syntax_highlight: Optional[Literal["python"]] = "python",
) -> str:
Expand All @@ -160,11 +162,14 @@ def code_block(
If a list of strings is provided, the strings are joined with space.
If None, no class is added.
If an empty list is provided, class attribute is added with an empty string.
A class is also added when `syntax_highlight` is set.
attrs: Additional attributes to be added to the <code> tag.
Dictionary `{"key1": "value1", ..., "keyN": "valueN"}`
is converted to `key1="value1" ... keyN="valueN"`.
To construct boolean HTML attributes,
set None for given key. Any quotes in values are not escaped.
pre_cl: The same as `cl` parameter, but for the `<pre>` tag.
pre_attrs: The same as `attrs` parameter, but for the `<pre>` tag.
sep: String separator of the values. Defaults to an empty string.
syntax_highlight: Syntax highlighting language.
Currently only "python" is supported. If None, no highlight is applied.
Expand All @@ -176,7 +181,7 @@ def code_block(
"""
cl = _collect_classes_for_code_strings([], cl, syntax_highlight)
code_text = tag(*values, name="code", cl=cl, attrs=attrs, sep=sep)
return tag(code_text, name="pre")
return tag(code_text, name="pre", cl=pre_cl, attrs=pre_attrs)


def div(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ def test_code_block__with_syntax_highlight():
assert code_block("a", "b", sep="\n", syntax_highlight="python") == expected_result


def test_code_block__with_syntax_highlight_and_attributes():
expected_result = (
'<pre class="pre1" pa="pv">'
'<code class="code1 python" ca="cv">\na\nb\n</code>'
"</pre>"
)
result = code_block(
"a",
"b",
cl="code1",
attrs={"ca": "cv"},
pre_cl="pre1",
pre_attrs={"pa": "pv"},
sep="\n",
syntax_highlight="python",
)
assert result == expected_result


def test_code_block__unsupported_syntax_highlight():
with pytest.raises(ValueError):
code_block("a", "b", sep="\n", syntax_highlight="my_new_lang")
Expand Down

0 comments on commit 4b0166d

Please sign in to comment.