Skip to content

Commit df3c3ec

Browse files
authored
The sections property of the MarkdownPdf class contains a list of added sections in the order in which they were added. (#98)
1 parent 69ea862 commit df3c3ec

6 files changed

Lines changed: 26 additions & 2 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ css = "table, th, td {border: 1px solid black;}"
101101
pdf.add_section(Section(text), user_css=css)
102102
```
103103

104+
The `sections` property of the `MarkdownPdf` class contains a list of added sections in the order in which they were added.
105+
106+
```python
107+
assert len(pdf.sections) > 1
108+
```
109+
104110
Set the properties of the pdf document.
105111

106112
```python

README_ru.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ from markdown_pdf import Section
4343

4444
section = Section("# Title\n", toc=False)
4545
assert section.page_count == 0
46+
4647
pdf.add_section(section)
4748
assert section.page_count == 1
4849
```
@@ -95,6 +96,12 @@ css = "table, th, td {border: 1px solid black;}"
9596
pdf.add_section(Section(text), user_css=css)
9697
```
9798

99+
Свойство `sections` кдасса `MarkdownPdf` содержит список секций в том порядке, в котором их добавляли.
100+
101+
```python
102+
assert len(pdf.sections) > 1
103+
```
104+
98105
Устанавливаем свойства pdf документа.
99106

100107
```python

history.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
+ Section.page_count contains number of pdf pages that has been generated for section after add_section call.
22

3+
+ The `sections` property of the `MarkdownPdf` class contains a list of added sections in the order in which they were added.
4+
35
12.02.2026 ver.1.12
46
-------------------
57

markdown_pdf/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def __init__(
7676
self.m_d = (MarkdownIt(mode).enable('table')) # Enable support for tables
7777

7878
self.optimize = optimize
79-
8079
self.out_file = io.BytesIO()
8180
self.writer = fitz.DocumentWriter(self.out_file)
8281
self.page_num = 0
8382
self.hrefs = []
83+
self.sections = []
8484

8585
@staticmethod
8686
def _recorder(elpos):
@@ -128,6 +128,8 @@ def add_section(self, section: Section, user_css: typing.Optional[str] = None) -
128128
story.draw(device)
129129
self.writer.end_page()
130130

131+
self.sections.append(section)
132+
131133
return html
132134

133135
def _make_doc(self):

tests/test/test_converter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ def test_hrefs(self):
8181
sect = Section(open(self.fixture("hrefs.md"), "rt", encoding='utf-8').read())
8282
assert sect.page_count == 0
8383
pdf = MarkdownPdf()
84+
assert not pdf.sections
8485
pdf.add_section(sect)
8586
assert sect.page_count == 1
87+
assert len(pdf.sections) == 1
8688

8789
pdf.save(self.build("hrefs.pdf"))
8890

tests/test/test_readme.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def test_en(self):
1313
from markdown_pdf import Section, MarkdownPdf
1414

1515
pdf = MarkdownPdf(toc_level=2, optimize=True)
16-
pdf.add_section(Section("# Title\n", toc=False))
16+
section = Section("# Title\n", toc=False)
17+
assert section.page_count == 0
18+
pdf.add_section(section)
19+
assert section.page_count == 1
1720

1821
text = """# Section with links
1922
@@ -43,6 +46,8 @@ def test_en(self):
4346

4447
pdf.save(self.build("readme.pdf"))
4548

49+
assert len(pdf.sections) == 5
50+
4651
def test_user_case_1(self):
4752
"""Test user case from discussion."""
4853
from markdown_pdf import Section, MarkdownPdf

0 commit comments

Comments
 (0)