diff --git a/README.md b/README.md index e7f85ab..2f4593d 100644 --- a/README.md +++ b/README.md @@ -42,11 +42,15 @@ pdf = MarkdownPdf(toc_level=2, optimize=True) ``` Add the first section to the pdf. The title is not included in the table of contents. +After adding a section to a pdf, the `page_count` property in the section contains the number of pdf pages created for the added section. ```python from markdown_pdf import Section -pdf.add_section(Section("# Title\n", toc=False)) +section = Section("# Title\n", toc=False) +assert section.page_count == 0 +pdf.add_section(section) +assert section.page_count == 1 ``` Add a second section with external and internal hyperlinks. diff --git a/README_ru.md b/README_ru.md index 5d7cd84..6589867 100644 --- a/README_ru.md +++ b/README_ru.md @@ -36,11 +36,15 @@ pdf = MarkdownPdf(toc_level=2) ``` Добавляем в pdf первую секцию. Заголовок не включаем в оглавление. +После добавления секции в pdf свойство `page_count` у секции содержит количество созданных страниц pdf для добавленной секции. ```python from markdown_pdf import Section -pdf.add_section(Section("# Title\n", toc=False)) +section = Section("# Title\n", toc=False) +assert section.page_count == 0 +pdf.add_section(section) +assert section.page_count == 1 ``` Добавляем вторую секцию с внешними и внутренними гипер-ссылками. diff --git a/history.txt b/history.txt index a5f64c3..e45a894 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,5 @@ ++ Section.page_count contains number of pdf pages that has been generated for section after add_section call. + 12.02.2026 ver.1.12 ------------------- diff --git a/markdown_pdf/__init__.py b/markdown_pdf/__init__.py index 5d99913..52d0cab 100644 --- a/markdown_pdf/__init__.py +++ b/markdown_pdf/__init__.py @@ -28,6 +28,7 @@ def __init__( self.text = text self.toc = toc self.root = root + self.page_count = 0 self.paper_size = paper_size if isinstance(paper_size, str): @@ -120,6 +121,7 @@ def add_section(self, section: Section, user_css: typing.Optional[str] = None) - more = 1 while more: # loop outputting the story self.page_num += 1 + section.page_count += 1 device = self.writer.begin_page(section.rect) more, _ = story.place(where) # layout into allowed rectangle story.element_positions(self._recorder, {"toc": section.toc, "pdfile": self}) diff --git a/tests/test/test_converter.py b/tests/test/test_converter.py index 488e6ac..711272a 100644 --- a/tests/test/test_converter.py +++ b/tests/test/test_converter.py @@ -78,10 +78,12 @@ def test_hrefs(self): """Convert hrefs content to pdf.""" from markdown_pdf import Section, MarkdownPdf + sect = Section(open(self.fixture("hrefs.md"), "rt", encoding='utf-8').read()) + assert sect.page_count == 0 pdf = MarkdownPdf() - pdf.add_section( - Section(open(self.fixture("hrefs.md"), "rt", encoding='utf-8').read()) - ) + pdf.add_section(sect) + assert sect.page_count == 1 + pdf.save(self.build("hrefs.pdf")) def test_bytes(self):