Skip to content

Commit

Permalink
Fix html escaping of source line marks
Browse files Browse the repository at this point in the history
SourceFileViewHTMLGenerator escaping code had multiple issues:

- When marking each line from range_start_pragma_processor as safe, we
  wrapped not only strings but also whole tuples in Markup().
- For tuple elements we didn't differentiate whether they from pygments
  (already escaped) or from source file (not yet escaped). Only the
  later must be explicitly escaped.

Relates to #1921.
  • Loading branch information
haxtibal committed Jul 26, 2024
1 parent fd2e364 commit b5ba85e
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions strictdoc/export/html/generators/source_file_view_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mypy: disable-error-code="no-untyped-call,no-untyped-def,operator"
from typing import List, Tuple
from typing import List, Tuple, Union

from markupsafe import Markup, escape
from pygments import highlight
Expand Down Expand Up @@ -89,7 +89,19 @@ def get_pygmented_source_lines(
source_file: SourceFile,
source_file_lines: List[str],
coverage_info: SourceFileTraceabilityInfo,
) -> Tuple[List[Markup], Markup]:
) -> Tuple[
List[
Union[
Markup,
Tuple[
Markup,
Markup,
Union[ForwardRangeMarker, LineMarker, RangeMarker],
],
]
],
Markup,
]:
assert isinstance(source_file, SourceFile)
assert isinstance(source_file_lines, list)
assert isinstance(coverage_info, SourceFileTraceabilityInfo)
Expand Down Expand Up @@ -178,15 +190,18 @@ def get_pygmented_source_lines(
for pragma in coverage_info.pragmas:
pragma_line = pragma.ng_source_line_begin
if isinstance(pragma, ForwardRangeMarker):
pygmented_source_file_lines[pragma_line - 1] = (
before_line = (
pygmented_source_file_lines[pragma_line - 1].rstrip("\n")
+ " ",
"\n",
+ " "
)
pygmented_source_file_lines[pragma_line - 1] = (
Markup(before_line),
Markup("\n"),
pragma,
)
continue

source_line = source_file_lines[pragma_line - 1]
source_line = escape(source_file_lines[pragma_line - 1])

assert len(pragma.reqs_objs) > 0
before_line = source_line[
Expand All @@ -203,15 +218,17 @@ def get_pygmented_source_lines(
after_line = source_line[closing_bracket_index:].rstrip()

pygmented_source_file_lines[pragma_line - 1] = (
escape(before_line),
escape(after_line),
before_line,
after_line,
pragma,
)
pygments_styles = (
f"/* Lexer: {lexer.name} */\n"
+ html_formatter.get_style_defs(".highlight")
)

return list(map(Markup, pygmented_source_file_lines)), Markup(
pygments_styles
as_markup = map(
lambda l: l if isinstance(l, tuple) else Markup(l),
pygmented_source_file_lines,
)
return list(as_markup), Markup(pygments_styles)

0 comments on commit b5ba85e

Please sign in to comment.