1
|
|
|
# Copyright 2022, Red Hat, Inc. |
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
3
|
|
|
|
4
|
1 |
|
import base64 |
5
|
1 |
|
import logging |
6
|
1 |
|
import re |
7
|
1 |
|
from io import BytesIO |
8
|
1 |
|
from pathlib import Path |
9
|
|
|
|
10
|
1 |
|
from jinja2 import Environment, FileSystemLoader |
11
|
|
|
|
12
|
1 |
|
try: |
13
|
1 |
|
from jinja2 import pass_context |
14
|
|
|
except ImportError: |
15
|
|
|
from jinja2 import contextfunction as pass_context |
16
|
|
|
|
17
|
1 |
|
from markupsafe import Markup |
18
|
|
|
|
19
|
1 |
|
from .report_generator import ReportGenerator |
20
|
|
|
|
21
|
|
|
|
22
|
1 |
|
class HTMLReportGenerator(ReportGenerator): |
23
|
1 |
|
def __init__(self, parser): # pylint: disable=W0231 |
24
|
|
|
self.report = parser.parse_report() |
25
|
|
|
self.file_loader = FileSystemLoader(str(Path(__file__).parent / "html_templates")) |
26
|
|
|
self.env = Environment(loader=self.file_loader) |
27
|
|
|
self.env.globals['include_file_in_base64'] = self.include_file_in_base64 |
28
|
|
|
self.env.filters['set_css_for_list'] = self.set_css_for_list |
29
|
|
|
self.env.filters['to_jquery_complaint_id'] = self.to_jquery_complaint_id |
30
|
|
|
self.env.trim_blocks = True |
31
|
|
|
self.env.lstrip_blocks = True |
32
|
|
|
|
33
|
1 |
|
def generate_report(self, debug_setting): |
34
|
|
|
template = self.env.get_template("template_report.html") |
35
|
|
|
html_report = template.render(report=self.report, debug_setting=debug_setting) |
36
|
|
|
if debug_setting.no_minify: |
37
|
|
|
return BytesIO(html_report.encode()) |
38
|
|
|
minified_html_report = re.sub(r'>\s+<', '><', html_report) |
39
|
|
|
return BytesIO(minified_html_report.encode()) |
40
|
|
|
|
41
|
1 |
|
@staticmethod |
42
|
1 |
|
@pass_context |
43
|
1 |
|
def include_file_in_base64(context, relative_path): |
44
|
|
|
real_path = Path(context.environment.loader.searchpath[0]) / relative_path |
45
|
|
|
if "RedHat" in relative_path: |
46
|
|
|
real_path = Path(relative_path) |
47
|
|
|
if not real_path.exists(): |
48
|
|
|
logging.info("Please, install font: %s", real_path.name) |
49
|
|
|
return Markup("NO-FONT-DATA") |
50
|
|
|
base64_data = None |
51
|
|
|
with open(real_path, "rb") as file_data: |
52
|
|
|
base64_data = (base64.b64encode(file_data.read())).decode('utf-8') |
53
|
|
|
return Markup(base64_data) |
54
|
|
|
|
55
|
1 |
|
@staticmethod |
56
|
1 |
|
def set_css_for_list(data): |
57
|
|
|
out = data.replace("<ul>", "<ul class=\"pf-c-list\">") |
58
|
|
|
out = out.replace("<ol>", "<ol class=\"pf-c-list\">") |
59
|
|
|
return out |
60
|
|
|
|
61
|
1 |
|
@staticmethod |
62
|
1 |
|
def to_jquery_complaint_id(data): |
63
|
|
|
return re.sub(r"\.|:", "", data) |
64
|
|
|
|