DocumentStructureParser
On this page
Module: chunks.document_structure_parser
Import: from zerda.chunks import DocumentStructureParser, StructuredSection
Table of Contents
- Purpose
- StructuredSection
- DocumentStructureParser Constructor
- Public Methods
- Parser Internals
- Examples
Purpose
DocumentStructureParser converts raw text (Markdown, HTML, or plain text) into a list of StructuredSection objects, each representing a logical document unit: heading, paragraph, code block, table, list, etc.
It is used internally by StructureAwareChunker but can be used standalone.
StructuredSection
@dataclass
class StructuredSection:
content: str
level: int = 0 # Heading level (0=body, 1=H1, 2=H2, ...)
section_type: str = "text" # "heading", "code", "table", "list", "text"
is_code_block: bool = False
is_table: bool = False
is_list: bool = False
parent_heading: str = "" # Text of parent heading section
extra: Dict[str, Any] = field(default_factory=dict)
Fields
Constructor
DocumentStructureParser()
No parameters. Instantiate once and reuse across multiple parse() calls.
Public Methods
detect_document_type
def detect_document_type(self, text: str) -> DocumentType
Detects the document type from text heuristics.
Returns: DocumentType enum value — HTML, MARKDOWN, ARABIC, or PLAIN_TEXT.
parse
def parse(
self,
text: str,
doc_type: Optional[DocumentType] = None,
) -> List[StructuredSection]
Parses text into a list of StructuredSection objects.
If doc_type is None, detect_document_type() is called automatically.
Returns: List[StructuredSection] — may be empty for empty input.
Markdown Parsing
- Scans lines sequentially.
- Detects fenced code blocks (
```) — wraps entire block asis_code_block=True. - Detects headings (
#,##, etc.) — wraps assection_type="heading"withlevel=count("#"). - Accumulates remaining lines into paragraph sections.
- Splits large paragraph blocks on blank lines.
HTML Parsing
If beautifulsoup4 is installed:
- Uses
BeautifulSoup(text, "html.parser"). - Walks the DOM: extracts
<h1>–<h6>as headings,<p>as text,<pre>/<code>as code blocks,<table>as tables,<ul>/<ol>as lists.
If beautifulsoup4 is not installed:
- Falls back to regex tag stripper: removes all HTML tags, then treats result as plain text.
Plain Text / Arabic Parsing
Splits on double newlines into paragraphs, wraps each as a StructuredSection with section_type="text".
Examples
Basic Markdown Parsing
from zerda.chunks import DocumentStructureParser
parser = DocumentStructureParser()
md = """
# Introduction
Welcome to the guide.
## Installation
```bash
pip install chunks"""
sections = parser.parse(md)
for s in sections:
print(s.section_type, s.level, repr(s.content[:40]))
Expected output:
heading 1 '# Introduction'
text 0 'Welcome to the guide.'
heading 2 '## Installation'
code 0 '```bash\npip install chunks\n```'
Type Detection
from zerda.chunks import DocumentStructureParser
parser = DocumentStructureParser()
print(parser.detect_document_type("<html><body>...</body></html>")) # DocumentType.HTML
print(parser.detect_document_type("# Title\n## Section")) # DocumentType.MARKDOWN
print(parser.detect_document_type("مرحباً بالعالم")) # DocumentType.ARABIC
print(parser.detect_document_type("Plain text.")) # DocumentType.PLAIN_TEXT
HTML Parsing
html = """
<html><body>
<h1>Getting Started</h1>
<p>Welcome to the tutorial.</p>
<pre><code>print("hello")</code></pre>
</body></html>
"""
sections = parser.parse(html)
for s in sections:
print(s.section_type, s.is_code_block, s.content[:30])
Forced Type
# Force plain-text mode even for HTML-looking content
from zerda.chunks import DocumentType
sections = parser.parse(text, doc_type=DocumentType.PLAIN_TEXT)