File: md.py

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (51 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import utils.srg_export.data


def get_heading(row: dict) -> str:
    if row['STIGID'] != '':
        return row['STIGID']
    else:
        return row['SRGID']


def get_content(header: str, row: dict) -> str:
    if row[header] is None:
        return ''
    else:
        content = row[header]
        content = content.replace('\n', '\n\n')
        content = content.replace('*', '*')
        return content


def write_md_file(output, output_path):
    with open(output_path, 'w') as f:
        output_lines = '\n'.join(output)
        f.write(output_lines)


def handle_dict(data: list, output_path: str, title: str) -> None:
    """
    Given a dict with the fields for the srg export, create a formatted Markdown file

    To convert to a DOCX
    sudo dnf install pandoc texlive
    pip3 install git+https://github.com/pandocker/pandoc-docx-pagebreak-py
    pandoc -f markdown -t docx -o docx.docx --filter=pandoc-docx-pagebreakpy output.md

    """
    output = []
    for row in data:
        heading = get_heading(row)
        output.append(f'# {heading}')
        output.append('')
        for _, header in utils.srg_export.data.COLUMN_MAPPINGS.items():
            output.append(f'## {header}')
            content = get_content(header, row)
            output.append(content)
            output.append('')
        output.append('')
        output.append('\\newpage')
        output.append('')

    write_md_file(output, output_path)