Skip to content

Commit 1644d3c

Browse files
authored
Merge pull request #2533 from strictdoc-project/stanislaw/html2pdf
fix(export/html2pdf): implement a proper handling of LINKs to documents in bundle and non-bundle documents
2 parents 9125120 + ec4d95c commit 1644d3c

File tree

8 files changed

+49
-34
lines changed

8 files changed

+49
-34
lines changed

strictdoc/export/html/renderers/link_renderer.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,6 @@ def render_node_link(
9898

9999
assert isinstance(node, (SDocDocument, SDocNode, Anchor)), node
100100

101-
if isinstance(node, SDocDocument):
102-
context_level_or_none: Optional[int] = None
103-
if context_document is not None:
104-
assert context_document.meta is not None
105-
context_level_or_none = context_document.meta.level
106-
107-
assert node.meta is not None
108-
document_link = node.meta.get_html_link(
109-
document_type,
110-
context_level_or_none,
111-
)
112-
return document_link + "#_TOP"
113-
114-
assert isinstance(node, (SDocNode, Anchor)), node
115101
assert isinstance(document_type, DocumentType), type(document_type)
116102
local_link = self.render_local_anchor(node)
117103

@@ -122,18 +108,15 @@ def render_node_link(
122108
return f"#{local_link}"
123109

124110
including_document = node.get_including_document()
125-
if (
126-
including_document is not None
127-
and including_document.is_bundle_document
128-
):
111+
if including_document is not None:
129112
return f"#{local_link}"
130113

131-
if (
132-
allow_local
133-
and context_document is not None
134-
and node.get_document() == context_document
135-
):
136-
return f"#{local_link}"
114+
if allow_local and context_document is not None:
115+
if isinstance(node, SDocDocument):
116+
if node == context_document:
117+
return f"#{local_link}"
118+
elif node.get_document() == context_document:
119+
return f"#{local_link}"
137120

138121
# Now two cases:
139122
# - Context document exists, and we want to take into account this
@@ -153,7 +136,11 @@ def render_node_link(
153136
else:
154137
self.req_link_cache[link_cache_key] = {}
155138

156-
parent_or_including_document = node.get_parent_or_including_document()
139+
parent_or_including_document = (
140+
node.get_parent_or_including_document()
141+
if not isinstance(node, SDocDocument)
142+
else node
143+
)
157144
assert parent_or_including_document.meta is not None
158145
document_link = parent_or_including_document.meta.get_html_link(
159146
document_type,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span style="position: absolute; left: 0;" id="{{ local_anchor }}" data-uid="{{ local_anchor }}" data-anchor="{{ local_anchor }}"></span>
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<h1 data-testid="document-title">{{ view_object.document.title }}</h1>
1+
{%- set local_anchor = view_object.render_local_anchor(view_object.document) %}
2+
<h1 data-testid="document-title">
3+
{{ view_object.document.title }}
4+
{% include "components/anchor_document/index.jinja" %}
5+
</h1>

tests/integration/features/html/escaping/01_escape_input_from_sdoc/test.itest

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ CHECK-ALL-NEXT: >
4141
CHECK-ALL-NEXT: &lt;b&gt;&#34;escaping&#34;&amp;nbsp;&#39;document title&#39;&lt;/b&gt;
4242

4343
# Main document: Title.
44-
CHECK-INPUT: <h1 data-testid="document-title">&lt;b&gt;&#34;escaping&#34;&amp;nbsp;&#39;document title&#39;&lt;/b&gt;</h1>
45-
CHECK-TABLE: <h1 data-testid="document-title">&lt;b&gt;&#34;escaping&#34;&amp;nbsp;&#39;document title&#39;&lt;/b&gt;</h1>
44+
CHECK-INPUT: <h1 data-testid="document-title">
45+
CHECK-INPUT-NEXT:&lt;b&gt;&#34;escaping&#34;&amp;nbsp;&#39;document title&#39;&lt;/b&gt;
46+
47+
CHECK-TABLE: <h1 data-testid="document-title">
48+
CHECK-TABLE-NEXT: &lt;b&gt;&#34;escaping&#34;&amp;nbsp;&#39;document title&#39;&lt;/b&gt;
4649

4750
# Main document: Section.
4851
CHECK-INPUT: <sdoc-autogen>1.&nbsp;&lt;b&gt;&#34;escaping&#34;&amp;nbsp;&#39;section title&#39;&lt;/b&gt;</sdoc-autogen>

tests/integration/features/html2pdf/07_composable_document_with_relations/input.sdoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[DOCUMENT]
22
TITLE: Project Risk Management
3+
UID: PARENT-DOC
34

45
[TEXT]
56
STATEMENT: Main file
@@ -11,7 +12,11 @@ FILE: nested/included.sdoc
1112
UID: REQ-003
1213
TITLE: Third one
1314
STATEMENT: >>>
14-
Third statement links to fourth: [LINK: REQ-004]
15+
Link from parent document to parent document: [LINK: PARENT-DOC]
16+
17+
Link from parent document to included document: [LINK: INCLUDED-DOC]
18+
19+
Link from parent document to this document's requiremnt: [LINK: REQ-004]
1520
<<<
1621

1722
[REQUIREMENT]

tests/integration/features/html2pdf/07_composable_document_with_relations/nested/included.sdoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
[DOCUMENT]
22
TITLE: Included doc
3+
UID: INCLUDED-DOC
34

45
[REQUIREMENT]
56
UID: REQ-001
67
TITLE: First one
78
STATEMENT: >>>
8-
First statement links to second: [LINK: REQ-002]
9+
Link from included document to parent document: [LINK: PARENT-DOC]
10+
11+
Link from included document to included document: [LINK: INCLUDED-DOC]
12+
13+
Link from included document to this included document's requirement: [LINK: REQ-002]
914
<<<
1015

1116
[REQUIREMENT]

tests/integration/features/html2pdf/07_composable_document_with_relations/test.itest

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ RUN: %check_exists --file %T/html2pdf/html/%THIS_TEST_FOLDER/input-PDF.html
99
RUN: %check_exists --file %T/html2pdf/pdf/input.pdf
1010

1111
RUN: %cat %T/html2pdf/html/%THIS_TEST_FOLDER/input-PDF.html | filecheck %s --dump-input=fail --check-prefix CHECK-INPUT-PDF
12-
CHECK-INPUT-PDF: <p>First statement links to second: <a href="../07_composable_document_with_relations/input-PDF.html#REQ-002">🔗&nbsp;1.2. second one</a></p>
12+
CHECK-INPUT-PDF: <p>Link from included document to parent document: <a href="#PARENT-DOC">🔗&nbsp;Project Risk Management</a></p>
13+
CHECK-INPUT-PDF: <p>Link from included document to included document: <a href="#INCLUDED-DOC">🔗&nbsp;Included doc</a></p>
14+
CHECK-INPUT-PDF: <p>Link from included document to this included document's requirement: <a href="#REQ-002">🔗&nbsp;1.2. second one</a></p>
15+
CHECK-INPUT-PDF: <p>Link from parent document to parent document: <a href="#PARENT-DOC">🔗&nbsp;Project Risk Management</a></p>
16+
CHECK-INPUT-PDF: <p>Link from parent document to included document: <a href="#INCLUDED-DOC">🔗&nbsp;Included doc</a></p>
17+
CHECK-INPUT-PDF: <p>Link from parent document to this document's requiremnt: <a href="#REQ-004">🔗&nbsp;3. fourth one</a></p>
1318

1419
# With --generate-bundle-document, a bundle document is generated.
1520
RUN: %strictdoc export %S --formats=html2pdf --included-documents --generate-bundle-document --output-dir %T | filecheck %s --dump-input=fail
@@ -18,4 +23,9 @@ RUN: %check_exists --file %T/html2pdf/pdf/bundle.pdf
1823

1924
RUN: %cat %T/html2pdf/html/bundle-PDF.html | filecheck %s --dump-input=fail --check-prefix CHECK-BUNDLE-PDF
2025
# The link points to the same bundle document, not to its parent document.
21-
CHECK-BUNDLE-PDF: <p>First statement links to second: <a href="#REQ-002">🔗&nbsp;1.1.2. second one</a></p>
26+
CHECK-BUNDLE-PDF: <p>Link from included document to parent document: <a href="#PARENT-DOC">🔗&nbsp;Project Risk Management</a></p>
27+
CHECK-BUNDLE-PDF: <p>Link from included document to included document: <a href="#INCLUDED-DOC">🔗&nbsp;Included doc</a></p>
28+
CHECK-BUNDLE-PDF: <p>Link from included document to this included document's requirement: <a href="#REQ-002">🔗&nbsp;1.1.2. second one</a></p>
29+
CHECK-BUNDLE-PDF: <p>Link from parent document to parent document: <a href="#PARENT-DOC">🔗&nbsp;Project Risk Management</a></p>
30+
CHECK-BUNDLE-PDF: <p>Link from parent document to included document: <a href="#INCLUDED-DOC">🔗&nbsp;Included doc</a></p>
31+
CHECK-BUNDLE-PDF: <p>Link from parent document to this document's requiremnt: <a href="#REQ-004">🔗&nbsp;1.3. fourth one</a></p>

tests/integration/features/sdoc/markup/11_node_LINK_references_a_document/test.itest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ CHECK: Published: Document 1
33

44
RUN: %cat %T/html/%THIS_TEST_FOLDER/input1.html | filecheck %s --dump-input=fail --check-prefix CHECK-HTML-DOC1
55

6-
CHECK-HTML-DOC1: Read <a href="../11_node_LINK_references_a_document/input1.html#_TOP">🔗&nbsp;Document 1</a> then <a href="../11_node_LINK_references_a_document/input2.html#_TOP">🔗&nbsp;Document 2</a>.
6+
CHECK-HTML-DOC1: Read <a href="#DOC-1">🔗&nbsp;Document 1</a> then <a href="../11_node_LINK_references_a_document/input2.html#DOC-2">🔗&nbsp;Document 2</a>.
77

88
RUN: %cat %T/html/%THIS_TEST_FOLDER/input2.html | filecheck %s --dump-input=fail --check-prefix CHECK-HTML-DOC2
99

10-
CHECK-HTML-DOC2: Read <a href="../11_node_LINK_references_a_document/input2.html#_TOP">🔗&nbsp;Document 2</a> then <a href="../11_node_LINK_references_a_document/input1.html#_TOP">🔗&nbsp;Document 1</a>.
10+
CHECK-HTML-DOC2: Read <a href="#DOC-2">🔗&nbsp;Document 2</a> then <a href="../11_node_LINK_references_a_document/input1.html#DOC-1">🔗&nbsp;Document 1</a>.

0 commit comments

Comments
 (0)