markdown-extractor 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- markdown_extractor/__init__.py +14 -0
- markdown_extractor/blocks.py +359 -0
- markdown_extractor/extractor.py +193 -0
- markdown_extractor/html_renderer.py +150 -0
- markdown_extractor/parser.py +153 -0
- markdown_extractor/py.typed +0 -0
- markdown_extractor/section.py +320 -0
- markdown_extractor/text_renderer.py +83 -0
- markdown_extractor-0.1.0.dist-info/METADATA +676 -0
- markdown_extractor-0.1.0.dist-info/RECORD +13 -0
- markdown_extractor-0.1.0.dist-info/WHEEL +5 -0
- markdown_extractor-0.1.0.dist-info/licenses/LICENSE +201 -0
- markdown_extractor-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: markdown-extractor
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Extract structured sections from Markdown by header — bracket-style access with robust handling of code blocks, tables, math, and YAML front matter.
|
|
5
|
+
Author-email: Fasil <fasilwdr@hotmail.com>
|
|
6
|
+
License: Apache License
|
|
7
|
+
Version 2.0, January 2004
|
|
8
|
+
http://www.apache.org/licenses/
|
|
9
|
+
|
|
10
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
11
|
+
|
|
12
|
+
1. Definitions.
|
|
13
|
+
|
|
14
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
15
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
16
|
+
|
|
17
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
18
|
+
the copyright owner that is granting the License.
|
|
19
|
+
|
|
20
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
21
|
+
other entities that control, are controlled by, or are under common
|
|
22
|
+
control with that entity. For the purposes of this definition,
|
|
23
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
24
|
+
direction or management of such entity, whether by contract or
|
|
25
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
26
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
27
|
+
|
|
28
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
29
|
+
exercising permissions granted by this License.
|
|
30
|
+
|
|
31
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
32
|
+
including but not limited to software source code, documentation
|
|
33
|
+
source, and configuration files.
|
|
34
|
+
|
|
35
|
+
"Object" form shall mean any form resulting from mechanical
|
|
36
|
+
transformation or translation of a Source form, including but
|
|
37
|
+
not limited to compiled object code, generated documentation,
|
|
38
|
+
and conversions to other media types.
|
|
39
|
+
|
|
40
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
41
|
+
Object form, made available under the License, as indicated by a
|
|
42
|
+
copyright notice that is included in or attached to the work
|
|
43
|
+
(an example is provided in the Appendix below).
|
|
44
|
+
|
|
45
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
46
|
+
form, that is based on (or derived from) the Work and for which the
|
|
47
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
48
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
49
|
+
of this License, Derivative Works shall not include works that remain
|
|
50
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
51
|
+
the Work and Derivative Works thereof.
|
|
52
|
+
|
|
53
|
+
"Contribution" shall mean any work of authorship, including
|
|
54
|
+
the original version of the Work and any modifications or additions
|
|
55
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
56
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
57
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
58
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
59
|
+
means any form of electronic, verbal, or written communication sent
|
|
60
|
+
to the Licensor or its representatives, including but not limited to
|
|
61
|
+
communication on electronic mailing lists, source code control systems,
|
|
62
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
63
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
64
|
+
excluding communication that is conspicuously marked or otherwise
|
|
65
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
66
|
+
|
|
67
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
68
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
69
|
+
subsequently incorporated within the Work.
|
|
70
|
+
|
|
71
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
72
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
73
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
74
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
75
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
76
|
+
Work and such Derivative Works in Source or Object form.
|
|
77
|
+
|
|
78
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
79
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
80
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
81
|
+
(except as stated in this section) patent license to make, have made,
|
|
82
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
83
|
+
where such license applies only to those patent claims licensable
|
|
84
|
+
by such Contributor that are necessarily infringed by their
|
|
85
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
86
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
87
|
+
institute patent litigation against any entity (including a
|
|
88
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
89
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
90
|
+
or contributory patent infringement, then any patent licenses
|
|
91
|
+
granted to You under this License for that Work shall terminate
|
|
92
|
+
as of the date such litigation is filed.
|
|
93
|
+
|
|
94
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
95
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
96
|
+
modifications, and in Source or Object form, provided that You
|
|
97
|
+
meet the following conditions:
|
|
98
|
+
|
|
99
|
+
(a) You must give any other recipients of the Work or
|
|
100
|
+
Derivative Works a copy of this License; and
|
|
101
|
+
|
|
102
|
+
(b) You must cause any modified files to carry prominent notices
|
|
103
|
+
stating that You changed the files; and
|
|
104
|
+
|
|
105
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
106
|
+
that You distribute, all copyright, patent, trademark, and
|
|
107
|
+
attribution notices from the Source form of the Work,
|
|
108
|
+
excluding those notices that do not pertain to any part of
|
|
109
|
+
the Derivative Works; and
|
|
110
|
+
|
|
111
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
112
|
+
distribution, then any Derivative Works that You distribute must
|
|
113
|
+
include a readable copy of the attribution notices contained
|
|
114
|
+
within such NOTICE file, excluding those notices that do not
|
|
115
|
+
pertain to any part of the Derivative Works, in at least one
|
|
116
|
+
of the following places: within a NOTICE text file distributed
|
|
117
|
+
as part of the Derivative Works; within the Source form or
|
|
118
|
+
documentation, if provided along with the Derivative Works; or,
|
|
119
|
+
within a display generated by the Derivative Works, if and
|
|
120
|
+
wherever such third-party notices normally appear. The contents
|
|
121
|
+
of the NOTICE file are for informational purposes only and
|
|
122
|
+
do not modify the License. You may add Your own attribution
|
|
123
|
+
notices within Derivative Works that You distribute, alongside
|
|
124
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
125
|
+
that such additional attribution notices cannot be construed
|
|
126
|
+
as modifying the License.
|
|
127
|
+
|
|
128
|
+
You may add Your own copyright statement to Your modifications and
|
|
129
|
+
may provide additional or different license terms and conditions
|
|
130
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
131
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
132
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
133
|
+
the conditions stated in this License.
|
|
134
|
+
|
|
135
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
136
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
137
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
138
|
+
this License, without any additional terms or conditions.
|
|
139
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
140
|
+
the terms of any separate license agreement you may have executed
|
|
141
|
+
with Licensor regarding such Contributions.
|
|
142
|
+
|
|
143
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
144
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
145
|
+
except as required for reasonable and customary use in describing the
|
|
146
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
147
|
+
|
|
148
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
149
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
150
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
151
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
152
|
+
implied, including, without limitation, any warranties or conditions
|
|
153
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
154
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
155
|
+
appropriateness of using or redistributing the Work and assume any
|
|
156
|
+
risks associated with Your exercise of permissions under this License.
|
|
157
|
+
|
|
158
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
159
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
160
|
+
unless required by applicable law (such as deliberate and grossly
|
|
161
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
162
|
+
liable to You for damages, including any direct, indirect, special,
|
|
163
|
+
incidental, or consequential damages of any character arising as a
|
|
164
|
+
result of this License or out of the use or inability to use the
|
|
165
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
166
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
167
|
+
other commercial damages or losses), even if such Contributor
|
|
168
|
+
has been advised of the possibility of such damages.
|
|
169
|
+
|
|
170
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
171
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
172
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
173
|
+
or other liability obligations and/or rights consistent with this
|
|
174
|
+
License. However, in accepting such obligations, You may act only
|
|
175
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
176
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
177
|
+
defend, and hold each Contributor harmless for any liability
|
|
178
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
179
|
+
of your accepting any such warranty or additional liability.
|
|
180
|
+
|
|
181
|
+
END OF TERMS AND CONDITIONS
|
|
182
|
+
|
|
183
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
184
|
+
|
|
185
|
+
To apply the Apache License to your work, attach the following
|
|
186
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
187
|
+
replaced with your own identifying information. (Don't include
|
|
188
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
189
|
+
comment syntax for the file format. We also recommend that a
|
|
190
|
+
file or class name and description of purpose be included on the
|
|
191
|
+
same "printed page" as the copyright notice for easier
|
|
192
|
+
identification within third-party archives.
|
|
193
|
+
|
|
194
|
+
Copyright [yyyy] [name of copyright owner]
|
|
195
|
+
|
|
196
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
197
|
+
you may not use this file except in compliance with the License.
|
|
198
|
+
You may obtain a copy of the License at
|
|
199
|
+
|
|
200
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
201
|
+
|
|
202
|
+
Unless required by applicable law or agreed to in writing, software
|
|
203
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
204
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
205
|
+
See the License for the specific language governing permissions and
|
|
206
|
+
limitations under the License.
|
|
207
|
+
|
|
208
|
+
Project-URL: Homepage, https://github.com/fasilwdr/MD-Extractor
|
|
209
|
+
Project-URL: Documentation, https://github.com/fasilwdr/MD-Extractor#readme
|
|
210
|
+
Project-URL: Source, https://github.com/fasilwdr/MD-Extractor
|
|
211
|
+
Project-URL: Issues, https://github.com/fasilwdr/MD-Extractor/issues
|
|
212
|
+
Project-URL: Changelog, https://github.com/fasilwdr/MD-Extractor/releases
|
|
213
|
+
Keywords: markdown,parser,extractor,headers,sections,ast
|
|
214
|
+
Classifier: Development Status :: 4 - Beta
|
|
215
|
+
Classifier: Intended Audience :: Developers
|
|
216
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
217
|
+
Classifier: Operating System :: OS Independent
|
|
218
|
+
Classifier: Programming Language :: Python :: 3
|
|
219
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
220
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
221
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
222
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
223
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
224
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
225
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
226
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
227
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
228
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
229
|
+
Classifier: Typing :: Typed
|
|
230
|
+
Requires-Python: >=3.8
|
|
231
|
+
Description-Content-Type: text/markdown
|
|
232
|
+
License-File: LICENSE
|
|
233
|
+
Provides-Extra: dev
|
|
234
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
235
|
+
Requires-Dist: lxml>=4.9; extra == "dev"
|
|
236
|
+
Requires-Dist: build; extra == "dev"
|
|
237
|
+
Requires-Dist: twine; extra == "dev"
|
|
238
|
+
Provides-Extra: xpath
|
|
239
|
+
Requires-Dist: lxml>=4.9; extra == "xpath"
|
|
240
|
+
Dynamic: license-file
|
|
241
|
+
|
|
242
|
+
<p align="center">
|
|
243
|
+
<img src="https://raw.githubusercontent.com/fasilwdr/MD-Extractor/main/assets/markdown-extractor.svg" alt="markdown-extractor logo">
|
|
244
|
+
</p>
|
|
245
|
+
|
|
246
|
+
# markdown-extractor
|
|
247
|
+
|
|
248
|
+
Turn a Markdown document into a navigable tree of sections keyed by header,
|
|
249
|
+
then drop into the body of any section to get its blocks, plain text, JSON,
|
|
250
|
+
or HTML — with optional XPath filtering.
|
|
251
|
+
|
|
252
|
+
- **Header tree** — bracket access, recursive search, ASCII tree.
|
|
253
|
+
- **Block tree** — paragraphs, ordered/unordered lists with nesting, code
|
|
254
|
+
fences, blockquotes — parsed lazily per section.
|
|
255
|
+
- **Renderers** — `to_list()`, `to_dict()`, `to_json()`, `to_html()`,
|
|
256
|
+
`to_text()`, all on both `MDExtractor` and any `Section`.
|
|
257
|
+
- **Robust parsing** — headers inside code blocks, tables, math blocks, and
|
|
258
|
+
YAML front matter are correctly ignored.
|
|
259
|
+
- **Zero runtime dependencies** for everything except XPath (opt-in extra).
|
|
260
|
+
- **Pure Python**, `>= 3.8`. Lazy slicing — large documents stay cheap.
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## Installation
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
pip install markdown-extractor
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
For XPath support on `to_html()`:
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
pip install markdown-extractor[xpath]
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
From a local checkout:
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
pip install -e .
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## Quick start
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
from markdown_extractor import MDExtractor
|
|
288
|
+
|
|
289
|
+
md = """
|
|
290
|
+
# Section 1
|
|
291
|
+
Some content here.
|
|
292
|
+
|
|
293
|
+
- **Lightweight** — small footprint.
|
|
294
|
+
- *Flexible* — extensible by design.
|
|
295
|
+
- `Tested` — full coverage.
|
|
296
|
+
|
|
297
|
+
## Subsection 1.1
|
|
298
|
+
More details.
|
|
299
|
+
|
|
300
|
+
## FAQ
|
|
301
|
+
- **Which versions are supported?**
|
|
302
|
+
|
|
303
|
+
Versions 1.0 and up.
|
|
304
|
+
- **Where do I report bugs?**
|
|
305
|
+
|
|
306
|
+
Open an issue on GitHub.
|
|
307
|
+
"""
|
|
308
|
+
|
|
309
|
+
e = MDExtractor(md)
|
|
310
|
+
|
|
311
|
+
e["Section 1"] # bracket access by title
|
|
312
|
+
e["Section 1"]["Subsection 1.1"] # nested
|
|
313
|
+
e.get("Section 1", "Subsection 1.1") # multi-step (soft — see below)
|
|
314
|
+
e.list() # ['Section 1']
|
|
315
|
+
e[""] # synthetic root: the whole document
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
Every example below uses this same `md`. Load straight from disk instead:
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
e = MDExtractor.from_file("docs/guide.md")
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
## Navigating the header tree
|
|
327
|
+
|
|
328
|
+
### Strict access — `[]` / `get_section()`
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
e["Section 1"] # KeyError if missing
|
|
332
|
+
e.get_section("Section 1", "Sub 1.1") # KeyError if any title is missing
|
|
333
|
+
"Section 1" in e
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Soft access — `get()`
|
|
337
|
+
|
|
338
|
+
`get()` walks the same path but returns a **null section sentinel** instead
|
|
339
|
+
of raising when something is missing. The sentinel is falsy and its
|
|
340
|
+
renderer methods all return empty values, so chains stay safe:
|
|
341
|
+
|
|
342
|
+
```python
|
|
343
|
+
e.get("Section 1", "Subsection 1.1").to_list() # works
|
|
344
|
+
e.get("Nope", "Anything").to_list() # [] — no exception
|
|
345
|
+
e.get("Nope").to_html() # ""
|
|
346
|
+
e.get("Nope").to_dict() # {'title':'', 'level':0, ...}
|
|
347
|
+
|
|
348
|
+
if not e.get("Optional Section"):
|
|
349
|
+
print("section absent")
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
Use `[]`/`get_section()` when you want missing keys to fail loudly; use
|
|
353
|
+
`get()` when you want to flow through to an empty result.
|
|
354
|
+
|
|
355
|
+
### Discovery
|
|
356
|
+
|
|
357
|
+
```python
|
|
358
|
+
e.list() # immediate child titles
|
|
359
|
+
e["Section 1"].list() # children of "Section 1"
|
|
360
|
+
e.find("Subsection 1.1") # every section with that title
|
|
361
|
+
e.walk() # depth-first iterator over every header
|
|
362
|
+
e.tree() # ASCII tree of the whole document
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
367
|
+
## Reading a section's body
|
|
368
|
+
|
|
369
|
+
Every `Section` parses its own body lazily into a tree of blocks
|
|
370
|
+
(paragraphs / lists / list items / code / blockquote). This view is what
|
|
371
|
+
powers `to_list`, `to_dict`, `to_html`, and `to_text`.
|
|
372
|
+
|
|
373
|
+
```python
|
|
374
|
+
s = e["Section 1"]
|
|
375
|
+
|
|
376
|
+
s.blocks # the parsed Block tree (lazy, cached)
|
|
377
|
+
s.text # raw prose, no header line, no subsections
|
|
378
|
+
s.body # raw prose + nested subsections
|
|
379
|
+
s.content # the full slice including the header line
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Reaching a single block — `block()` and `text_plain`
|
|
383
|
+
|
|
384
|
+
When you've isolated a single piece of text inside the block tree,
|
|
385
|
+
`Block.text_plain` strips the inline Markdown markers so you don't have
|
|
386
|
+
to round-trip through `to_text()`:
|
|
387
|
+
|
|
388
|
+
```python
|
|
389
|
+
s.blocks[1].children[1].text # '*Flexible* — extensible by design.'
|
|
390
|
+
s.blocks[1].children[1].text_plain # 'Flexible — extensible by design.'
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
`block.text` is kept raw on purpose — `to_dict()` / `to_json()` is a
|
|
394
|
+
lossless round-trip of the source. `text_plain` is the on-demand
|
|
395
|
+
plain-text view.
|
|
396
|
+
|
|
397
|
+
For chains that might break (a missing list item, an empty section),
|
|
398
|
+
use `Section.block(*indices)` and `Block.get(*indices)`. These return
|
|
399
|
+
a *null Block* on out-of-range indices instead of raising
|
|
400
|
+
`IndexError` — so the chain stays safe end-to-end:
|
|
401
|
+
|
|
402
|
+
```python
|
|
403
|
+
s.block(1, 1).text_plain # 'Flexible — extensible by design.'
|
|
404
|
+
s.block(99, 99).text_plain # '' — no exception
|
|
405
|
+
|
|
406
|
+
# Equivalent two-step form (block() walks .blocks; .get() walks .children):
|
|
407
|
+
s.block(1).get(1).text_plain
|
|
408
|
+
|
|
409
|
+
bool(s.block(99)) # False — null sentinel
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
`section.blocks[i]` / `block.children[j]` keep raising `IndexError` on
|
|
413
|
+
out-of-range — strict access stays strict. Use `block()` / `.get()` only
|
|
414
|
+
when you want soft fall-through.
|
|
415
|
+
|
|
416
|
+
### `to_list()` — flatten body to strings
|
|
417
|
+
|
|
418
|
+
One entry per top-level block. Lists expand to one entry per top-level
|
|
419
|
+
item; `text` is preserved raw (use `text_plain` per-item if you want
|
|
420
|
+
markers stripped):
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
s.to_list()
|
|
424
|
+
# ['Some content here.',
|
|
425
|
+
# '**Lightweight** — small footprint.',
|
|
426
|
+
# '*Flexible* — extensible by design.',
|
|
427
|
+
# '`Tested` — full coverage.']
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
### `to_dict()` / `to_json()` — full structured output
|
|
431
|
+
|
|
432
|
+
Header subsections live under `children`; the body block tree lives under
|
|
433
|
+
`blocks`. Indented continuation paragraphs under a bullet (FAQ-style)
|
|
434
|
+
are attached as that bullet's `children`:
|
|
435
|
+
|
|
436
|
+
```python
|
|
437
|
+
e["Section 1"]["FAQ"].to_dict()
|
|
438
|
+
# {
|
|
439
|
+
# "title": "FAQ",
|
|
440
|
+
# "level": 2,
|
|
441
|
+
# "text": "...",
|
|
442
|
+
# "blocks": [
|
|
443
|
+
# {"kind": "list", "text": "", "children": [
|
|
444
|
+
# {"kind": "list_item",
|
|
445
|
+
# "text": "**Which versions are supported?**",
|
|
446
|
+
# "children": [
|
|
447
|
+
# {"kind": "paragraph", "text": "Versions 1.0 and up."}
|
|
448
|
+
# ]},
|
|
449
|
+
# {"kind": "list_item",
|
|
450
|
+
# "text": "**Where do I report bugs?**",
|
|
451
|
+
# "children": [
|
|
452
|
+
# {"kind": "paragraph", "text": "Open an issue on GitHub."}
|
|
453
|
+
# ]}
|
|
454
|
+
# ]}
|
|
455
|
+
# ],
|
|
456
|
+
# "children": []
|
|
457
|
+
# }
|
|
458
|
+
|
|
459
|
+
e["Section 1"]["FAQ"].to_json(indent=2)
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### `to_text()` — Markdown stripped
|
|
463
|
+
|
|
464
|
+
Inline markers (`**bold**`, `*em*`, `` `code` ``, `[link](url)`,
|
|
465
|
+
``) are reduced to their visible text. Bullets become
|
|
466
|
+
`- ` lines, ordered items become `1. `, nested children indent four
|
|
467
|
+
spaces, and fenced code is kept verbatim:
|
|
468
|
+
|
|
469
|
+
```python
|
|
470
|
+
print(s.to_text())
|
|
471
|
+
# Some content here.
|
|
472
|
+
#
|
|
473
|
+
# - Lightweight — small footprint.
|
|
474
|
+
# - Flexible — extensible by design.
|
|
475
|
+
# - Tested — full coverage.
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
### `to_html()` — render to HTML, optionally filter with XPath
|
|
479
|
+
|
|
480
|
+
```python
|
|
481
|
+
s.to_html()
|
|
482
|
+
# <p>Some content here.</p>
|
|
483
|
+
# <ul>
|
|
484
|
+
# <li><strong>Lightweight</strong> — small footprint.</li>
|
|
485
|
+
# <li><em>Flexible</em> — extensible by design.</li>
|
|
486
|
+
# <li><code>Tested</code> — full coverage.</li>
|
|
487
|
+
# </ul>
|
|
488
|
+
|
|
489
|
+
s.to_html(xpath=".//ul/li")
|
|
490
|
+
# ['<li><strong>Lightweight</strong> — small footprint.</li>',
|
|
491
|
+
# '<li><em>Flexible</em> — extensible by design.</li>',
|
|
492
|
+
# '<li><code>Tested</code> — full coverage.</li>']
|
|
493
|
+
|
|
494
|
+
s.to_html(xpath=".//strong")
|
|
495
|
+
# ['<strong>Lightweight</strong>']
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
#### Getting just the text value
|
|
499
|
+
|
|
500
|
+
When you want the *data* inside the matched elements rather than the
|
|
501
|
+
markup, you have two options:
|
|
502
|
+
|
|
503
|
+
**1. `as_text=True`** — flatten each match to its text content, including
|
|
504
|
+
text nested inside inline tags (`<strong>`, `<em>`, `<code>`, …):
|
|
505
|
+
|
|
506
|
+
```python
|
|
507
|
+
s.to_html(xpath=".//ul/li", as_text=True)
|
|
508
|
+
# ['Lightweight — small footprint.',
|
|
509
|
+
# 'Flexible — extensible by design.',
|
|
510
|
+
# 'Tested — full coverage.']
|
|
511
|
+
|
|
512
|
+
s.to_html(xpath=".//ul/li[1]", as_text=True)
|
|
513
|
+
# ['Lightweight — small footprint.']
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
**2. `/text()` in the XPath itself** — works without the `as_text` flag,
|
|
517
|
+
but only collects *direct* text nodes. Text wrapped in inline tags is
|
|
518
|
+
skipped:
|
|
519
|
+
|
|
520
|
+
```python
|
|
521
|
+
s.to_html(xpath=".//ul/li/text()")
|
|
522
|
+
# [' — small footprint.',
|
|
523
|
+
# ' — extensible by design.',
|
|
524
|
+
# ' — full coverage.']
|
|
525
|
+
# Note: 'Lightweight' / 'Flexible' / 'Tested' are missing — they sit
|
|
526
|
+
# inside <strong>/<em>/<code>, which /text() doesn't enter.
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
Use `as_text=True` when items contain inline formatting; use `/text()`
|
|
530
|
+
when you specifically want only the loose text and not the wrapped
|
|
531
|
+
content.
|
|
532
|
+
|
|
533
|
+
XPath uses `lxml` and is opt-in via the `[xpath]` extra:
|
|
534
|
+
|
|
535
|
+
```bash
|
|
536
|
+
pip install markdown-extractor[xpath]
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
Without `lxml`, plain `to_html()` still works — only `to_html(xpath=...)`
|
|
540
|
+
raises `ModuleNotFoundError` with the install hint.
|
|
541
|
+
|
|
542
|
+
---
|
|
543
|
+
|
|
544
|
+
## Robust extraction
|
|
545
|
+
|
|
546
|
+
The header parser walks the document with full block-context awareness,
|
|
547
|
+
so a stray `#` is never mistaken for a header.
|
|
548
|
+
|
|
549
|
+
| Block | Example | Behaviour |
|
|
550
|
+
|-------|---------|-----------|
|
|
551
|
+
| Fenced code | ```` ``` ```` … ```` ``` ```` (or `~~~`) | Headers inside are ignored |
|
|
552
|
+
| Math block | `$$` … `$$` | Headers inside are ignored |
|
|
553
|
+
| Tables | `\| col \| col \|` rows | Cell contents are ignored |
|
|
554
|
+
| YAML front matter | `---` at line 1, closes on `---`/`...` | Whole block is ignored |
|
|
555
|
+
|
|
556
|
+
```markdown
|
|
557
|
+
---
|
|
558
|
+
title: My Doc
|
|
559
|
+
# not a real header
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
# Real Header
|
|
563
|
+
|
|
564
|
+
```python
|
|
565
|
+
# also not a header
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
$$
|
|
569
|
+
# definitely not a header
|
|
570
|
+
$$
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
```python
|
|
574
|
+
MDExtractor(md).list() # ['Real Header']
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
ATX headers (`#` … `######`) and Setext underlines (`===` / `---`) are
|
|
578
|
+
both recognised. Skip-level jumps (h1 → h3 → h2) are handled gracefully.
|
|
579
|
+
Any leading whitespace is allowed before a header.
|
|
580
|
+
|
|
581
|
+
---
|
|
582
|
+
|
|
583
|
+
## Slices for any granularity
|
|
584
|
+
|
|
585
|
+
Each `Section` exposes three text views:
|
|
586
|
+
|
|
587
|
+
| Property | Includes header line? | Includes child sections? |
|
|
588
|
+
|----------|-----------------------|--------------------------|
|
|
589
|
+
| `content` | yes | yes |
|
|
590
|
+
| `body` | no | yes |
|
|
591
|
+
| `text` | no | no — own prose only |
|
|
592
|
+
|
|
593
|
+
---
|
|
594
|
+
|
|
595
|
+
## API reference
|
|
596
|
+
|
|
597
|
+
### `MDExtractor`
|
|
598
|
+
|
|
599
|
+
| Member | Description |
|
|
600
|
+
|--------|-------------|
|
|
601
|
+
| `MDExtractor(markdown)` | Parse a string. |
|
|
602
|
+
| `MDExtractor.from_file(path, encoding="utf-8")` | Read & parse a file. |
|
|
603
|
+
| `e[""]` / `e.root` | Synthetic root section (whole document). |
|
|
604
|
+
| `e["Title"]` / `e[i]` | Top-level child by title or index. |
|
|
605
|
+
| `"Title" in e` | Membership test. |
|
|
606
|
+
| `iter(e)` / `len(e)` | Iterate top-level children / count them. |
|
|
607
|
+
| `.list()` | Top-level header titles. |
|
|
608
|
+
| `.get_section(*path)` | Strict multi-step descent (raises). |
|
|
609
|
+
| `.get(*path)` | Soft multi-step descent (null sentinel on miss). |
|
|
610
|
+
| `.find(title)` | All sections (any depth) with that title. |
|
|
611
|
+
| `.walk()` / `.headers()` | Depth-first iterator / list of every header. |
|
|
612
|
+
| `.tree()` | ASCII tree of the document's header structure. |
|
|
613
|
+
| `.to_list()` | Body flattened to strings (proxies to root). |
|
|
614
|
+
| `.to_dict()` / `.to_json(**kw)` | Serialise the tree (with body blocks). |
|
|
615
|
+
| `.to_text()` | Body rendered as plain text. |
|
|
616
|
+
| `.to_html(xpath=None, as_text=False)` | Body rendered as HTML, optionally XPath-filtered (`as_text=True` returns text values). |
|
|
617
|
+
| `.block(*indices)` | Soft index into root's body block tree (null Block on miss). |
|
|
618
|
+
| `.content` | Original Markdown source. |
|
|
619
|
+
|
|
620
|
+
### `Section`
|
|
621
|
+
|
|
622
|
+
| Member | Description |
|
|
623
|
+
|--------|-------------|
|
|
624
|
+
| `.title` / `.level` | Header text and depth (1–6, or 0 for root). |
|
|
625
|
+
| `.parent` / `.children` | Tree links. |
|
|
626
|
+
| `.path` | Title chain from top-level ancestor down to this node. |
|
|
627
|
+
| `.content` / `.body` / `.text` | Raw text views (see table above). |
|
|
628
|
+
| `.blocks` | Lazy-parsed body block tree. |
|
|
629
|
+
| `section["Title"]` / `section[i]` | Child by title or index (strict). |
|
|
630
|
+
| `section.get(*path)` | Soft multi-step descent (null sentinel on miss). |
|
|
631
|
+
| `section.get_section(*path)` | Strict multi-step descent. |
|
|
632
|
+
| `"Title" in section` | Membership test. |
|
|
633
|
+
| `iter(section)` / `len(section)` | Iterate / count direct children. |
|
|
634
|
+
| `bool(section)` | `False` only for the null sentinel returned by `get()`. |
|
|
635
|
+
| `.list()` | Direct child titles. |
|
|
636
|
+
| `.find(title)` | Recursive search. |
|
|
637
|
+
| `.walk()` | Depth-first iterator over self + descendants. |
|
|
638
|
+
| `.to_list()` | Body flattened to one string per top-level block / item. |
|
|
639
|
+
| `.to_dict()` | Nested dict — `blocks` (body) and `children` (header subsections). |
|
|
640
|
+
| `.to_json(**kw)` | `json.dumps` of `to_dict()`. |
|
|
641
|
+
| `.to_text()` | Body rendered as plain text (Markdown markers stripped). |
|
|
642
|
+
| `.to_html(xpath=None, as_text=False)` | Body rendered as HTML, optionally XPath-filtered (`as_text=True` returns text values). |
|
|
643
|
+
| `.block(*indices)` | Soft index walk into the body block tree (null Block on miss). |
|
|
644
|
+
| `.tree()` | ASCII tree of this subsection. |
|
|
645
|
+
| `str(section)` | Same as `.content`. |
|
|
646
|
+
|
|
647
|
+
### `Block`
|
|
648
|
+
|
|
649
|
+
A node in the body block tree.
|
|
650
|
+
|
|
651
|
+
| Member | Description |
|
|
652
|
+
|--------|-------------|
|
|
653
|
+
| `.kind` | One of `paragraph`, `list`, `ordered_list`, `list_item`, `code`, `blockquote`. |
|
|
654
|
+
| `.text` | The block's own text — raw, with inline Markdown markers preserved. |
|
|
655
|
+
| `.text_plain` | `.text` with inline markers stripped (`**bold**` → `bold`, `[t](u)` → `t`, …). |
|
|
656
|
+
| `.children` | Nested blocks (list items, sub-lists, indented paragraphs). |
|
|
657
|
+
| `.info` | Code-fence language, e.g. `"python"`. |
|
|
658
|
+
| `.get(*indices)` | Soft index walk into `.children` (null Block on miss). Chainable. |
|
|
659
|
+
| `bool(block)` | `False` only for the null sentinel returned by `get()` / `Section.block()`. |
|
|
660
|
+
| `.walk()` | Yield this block and every descendant. |
|
|
661
|
+
| `.to_dict()` | JSON-friendly nested dict. |
|
|
662
|
+
|
|
663
|
+
---
|
|
664
|
+
|
|
665
|
+
## Development
|
|
666
|
+
|
|
667
|
+
```bash
|
|
668
|
+
pip install -e .[dev]
|
|
669
|
+
pytest
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
---
|
|
673
|
+
|
|
674
|
+
## License
|
|
675
|
+
|
|
676
|
+
See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
markdown_extractor/__init__.py,sha256=vjSYfc9p3GiUxtuw8ythtqNIExt9gcBgQJn7Lo-pWVQ,500
|
|
2
|
+
markdown_extractor/blocks.py,sha256=S4r2FWZpZp6Z-Hn3Ijn8tD7HRXcOAY_5A7r4-DhyLGo,12026
|
|
3
|
+
markdown_extractor/extractor.py,sha256=PPQHqNOr_QxMkuf52tZp1DKUhxcQjNy3yS9OV8tDpK4,6572
|
|
4
|
+
markdown_extractor/html_renderer.py,sha256=o6-uqQjz5ej0LNzODP8j7L-oPQOEKtTAHFsYpTGtcuk,5673
|
|
5
|
+
markdown_extractor/parser.py,sha256=1m_E5u3-LKisZDtnatx1SJGtZtn0PA5zoZUgGsk1eWw,5189
|
|
6
|
+
markdown_extractor/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
markdown_extractor/section.py,sha256=7a_wzejyqIhnJUhGAa_xGF3Afkeerd9zpt7lHTIiXT0,11598
|
|
8
|
+
markdown_extractor/text_renderer.py,sha256=tlWqRv7S9DTQ9fwo87uCV7QRSN1a-hWB04Njy4o1C4A,3083
|
|
9
|
+
markdown_extractor-0.1.0.dist-info/licenses/LICENSE,sha256=UOZ1F5fFDe3XXvG4oNnkL1-Ecun7zpHzRxjp-XsMeAo,11324
|
|
10
|
+
markdown_extractor-0.1.0.dist-info/METADATA,sha256=bpxy5J1GBrHYdSOfan5MzZYgT2rAeApb4rH8b7qfhKQ,28437
|
|
11
|
+
markdown_extractor-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
12
|
+
markdown_extractor-0.1.0.dist-info/top_level.txt,sha256=MpTrjC_HWmXaP_qEGNGmtodica8PkFIvqeQ1M3Cae-o,19
|
|
13
|
+
markdown_extractor-0.1.0.dist-info/RECORD,,
|