Skip to content

Commit 535c2a7

Browse files
authored
Merge pull request #2521 from strictdoc-project/stanislaw/server
feat(server): generate only a source single file when requested, not all source files
2 parents 0a1c3d0 + a82c07b commit 535c2a7

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

strictdoc/export/html/html_generator.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
from strictdoc.helpers.git_client import GitClient
6060
from strictdoc.helpers.mid import MID
6161
from strictdoc.helpers.parallelizer import Parallelizer
62-
from strictdoc.helpers.paths import SDocRelativePath
62+
from strictdoc.helpers.paths import SDocRelativePath, path_to_posix_path
6363
from strictdoc.helpers.timing import measure_performance, timing_decorator
6464

6565

@@ -171,6 +171,9 @@ def export_complete_tree(
171171
if self.project_config.is_feature_activated(
172172
ProjectFeature.REQUIREMENT_TO_SOURCE_TRACEABILITY
173173
):
174+
self.export_source_files_screens(
175+
traceability_index=traceability_index,
176+
)
174177
self.export_source_coverage_screen(
175178
traceability_index=traceability_index,
176179
)
@@ -579,7 +582,8 @@ def export_requirements_coverage_screen(
579582
) as file:
580583
file.write(requirements_coverage_content)
581584

582-
def export_source_coverage_screen(
585+
@timing_decorator("Export source file pages")
586+
def export_source_files_screens(
583587
self,
584588
*,
585589
traceability_index: TraceabilityIndex,
@@ -601,6 +605,15 @@ def export_source_coverage_screen(
601605
html_templates=self.html_templates,
602606
)
603607

608+
def export_source_coverage_screen(
609+
self,
610+
*,
611+
traceability_index: TraceabilityIndex,
612+
) -> None:
613+
assert isinstance(
614+
traceability_index.document_tree.source_tree, SourceTree
615+
), traceability_index.document_tree.source_tree
616+
604617
source_coverage_content = SourceFileCoverageHTMLGenerator.export(
605618
project_config=self.project_config,
606619
traceability_index=traceability_index,
@@ -612,6 +625,47 @@ def export_source_coverage_screen(
612625
with open(output_html_source_coverage, "w", encoding="utf8") as file:
613626
file.write(source_coverage_content)
614627

628+
def export_single_source_file_screen(
629+
self,
630+
*,
631+
traceability_index: TraceabilityIndex,
632+
path_to_source_file: str,
633+
) -> None:
634+
assert isinstance(
635+
traceability_index.document_tree.source_tree, SourceTree
636+
), traceability_index.document_tree.source_tree
637+
638+
# FIXME: path_to_source_file must not enter this function with forward slashes.
639+
# Test and fix this on Windows.
640+
# https://github.com/strictdoc-project/strictdoc/issues/2068
641+
relative_path_to_source_file = path_to_posix_path(path_to_source_file)
642+
relative_path_to_source_file = (
643+
relative_path_to_source_file.removeprefix("_source_files/")
644+
)
645+
relative_path_to_source_file = (
646+
relative_path_to_source_file.removesuffix(".html")
647+
)
648+
649+
for (
650+
source_file
651+
) in traceability_index.document_tree.source_tree.source_files:
652+
if not source_file.is_referenced:
653+
continue
654+
655+
if (
656+
relative_path_to_source_file
657+
== source_file.in_doctree_source_file_rel_path_posix
658+
):
659+
SourceFileViewHTMLGenerator.export_to_file(
660+
project_config=self.project_config,
661+
source_file=source_file,
662+
traceability_index=traceability_index,
663+
html_templates=self.html_templates,
664+
)
665+
return
666+
667+
raise FileNotFoundError
668+
615669
def export_project_statistics(
616670
self,
617671
traceability_index: TraceabilityIndex,

strictdoc/server/routers/main_router.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,11 +2518,23 @@ def get_document(request: Request, url_to_document: str) -> Response:
25182518
return Response(status_code=304)
25192519
else:
25202520
if document_relative_path.relative_path.startswith("_source_files"):
2521-
# FIXME: We could be more specific here and only generate the
2522-
# requested file.
2523-
html_generator.export_source_coverage_screen(
2524-
traceability_index=export_action.traceability_index,
2525-
)
2521+
if document_relative_path.relative_path.endswith(
2522+
"source_coverage.html"
2523+
):
2524+
html_generator.export_source_coverage_screen(
2525+
traceability_index=export_action.traceability_index,
2526+
)
2527+
else:
2528+
try:
2529+
html_generator.export_single_source_file_screen(
2530+
traceability_index=export_action.traceability_index,
2531+
path_to_source_file=document_relative_path.relative_path,
2532+
)
2533+
except FileNotFoundError:
2534+
return HTMLResponse(
2535+
content=f"Not Found: {url_to_document}",
2536+
status_code=404,
2537+
)
25262538
elif document_relative_path.relative_path == "index.html":
25272539
html_generator.export_project_tree_screen(
25282540
traceability_index=export_action.traceability_index,

0 commit comments

Comments
 (0)