Skip to content

Commit

Permalink
type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
vb64 committed Nov 19, 2024
1 parent 5411450 commit 4e3aa31
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 0 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ valid-metaclass-classmethod-first-arg=mcs

# Maximum number of arguments for function / method
max-args=15
max-positional-arguments=10

# Maximum number of attributes for a class (see R0902).
max-attributes=15
Expand Down
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 4e3aa31

Please sign in to comment.