Coverage for / var / devmt / py / ghmdlib_0.1.0 / ghmdlib / libs / _offline.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-05 12:04 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-05 12:04 +0000
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4:Purpose: This module provides the functionality for **offline**
5 conversions.
7:Platform: Linux/Windows | Python 3.10+
8:Developer: J Berendt
9:Email: development@s3dev.uk
11:Comments: This module is designed for internal use only.
13"""
14# pylint: disable=wrong-import-order
16import logging
17import mdtex2html
18import os
19from glob import glob
21# Silence debugging output from the markdown converter.
22logging.getLogger('MARKDOWN').setLevel(logging.ERROR)
25class Offline:
26 """This class provides the functions used for offline conversions.
28 Specifically, this class uses locally cached CSS files and local
29 libraries for Markdown to HTML conversions, rather than using the
30 GitHub API.
32 """
34 _DIR_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
35 _DIR_RESC = os.path.join(_DIR_ROOT, 'resources')
36 _EXTS = ["admonition",
37 "codehilite",
38 "fenced_code",
39 "footnotes",
40 "smarty",
41 "tables",
42 "toc"]
44 @classmethod
45 def convert(cls, content: str) -> str:
46 """Convert Markdown file(s) to HTML.
48 Args:
49 content (str): Markdown file content to be converted.
51 Returns:
52 str: A string containing the Markdown content converted to
53 HTML.
55 """
56 return mdtex2html.convert(content, extensions=cls._EXTS)
58 @classmethod
59 def set_css(cls, theme: str) -> str:
60 """Get and set the CSS for the HTML output.
62 Args:
63 theme (str): Theme to be used (dark | light).
65 Returns:
66 str: A string containing either the CSS for the desired
67 theme.
69 """
70 css = ''
71 files = glob(os.path.join(cls._DIR_RESC, f'*-{theme}.min.css'))
72 if not files:
73 msg = (f'A CSS file cannot be found for the theme: {theme}.\n'
74 'Please use the --download CLI utility to obtain the CSS file from GitHub.')
75 raise FileNotFoundError(msg)
76 with open(files[0], encoding='utf-8') as f:
77 css = f'<style>{f.read()}</style>'
78 return css