Skip to content

Commit

Permalink
Type hints (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
vb64 authored Nov 19, 2024
1 parent 5411450 commit 2f14f4c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions markdown_pdf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
"""Markdown to pdf converter based on markdown_it and fitz."""
import io
import typing
from markdown_it import MarkdownIt
import fitz


class Section:
"""Markdown section."""

def __init__(self, text, toc=True, root=".", paper_size="A4", borders=(36, 36, -36, -36)):
def __init__(
self,
text: str,
toc: bool = True,
root: str = ".",
paper_size: str = "A4",
borders: tuple = (36, 36, -36, -36)
):
"""Create md section with given properties."""
self.text = text
self.toc = toc
Expand All @@ -31,7 +39,7 @@ class MarkdownPdf:
"keywords": None,
}

def __init__(self, toc_level=6, mode='commonmark'):
def __init__(self, toc_level: int = 6, mode: str = 'commonmark'):
"""Create md -> pdf converter with given TOC level and mode of md parsing."""
self.toc_level = toc_level
self.toc = []
Expand All @@ -45,7 +53,7 @@ def __init__(self, toc_level=6, mode='commonmark'):
self.page = 0

@staticmethod
def recorder(elpos):
def _recorder(elpos):
"""Call function invoked during story.place() for making a TOC."""
if not elpos.open_close & 1: # only consider "open" items
return
Expand All @@ -60,7 +68,7 @@ def recorder(elpos):
elpos.rect[1], # top of written rectangle (use for TOC)
))

def add_section(self, section, user_css=None):
def add_section(self, section: Section, user_css: typing.Optional[str] = None) -> None:
"""Add markdown section to pdf."""
rect = fitz.paper_rect(section.paper_size)
where = rect + section.borders
Expand All @@ -70,11 +78,11 @@ def add_section(self, section, user_css=None):
self.page += 1
device = self.writer.begin_page(rect)
more, _ = story.place(where) # layout into allowed rectangle
story.element_positions(self.recorder, {"toc": section.toc, "pdfile": self})
story.element_positions(self._recorder, {"toc": section.toc, "pdfile": self})
story.draw(device)
self.writer.end_page()

def save(self, file_name):
def save(self, file_name: str) -> None:
"""Save pdf to file."""
self.writer.close()
doc = fitz.open("pdf", self.out_file)
Expand Down

0 comments on commit 2f14f4c

Please sign in to comment.