Skip to content

Commit 854b4dc

Browse files
committed
export/html: document meta information: recognize @GIT_VERSION and @GIT_BRANCH markers
1 parent b59ab5f commit 854b4dc

File tree

19 files changed

+192
-7
lines changed

19 files changed

+192
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ tests/unit_server/**/output/**
2626

2727
### LIT/FileCheck integration tests' artifacts. ###
2828
tests/integration/**/Output/**
29+
# This helps to gitignore fake git repositories that are created by the integration tests.
2930
tests/integration/**/output/**
3031
tests/integration/**/sandbox/
3132
.lit_test_times.txt

strictdoc/export/html/generators/document.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from strictdoc.export.html.html_templates import HTMLTemplates
99
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
10+
from strictdoc.helpers.git_client import GitClient
1011

1112

1213
class DocumentHTMLGenerator:
@@ -17,6 +18,7 @@ def export(
1718
traceability_index,
1819
markup_renderer,
1920
link_renderer: LinkRenderer,
21+
git_client: GitClient,
2022
standalone: bool,
2123
html_templates: HTMLTemplates,
2224
):
@@ -27,6 +29,7 @@ def export(
2729
project_config=project_config,
2830
link_renderer=link_renderer,
2931
markup_renderer=markup_renderer,
32+
git_client=git_client,
3033
standalone=standalone,
3134
)
3235
return view_object.render_screen(html_templates.jinja_environment())

strictdoc/export/html/generators/document_deep_trace.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77
from strictdoc.export.html.html_templates import HTMLTemplates
88
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
9+
from strictdoc.helpers.git_client import GitClient
910

1011

1112
class DocumentDeepTraceHTMLGenerator:
@@ -16,6 +17,7 @@ def export_deep(
1617
traceability_index,
1718
markup_renderer,
1819
link_renderer: LinkRenderer,
20+
git_client: GitClient,
1921
html_templates: HTMLTemplates,
2022
):
2123
view_object = DocumentScreenViewObject(
@@ -25,6 +27,7 @@ def export_deep(
2527
project_config=project_config,
2628
link_renderer=link_renderer,
2729
markup_renderer=markup_renderer,
30+
git_client=git_client,
2831
standalone=False,
2932
)
3033
return view_object.render_screen(html_templates.jinja_environment())

strictdoc/export/html/generators/document_pdf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from strictdoc.export.html.html_templates import HTMLTemplates
99
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
10+
from strictdoc.helpers.git_client import GitClient
1011

1112

1213
class DocumentHTML2PDFGenerator:
@@ -17,6 +18,7 @@ def export(
1718
traceability_index,
1819
markup_renderer,
1920
link_renderer: LinkRenderer,
21+
git_client: GitClient,
2022
standalone: bool,
2123
html_templates: HTMLTemplates,
2224
):
@@ -27,6 +29,7 @@ def export(
2729
project_config=project_config,
2830
link_renderer=link_renderer,
2931
markup_renderer=markup_renderer,
32+
git_client=git_client,
3033
standalone=standalone,
3134
)
3235
return view_object.render_screen(html_templates.jinja_environment())

strictdoc/export/html/generators/document_table.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77
from strictdoc.export.html.html_templates import HTMLTemplates
88
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
9+
from strictdoc.helpers.git_client import GitClient
910

1011

1112
class DocumentTableHTMLGenerator:
@@ -16,6 +17,7 @@ def export(
1617
traceability_index,
1718
markup_renderer,
1819
link_renderer: LinkRenderer,
20+
git_client: GitClient,
1921
html_templates: HTMLTemplates,
2022
):
2123
view_object = DocumentScreenViewObject(
@@ -25,6 +27,7 @@ def export(
2527
project_config=project_config,
2628
link_renderer=link_renderer,
2729
markup_renderer=markup_renderer,
30+
git_client=git_client,
2831
standalone=False,
2932
)
3033
return view_object.render_screen(html_templates.jinja_environment())

strictdoc/export/html/generators/document_trace.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)
77
from strictdoc.export.html.html_templates import HTMLTemplates
88
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
9+
from strictdoc.helpers.git_client import GitClient
910

1011

1112
class DocumentTraceHTMLGenerator:
@@ -16,6 +17,7 @@ def export(
1617
traceability_index,
1718
markup_renderer,
1819
link_renderer: LinkRenderer,
20+
git_client: GitClient,
1921
html_templates: HTMLTemplates,
2022
):
2123
view_object = DocumentScreenViewObject(
@@ -25,6 +27,7 @@ def export(
2527
project_config=project_config,
2628
link_renderer=link_renderer,
2729
markup_renderer=markup_renderer,
30+
git_client=git_client,
2831
standalone=False,
2932
)
3033
return view_object.render_screen(html_templates.jinja_environment())

strictdoc/export/html/generators/view_objects/document_screen_view_object.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from strictdoc.export.html.html_templates import JinjaEnvironment
2020
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
2121
from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer
22+
from strictdoc.helpers.git_client import GitClient
23+
from strictdoc.helpers.string import interpolate_at_pattern_lazy
2224
from strictdoc.server.helpers.turbo import render_turbo_stream
2325

2426

@@ -33,6 +35,7 @@ def __init__(
3335
project_config: ProjectConfig,
3436
link_renderer: LinkRenderer,
3537
markup_renderer: MarkupRenderer,
38+
git_client: GitClient,
3639
standalone: bool,
3740
):
3841
self.document_type: DocumentType = document_type
@@ -42,6 +45,7 @@ def __init__(
4245
self.project_config: ProjectConfig = project_config
4346
self.link_renderer: LinkRenderer = link_renderer
4447
self.markup_renderer: MarkupRenderer = markup_renderer
48+
self.git_client: GitClient = git_client
4549
self.standalone: bool = standalone
4650
self.document_iterator = self.traceability_index.get_document_iterator(
4751
self.document
@@ -190,6 +194,21 @@ def render_update_document_content_with_moved_node(
190194
)
191195
return output
192196

197+
def render_document_version(self) -> Optional[str]:
198+
if self.document.config.version is None:
199+
return None
200+
201+
def resolver(variable_name):
202+
if variable_name == "GIT_VERSION":
203+
return self.git_client.get_commit_hash()
204+
elif variable_name == "GIT_BRANCH":
205+
return self.git_client.get_branch()
206+
return variable_name
207+
208+
return interpolate_at_pattern_lazy(
209+
self.document.config.version, resolver
210+
)
211+
193212
def is_empty_tree(self) -> bool:
194213
return self.document_tree_iterator.is_empty_tree()
195214

strictdoc/export/html/html_generator.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer
4545
from strictdoc.export.html.tools.html_embedded import HTMLEmbedder
4646
from strictdoc.helpers.file_system import sync_dir
47+
from strictdoc.helpers.git_client import GitClient
4748
from strictdoc.helpers.paths import SDocRelativePath
4849
from strictdoc.helpers.timing import measure_performance
4950

@@ -54,6 +55,7 @@ def __init__(
5455
):
5556
self.project_config: ProjectConfig = project_config
5657
self.html_templates = html_templates
58+
self.git_client: GitClient = GitClient(commit_hash=None)
5759

5860
def export_complete_tree(
5961
self,
@@ -326,6 +328,7 @@ def export_single_document(
326328
traceability_index,
327329
markup_renderer,
328330
link_renderer,
331+
git_client=self.git_client,
329332
standalone=False,
330333
html_templates=self.html_templates,
331334
)
@@ -346,7 +349,8 @@ def export_single_document(
346349
traceability_index,
347350
markup_renderer,
348351
link_renderer,
349-
self.html_templates,
352+
git_client=self.git_client,
353+
html_templates=self.html_templates,
350354
)
351355
document_out_file = document_meta.get_html_table_path()
352356
with open(document_out_file, "w", encoding="utf8") as file:
@@ -365,7 +369,8 @@ def export_single_document(
365369
traceability_index,
366370
markup_renderer,
367371
link_renderer,
368-
self.html_templates,
372+
git_client=self.git_client,
373+
html_templates=self.html_templates,
369374
)
370375
document_out_file = document_meta.get_html_traceability_path()
371376
with open(document_out_file, "w", encoding="utf8") as file:
@@ -384,7 +389,8 @@ def export_single_document(
384389
traceability_index,
385390
markup_renderer,
386391
link_renderer,
387-
self.html_templates,
392+
git_client=self.git_client,
393+
html_templates=self.html_templates,
388394
)
389395
document_out_file = document_meta.get_html_deep_traceability_path()
390396
with open(document_out_file, "w", encoding="utf8") as file:
@@ -401,6 +407,7 @@ def export_single_document(
401407
traceability_index,
402408
markup_renderer,
403409
link_renderer,
410+
git_client=self.git_client,
404411
standalone=False,
405412
html_templates=self.html_templates,
406413
)
@@ -418,6 +425,7 @@ def export_single_document(
418425
traceability_index,
419426
markup_renderer,
420427
link_renderer,
428+
git_client=self.git_client,
421429
standalone=True,
422430
html_templates=self.html_templates,
423431
)

strictdoc/export/html/templates/components/node_field/document_meta/index.jinja

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
{%- endwith -%}
99
</sdoc-meta-field>
1010
{%- endif -%}
11-
{%- if view_object.document.config.version -%}
11+
12+
{% set document_version_ = view_object.render_document_version() %}
13+
{%- if document_version_ is not none -%}
1214
<sdoc-meta-label data-testid="document-config-version-label">VERSION:</sdoc-meta-label>
1315
<sdoc-meta-field data-testid="document-config-version-field">
14-
{%- with field_content = view_object.document.config.version %}
16+
{%- with field_content = document_version_ %}
1517
{%- include "components/field/index.jinja" -%}
1618
{%- endwith -%}
1719
</sdoc-meta-field>

strictdoc/export/html2pdf/html2pdf_generator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer
1515
from strictdoc.export.html2pdf.pdf_print_driver import PDFPrintDriver
1616
from strictdoc.helpers.exception import StrictDocException
17+
from strictdoc.helpers.git_client import GitClient
1718
from strictdoc.helpers.timing import measure_performance
1819

1920

@@ -29,6 +30,8 @@ def export_tree(
2930
if not project_config.is_activated_html2pdf():
3031
raise StrictDocException("HTML2PDF feature is not enabled")
3132

33+
git_client: GitClient = GitClient(commit_hash=None)
34+
3235
path_to_output_pdf_html_dir = os.path.join(output_html2pdf_root, "html")
3336
path_to_output_pdf_pdf_dir = os.path.join(output_html2pdf_root, "pdf")
3437

@@ -64,6 +67,7 @@ def export_tree(
6467
traceability_index,
6568
markup_renderer,
6669
link_renderer,
70+
git_client=git_client,
6771
standalone=False,
6872
html_templates=html_templates,
6973
)

strictdoc/helpers/git_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class GitClient:
88
def __init__(self, commit_hash: Optional[str]):
99
self._commit_hash: Optional[str] = commit_hash
10+
self._branch: Optional[str] = None
1011

1112
@staticmethod
1213
def create():
@@ -26,4 +27,43 @@ def create():
2627
return GitClient(commit_hash=commit_hash)
2728

2829
def get_commit_hash(self) -> Optional[str]:
30+
if self._commit_hash is not None:
31+
return self._commit_hash
32+
try:
33+
process_result = subprocess.run(
34+
["git", "describe", "--always", "--tags"],
35+
cwd=os.getcwd(),
36+
capture_output=True,
37+
text=True,
38+
check=False,
39+
)
40+
if process_result.returncode != 0:
41+
return "N/A"
42+
commit_hash = process_result.stdout
43+
except subprocess.CalledProcessError:
44+
commit_hash = "N/A"
45+
except FileNotFoundError:
46+
commit_hash = "Git not available"
47+
self._commit_hash = commit_hash.strip()
2948
return self._commit_hash
49+
50+
def get_branch(self) -> Optional[str]:
51+
if self._branch is not None:
52+
return self._branch
53+
try:
54+
process_result = subprocess.run(
55+
["git", "rev-parse", "--abbrev-ref", "HEAD"],
56+
cwd=os.getcwd(),
57+
capture_output=True,
58+
text=True,
59+
check=False,
60+
)
61+
if process_result.returncode != 0:
62+
return "N/A"
63+
branch = process_result.stdout
64+
except subprocess.CalledProcessError:
65+
branch = "N/A"
66+
except FileNotFoundError:
67+
branch = "Git not available"
68+
self._branch = branch.strip()
69+
return self._branch

strictdoc/helpers/string.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,13 @@ def create_safe_document_file_name(string) -> str:
9090

9191
def ensure_newline(text: str) -> str:
9292
return text.rstrip() + "\n"
93+
94+
95+
def interpolate_at_pattern_lazy(template: str, value_resolver) -> str:
96+
pattern = r"@(\w+)"
97+
98+
def replace_variable(match):
99+
variable_name = match.group(1)
100+
return value_resolver(variable_name)
101+
102+
return re.sub(pattern, replace_variable, template)

0 commit comments

Comments
 (0)