Skip to content

Commit

Permalink
Add ability to process link in AMM MD docs generator (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-kc authored Dec 7, 2023
1 parent d6924e5 commit 21edd3a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Sequence,
Type,
)
import urllib.parse

import attr
import yaml
Expand All @@ -25,9 +26,11 @@
AmmOperation,
AmmOperationExample,
)
from dl_attrs_model_mapper_doc_tools.md_link_extractor import process_links
from dl_attrs_model_mapper_doc_tools.render_units import (
ClassDoc,
DocHeader,
DocLink,
DocSection,
DocText,
DocUnit,
Expand All @@ -51,8 +54,34 @@ class Docs:
_dedicated_class_docs: list[ClassDoc] = attr.ib(factory=list)
_operations: list[tuple[AmmOperation, OperationDoc]] = attr.ib(factory=list)

_process_doc_links: bool = attr.ib(default=False)
_doc_links_map: dict[str, str] = attr.ib(factory=dict[str, str])

def adopt_doc_link(self, doc_link: DocLink) -> Optional[DocLink]:
parsed_href = urllib.parse.urlparse(doc_link.href)
if parsed_href.scheme != "visdocs":
return None

if doc_link.href not in self._doc_links_map:
raise ValueError(f"Mapping for visualization docs URL is not registered: {doc_link.href}")
return DocLink(text=doc_link.text, href=self._doc_links_map[doc_link.href])

def handle_text_from_spec(self, m_txt: Optional[MText]) -> Optional[DocText]:
if not self._process_doc_links:
return DocText(m_txt) if m_txt else None

if m_txt is None:
return None

en_txt: Optional[str] = m_txt.en
if en_txt is None:
return None

return DocText(process_links(en_txt, self.adopt_doc_link))

def field_to_doc_lines(self, field: AmmField, path: Sequence[str]) -> Sequence[FieldLine]:
cp = field.common_props
description: Optional[DocText] = self.handle_text_from_spec(field.common_props.description)

if isinstance(field, AmmScalarField):
scalar_type = field.scalar_type
Expand All @@ -71,7 +100,7 @@ def field_to_doc_lines(self, field: AmmField, path: Sequence[str]) -> Sequence[F
type_text=type_text,
nullable=cp.allow_none,
required=cp.required,
description=field.common_props.description,
description=description,
)
)
]
Expand All @@ -86,7 +115,7 @@ def field_to_doc_lines(self, field: AmmField, path: Sequence[str]) -> Sequence[F
type_ref=nested_schema_doc_file_path,
nullable=cp.allow_none,
required=cp.required,
description=field.common_props.description,
description=description,
)
if nested_schema_doc_file_path is not None:
return [main_line]
Expand All @@ -107,7 +136,7 @@ def field_to_doc_lines(self, field: AmmField, path: Sequence[str]) -> Sequence[F
type_text="list",
nullable=cp.allow_none,
required=cp.required,
description=field.common_props.description,
description=description,
),
*self.field_to_doc_lines(field.item, next_path),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class FieldLine(DocUnit):
nullable: bool
required: bool

description: Optional[MText] = None
description: Optional[DocText] = None
type_ref: Optional[str] = None

def _get_type_md(self, render_ctx: RenderContext) -> str:
Expand All @@ -286,10 +286,13 @@ def get_table_header(self) -> DocTableHeader:
def render_md(self, render_ctx: RenderContext) -> Sequence[str]:
path = ".".join(f"`{part}`" for part in self.path)

description_txt: str = ""
description = self.description
if description is not None:
description_txt = "\n".join(description.render_md(render_ctx))

return [
f"{path}"
f" | {self._get_type_md(render_ctx)}"
f" | {render_ctx.localized_m_text(self.description) or ''}".rstrip(" ").replace("\n", "<br>")
f"{path}" f" | {self._get_type_md(render_ctx)}" f" | {description_txt}".rstrip(" ").replace("\n", "<br>")
]


Expand Down

0 comments on commit 21edd3a

Please sign in to comment.