diff --git a/scanpipe/views.py b/scanpipe/views.py index 5cd240866..c5aee95b0 100644 --- a/scanpipe/views.py +++ b/scanpipe/views.py @@ -62,6 +62,8 @@ import xlsxwriter from django_filters.views import FilterView from packageurl.contrib.django.models import PACKAGE_URL_FIELDS +from licensedcode.spans import Span +from matchcode_toolkit.fingerprinting import get_line_by_pos from scancodeio.auth import ConditionalLoginRequired from scancodeio.auth import conditional_login_required @@ -1931,6 +1933,28 @@ def get_context_data(self, **kwargs): annotations = self.get_annotations(getattr(resource, field_name), value_key) context["detected_values"][field_name] = annotations + # convert qspan from list of ints to Spans + matched_snippet_annotations = [] + matched_snippets = resource.extra_data.get("matched_snippets") + if matched_snippets: + # tokenize file content and map tokens to line numbers + line_by_pos = get_line_by_pos(resource.file_content) + last_pos = max(line_by_pos, key=line_by_pos.get) + for matched_snippet in matched_snippets: + qspan = Span(matched_snippet["qspan"]) + matched_snippet_annotations.append + matched_snippet["qspan"] = qspan + for span in qspan.subspans(): + # Convert qstart and qends to start_line and end_lines + end = min(span.end, last_pos) + matched_snippet_annotations.append( + { + "start_line": line_by_pos[span.start], + "end_line": line_by_pos[end], + } + ) + + context["detected_values"]["matched_snippets"] = matched_snippet_annotations return context diff --git a/setup.cfg b/setup.cfg index 485d600b0..276d6b323 100644 --- a/setup.cfg +++ b/setup.cfg @@ -98,7 +98,7 @@ install_requires = # Font Awesome fontawesomefree==6.6.0 # MatchCode-toolkit - matchcode-toolkit @ git+https://github.com/aboutcode-org/matchcode-toolkit.git@3881f224d4ee56e95b3c15e34673c6844c07f4f5 + matchcode-toolkit @ git+https://github.com/aboutcode-org/matchcode-toolkit.git@007ca57509ab73dda391af8631616b59e35eb9ef # Univers univers==30.12.1 # Markdown