generated from coulomb/repo-seed
Contract framework with markdown-native contracts utilizing fenced YAML blocks
This commit is contained in:
@@ -29,7 +29,7 @@ def parse_markdown(markdown: str, source_path: str | None = None) -> Document:
|
||||
|
||||
frontmatter, body, body_line_offset = _split_frontmatter(markdown)
|
||||
tokens = _parse_tokens(body)
|
||||
blocks, headings = _blocks_and_headings(tokens, body_line_offset)
|
||||
blocks, headings = _blocks_and_headings(tokens, body_line_offset, body)
|
||||
sections = _sections_from_blocks(blocks, headings)
|
||||
return Document(
|
||||
source_path=source_path,
|
||||
@@ -97,7 +97,7 @@ def _token_to_dict(token: Token) -> dict[str, Any]:
|
||||
|
||||
|
||||
def _blocks_and_headings(
|
||||
tokens: list[dict[str, Any]], line_offset: int
|
||||
tokens: list[dict[str, Any]], line_offset: int, markdown: str
|
||||
) -> tuple[list[ContentBlock], list[Heading]]:
|
||||
blocks: list[ContentBlock] = []
|
||||
headings: list[Heading] = []
|
||||
@@ -126,6 +126,8 @@ def _blocks_and_headings(
|
||||
if not text and token_type.endswith("_open"):
|
||||
inline = _next_inline(tokens, index)
|
||||
text = inline.get("content", "") if inline else ""
|
||||
if not text:
|
||||
text = _source_text(token, line_offset, markdown)
|
||||
blocks.append(
|
||||
ContentBlock(
|
||||
type=_block_type(token_type),
|
||||
@@ -151,6 +153,16 @@ def _line_range(token: dict[str, Any], line_offset: int) -> tuple[int | None, in
|
||||
return line_map[0] + line_offset + 1, line_map[1] + line_offset
|
||||
|
||||
|
||||
def _source_text(token: dict[str, Any], line_offset: int, markdown: str) -> str:
|
||||
line_start, line_end = _line_range(token, line_offset)
|
||||
if line_start is None or line_end is None:
|
||||
return ""
|
||||
lines = markdown.splitlines()
|
||||
start_index = max(line_start - line_offset - 1, 0)
|
||||
end_index = max(line_end - line_offset, start_index)
|
||||
return "\n".join(lines[start_index:end_index]).strip()
|
||||
|
||||
|
||||
def _block_type(token_type: str) -> str:
|
||||
return {
|
||||
"paragraph_open": "paragraph",
|
||||
|
||||
Reference in New Issue
Block a user