Skip to content

Commit

Permalink
Merge pull request #44 from daizutabi/43-baselinestretch
Browse files Browse the repository at this point in the history
Add verbatim options
  • Loading branch information
daizutabi committed Jun 16, 2024
2 parents e52da9a + 852d248 commit 34306ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
11 changes: 11 additions & 0 deletions examples/src/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ AAA=1

```python {.output}
AAA=1
BBB=1
```

```python {.output baselinestretch=1.05}
AAA=1
BBB=1
```

```python {.output}
AAA=1
BBB=1
```

after
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ python = ["3.10", "3.11", "3.12"]
[tool.pytest.ini_options]
addopts = [
"-vv",
"--capture=no",
"--color=yes",
"--cov-report=lcov:lcov.info",
"--cov=panpdf",
Expand Down
28 changes: 20 additions & 8 deletions src/panpdf/filters/verbatim.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from panflute import Doc, Element


CONFIG: dict[str, str] = {"fontsize": "\\small", "baselinestretch": "auto"}


@dataclass(repr=False)
class Verbatim(Filter):
types: ClassVar[type[CodeBlock]] = CodeBlock
Expand All @@ -25,12 +28,12 @@ def __post_init__(self) -> None:
def action(self, elem: CodeBlock, doc: Doc) -> CodeBlock | list[Element]: # noqa: ARG002
self.shaded = True

if "output" in elem.classes:
return create_output(elem)

if "title" in elem.attributes:
return create_title(elem)

if "output" in elem.classes:
return create_output(elem)

return create_default(elem)

def finalize(self, doc: Doc) -> None:
Expand Down Expand Up @@ -80,11 +83,20 @@ def vspace() -> RawBlock:
TITLE_COLOR = "NavyBlue"


def define_verbatim_environment(attrs: dict[str, str], fontsize: str = "small") -> str:
text = r"\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},"
text = f"{text}fontsize=\\{fontsize}"
args = ",".join(f"{k}={v}" for k, v in attrs.items())
return f"{text},{args}}}" if args else text + "}"
def define_verbatim_environment(attrs: dict[str, str]) -> str:
text = r"\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}"
args = []
for key, value in attrs.items():
args.append(f"{key}={value}")
if key in CONFIG:
CONFIG[key] = value

for key, value in CONFIG.items():
if key not in attrs:
args.append(f"{key}={value}")

args_str = ",".join(args)
return f"{text},{args_str}}}"


def define_shade_color(rgb: Iterable) -> str:
Expand Down
12 changes: 8 additions & 4 deletions tests/filters/test_verbatim.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def test_create_code_block():


def test_define_verbatim_environment():
from panpdf.filters.verbatim import define_verbatim_environment
from panpdf.filters.verbatim import CONFIG, define_verbatim_environment

text = define_verbatim_environment({}, "large")
assert text.endswith(r"{commandchars=\\\{\},fontsize=\large}")
config = CONFIG.copy()
text = define_verbatim_environment({"fontsize": "\\large"})
assert text.endswith(r"{commandchars=\\\{\},fontsize=\large,baselinestretch=auto}")
text = define_verbatim_environment({"a": "b"})
assert text.endswith(r"small,a=b}")
assert text.endswith(r"a=b,fontsize=\large,baselinestretch=auto}")
CONFIG.update(config)
text = define_verbatim_environment({})
assert text.endswith(r"{commandchars=\\\{\},fontsize=\small,baselinestretch=auto}")


def test_define_shade_color():
Expand Down

0 comments on commit 34306ff

Please sign in to comment.