diff --git a/strictdoc/export/html/html_generator.py b/strictdoc/export/html/html_generator.py index 5a2d35f5e..6969cf4c4 100644 --- a/strictdoc/export/html/html_generator.py +++ b/strictdoc/export/html/html_generator.py @@ -59,7 +59,7 @@ from strictdoc.helpers.git_client import GitClient from strictdoc.helpers.mid import MID from strictdoc.helpers.parallelizer import Parallelizer -from strictdoc.helpers.paths import SDocRelativePath +from strictdoc.helpers.paths import SDocRelativePath, path_to_posix_path from strictdoc.helpers.timing import measure_performance, timing_decorator @@ -171,6 +171,9 @@ def export_complete_tree( if self.project_config.is_feature_activated( ProjectFeature.REQUIREMENT_TO_SOURCE_TRACEABILITY ): + self.export_source_files_screens( + traceability_index=traceability_index, + ) self.export_source_coverage_screen( traceability_index=traceability_index, ) @@ -579,7 +582,8 @@ def export_requirements_coverage_screen( ) as file: file.write(requirements_coverage_content) - def export_source_coverage_screen( + @timing_decorator("Export source file pages") + def export_source_files_screens( self, *, traceability_index: TraceabilityIndex, @@ -601,6 +605,15 @@ def export_source_coverage_screen( html_templates=self.html_templates, ) + def export_source_coverage_screen( + self, + *, + traceability_index: TraceabilityIndex, + ) -> None: + assert isinstance( + traceability_index.document_tree.source_tree, SourceTree + ), traceability_index.document_tree.source_tree + source_coverage_content = SourceFileCoverageHTMLGenerator.export( project_config=self.project_config, traceability_index=traceability_index, @@ -612,6 +625,47 @@ def export_source_coverage_screen( with open(output_html_source_coverage, "w", encoding="utf8") as file: file.write(source_coverage_content) + def export_single_source_file_screen( + self, + *, + traceability_index: TraceabilityIndex, + path_to_source_file: str, + ) -> None: + assert isinstance( + traceability_index.document_tree.source_tree, SourceTree + ), traceability_index.document_tree.source_tree + + # FIXME: path_to_source_file must not enter this function with forward slashes. + # Test and fix this on Windows. + # https://github.com/strictdoc-project/strictdoc/issues/2068 + relative_path_to_source_file = path_to_posix_path(path_to_source_file) + relative_path_to_source_file = ( + relative_path_to_source_file.removeprefix("_source_files/") + ) + relative_path_to_source_file = ( + relative_path_to_source_file.removesuffix(".html") + ) + + for ( + source_file + ) in traceability_index.document_tree.source_tree.source_files: + if not source_file.is_referenced: + continue + + if ( + relative_path_to_source_file + == source_file.in_doctree_source_file_rel_path_posix + ): + SourceFileViewHTMLGenerator.export_to_file( + project_config=self.project_config, + source_file=source_file, + traceability_index=traceability_index, + html_templates=self.html_templates, + ) + return + + raise FileNotFoundError + def export_project_statistics( self, traceability_index: TraceabilityIndex, diff --git a/strictdoc/server/routers/main_router.py b/strictdoc/server/routers/main_router.py index e8d04e321..047f7a7b4 100644 --- a/strictdoc/server/routers/main_router.py +++ b/strictdoc/server/routers/main_router.py @@ -2518,11 +2518,23 @@ def get_document(request: Request, url_to_document: str) -> Response: return Response(status_code=304) else: if document_relative_path.relative_path.startswith("_source_files"): - # FIXME: We could be more specific here and only generate the - # requested file. - html_generator.export_source_coverage_screen( - traceability_index=export_action.traceability_index, - ) + if document_relative_path.relative_path.endswith( + "source_coverage.html" + ): + html_generator.export_source_coverage_screen( + traceability_index=export_action.traceability_index, + ) + else: + try: + html_generator.export_single_source_file_screen( + traceability_index=export_action.traceability_index, + path_to_source_file=document_relative_path.relative_path, + ) + except FileNotFoundError: + return HTMLResponse( + content=f"Not Found: {url_to_document}", + status_code=404, + ) elif document_relative_path.relative_path == "index.html": html_generator.export_project_tree_screen( traceability_index=export_action.traceability_index,