Skip to content

Commit

Permalink
Merge pull request strictdoc-project#1926 from strictdoc-project/stan…
Browse files Browse the repository at this point in the history
…islaw/fix_type_issue

Code climate: markup_renderer: remove unused code
  • Loading branch information
stanislaw authored Jul 21, 2024
2 parents e73eac4 + 5633ba6 commit e823f8c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
7 changes: 3 additions & 4 deletions strictdoc/export/html/renderers/html_fragment_writer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# mypy: disable-error-code="no-untyped-def"
class HTMLFragmentWriter:
@staticmethod
def write(text_fragment):
def write(text_fragment: str) -> str:
return text_fragment

@staticmethod
def write_link(title, _):
return f"{title}"
def write_anchor_link(title: str, href: str) -> str:
return f'<a href="{href}">🔗&nbsp;{title}</a>`'
51 changes: 30 additions & 21 deletions strictdoc/export/html/renderers/markup_renderer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mypy: disable-error-code="attr-defined,no-untyped-call,no-untyped-def,var-annotated"
from typing import Optional, Type, Union
from typing import Dict, Optional, Tuple, Union

from strictdoc.backend.sdoc.models.anchor import Anchor
from strictdoc.backend.sdoc.models.document import SDocDocument
Expand Down Expand Up @@ -34,18 +34,18 @@ def create(
assert isinstance(html_templates, HTMLTemplates)
html_fragment_writer: Union[
RstToHtmlFragmentWriter,
Type[HTMLFragmentWriter],
Type[TextToHtmlWriter],
HTMLFragmentWriter,
TextToHtmlWriter,
]
if not markup or markup == "RST":
html_fragment_writer = RstToHtmlFragmentWriter(
path_to_output_dir=config.export_output_dir,
context_document=context_document,
)
elif markup == "HTML":
html_fragment_writer = HTMLFragmentWriter
html_fragment_writer = HTMLFragmentWriter()
else:
html_fragment_writer = TextToHtmlWriter
html_fragment_writer = TextToHtmlWriter()
return MarkupRenderer(
html_fragment_writer,
traceability_index,
Expand All @@ -61,53 +61,62 @@ def __init__(
link_renderer: LinkRenderer,
html_templates: HTMLTemplates,
context_document: Optional[SDocDocument],
):
) -> None:
assert isinstance(traceability_index, TraceabilityIndex)
assert isinstance(link_renderer, LinkRenderer)
assert context_document is None or isinstance(
context_document, SDocDocument
), context_document
assert isinstance(html_templates, HTMLTemplates)

self.fragment_writer = fragment_writer
self.traceability_index = traceability_index
self.fragment_writer: Union[
RstToHtmlFragmentWriter,
HTMLFragmentWriter,
TextToHtmlWriter,
] = fragment_writer
self.traceability_index: TraceabilityIndex = traceability_index
self.link_renderer: LinkRenderer = link_renderer
self.context_document: Optional[SDocDocument] = context_document

# FIXME: Now that the underlying RST fragment caching is in place,
# This caching could be removed. It is unlikely that it adds any serious
# performance improvement.
self.cache = {}
self.rationale_cache = {}
self.cache: Dict[Tuple[DocumentType, SDocNodeField], str] = {}

self.template_anchor = html_templates.jinja_environment().get_template(
"rst/anchor.jinja"
)

def render_node_statement(self, document_type, node):
def render_node_statement(
self, document_type: DocumentType, node: SDocNode
) -> str:
assert isinstance(node, SDocNode)
return self.render_node_field(document_type, node.get_content_field())

def render_truncated_node_statement(self, document_type, node):
def render_truncated_node_statement(
self, document_type: DocumentType, node: SDocNode
) -> str:
assert isinstance(node, SDocNode)
return self.render_node_field(
document_type, node.get_content_field(), truncated=True
)
# FIXME: Double-check and switch to truncating using CSS.
# https://github.com/strictdoc-project/strictdoc/issues/1925
return self.render_node_field(document_type, node.get_content_field())

def render_node_rationale(self, document_type, node: SDocNode):
def render_node_rationale(
self, document_type: DocumentType, node: SDocNode
) -> str:
assert isinstance(node, SDocNode)
return self.render_node_field(
document_type,
node.get_field_by_name(RequirementFieldName.RATIONALE),
)

def render_node_field(
self, document_type, node_field: SDocNodeField, truncated: bool = False
):
self, document_type: DocumentType, node_field: SDocNodeField
) -> str:
assert isinstance(node_field, SDocNodeField), node_field

if (document_type, node_field, truncated) in self.cache:
return self.cache[(document_type, node_field, truncated)]
if (document_type, node_field) in self.cache:
return self.cache[(document_type, node_field)]

prev_part = None
parts_output = ""
Expand Down Expand Up @@ -138,6 +147,6 @@ def render_node_field(
raise NotImplementedError
prev_part = part
output = self.fragment_writer.write(parts_output)
self.cache[(document_type, node_field, truncated)] = output
self.cache[(document_type, node_field)] = output

return output
7 changes: 3 additions & 4 deletions strictdoc/export/html/renderers/text_to_html_writer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# mypy: disable-error-code="no-untyped-def"
import html


class TextToHtmlWriter:
@staticmethod
def write(text_fragment):
def write(text_fragment: str) -> str:
return html.escape(text_fragment, quote=True).replace("\n", "<br/>\n")

@staticmethod
def write_link(title, _):
return f"{title}"
def write_anchor_link(title: str, href: str) -> str:
return f'<a href="{href}">🔗&nbsp;{title}</a>`'
7 changes: 4 additions & 3 deletions strictdoc/export/rst/rst_to_html_fragment_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def __init__(
self.source_path: str = "<string>"
self.context_document: Optional[SDocDocument] = context_document

def write(self, rst_fragment):
def write(self, rst_fragment: str) -> str:
assert isinstance(rst_fragment, str), rst_fragment

# FIXME: This is broken.
if len(rst_fragment) < 0:
return self._write_no_cache(rst_fragment)

Expand Down Expand Up @@ -94,7 +95,7 @@ def write(self, rst_fragment):

return rendered_html

def _write_no_cache(self, rst_fragment):
def _write_no_cache(self, rst_fragment: str) -> str:
assert isinstance(rst_fragment, str), rst_fragment

# How do I convert a docutils document tree into an HTML string?
Expand Down Expand Up @@ -137,7 +138,7 @@ def _write_no_cache(self, rst_fragment):
print("<<<") # noqa: T201
sys.exit(1)

html = output["html_body"]
html: str = output["html_body"]

return html

Expand Down

0 comments on commit e823f8c

Please sign in to comment.