import re from conftest import Book, Doc PY = """def read_book(path): if path: return path.read_text(encoding="utf-8") # <utf-8> \treturn None""" def fenced_blocks(text: str) -> list[tuple[str, str]]: return re.findall(r"^(`{3,})([\w+-]*)\n(.*?)\n\1$", text, re.M | re.S) def test_python_indentation_and_special_chars(convert): body = f'
{PY}'
run = convert(Book(docs=[Doc("c.xhtml", body)]))
blocks = fenced_blocks(run.chapter("001"))
assert len(blocks) == 1
fence, lang, code = blocks[0]
assert lang == "python"
assert code == PY.replace("<", "<").replace(">", ">")
assert "<" not in code
def test_shell_code_with_pipes_backslashes_and_dollar(convert):
shell = "$ cat datei.txt | grep -v '^#' \\\n | sort && echo \"$HOME\" > out.txt"
body = f"{shell}"
run = convert(Book(docs=[Doc("c.xhtml", body)]))
(_, lang, code), = fenced_blocks(run.chapter("001"))
assert code == "$ cat datei.txt | grep -v '^#' \\\n | sort && echo \"$HOME\" > out.txt"
assert lang == "" # no reliable language signal -> no label
def test_backticks_in_code_get_longer_fence(convert):
body = "echo ```nested``` and `x`" run = convert(Book(docs=[Doc("c.xhtml", body)])) (fence, _, code), = fenced_blocks(run.chapter("001")) assert fence == "````" assert code == "echo ```nested``` and `x`" def test_br_lines_and_highlight_spans_in_pre(convert): body = ('
#include <stdio.h>
' 'int main(void) {
printf("Hallo\\n");
}
1a = 1' run = convert(Book(docs=[Doc("c.xhtml", body)])) (_, _, code), = fenced_blocks(run.chapter("001")) assert code == "a = 1\nb = 2" def test_inline_code_variants(convert): body = ('
2b = 2
Nutze a*b_c, x | y und `tick` '
'sowie Strg.
Ein normaler Absatz mit Leerzeichen.
" run = convert(Book(docs=[Doc("c.xhtml", body)])) assert "```" not in run.chapter("001") assert "Ein normaler Absatz mit Leerzeichen." in run.chapter("001") def test_unlabeled_code_is_reported(convert): run = convert(Book(docs=[Doc("c.xhtml", "foo bar")])) assert run.report["code_blocks"]["languages"] == {"(keine)": 1} def test_code_language_option_fills_only_unlabeled_blocks(convert): body = "
x = compute()
#include <stdio.h>\nint main(void) { return 0; }"
run = convert(Book(docs=[Doc("c.xhtml", body)]), "--code-language", "py")
langs = [lang for _, lang, _ in fenced_blocks(run.chapter("001"))]
assert langs == ["python", "c"]
by = {item["language"]: item["detected_by"] for item in run.report["code_blocks"]["items"]}
assert by == {"python": "option", "c": "content"}
def test_code_language_option_rejects_unknown_values(convert):
run = convert(Book(docs=[Doc("c.xhtml", "x")]), "--code-language", "quatsch") assert run.rc == 1 assert "quatsch" in run.stderr