51 lines
1.3 KiB
Plaintext
51 lines
1.3 KiB
Plaintext
Heading 2 Core API
|
|
class mt-12 mb-6
|
|
|
|
Markdown
|
|
The core Python API provides parsing utilities and data structures for handling Terrace document structures.
|
|
|
|
Heading 3 Types
|
|
class mt-8 mb-4
|
|
|
|
CodeBlock python
|
|
from typing import TypedDict, Optional, Callable
|
|
|
|
# Type for line data
|
|
class LineData(TypedDict):
|
|
indent: str
|
|
level: int
|
|
offsetHead: int
|
|
offsetTail: int
|
|
|
|
# Type for a reader function
|
|
Reader = Callable[[], Optional[str]]
|
|
|
|
Heading 3 Parser Functions
|
|
class mt-8 mb-4
|
|
|
|
Markdown
|
|
Core parsing functions for processing Terrace document lines:
|
|
|
|
CodeBlock python
|
|
def createLineData(indent: str = ' ') -> LineData:
|
|
"""Initialize a LineData instance with default values"""
|
|
|
|
def parseLine(line: str, lineData: LineData) -> None:
|
|
"""Parse a line and update the LineData in-place"""
|
|
|
|
Heading 3 Usage Example
|
|
class mt-8 mb-4
|
|
|
|
CodeBlock python
|
|
from terrace import createLineData, parseLine
|
|
|
|
# Initialize line data with space indentation
|
|
line_data = createLineData(' ')
|
|
|
|
# Parse a line
|
|
line = " config database localhost"
|
|
parseLine(line, line_data)
|
|
|
|
print(f"Level: {line_data['level']}") # 2
|
|
print(f"Head start: {line_data['offsetHead']}") # 2
|
|
print(f"Head end: {line_data['offsetTail']}") # 8 |