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

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4:Purpose: This module provides the functionality for **offline** 

5 conversions. 

6 

7:Platform: Linux/Windows | Python 3.10+ 

8:Developer: J Berendt 

9:Email: development@s3dev.uk 

10 

11:Comments: This module is designed for internal use only. 

12 

13""" 

14# pylint: disable=wrong-import-order 

15 

16import logging 

17import mdtex2html 

18import os 

19from glob import glob 

20 

21# Silence debugging output from the markdown converter. 

22logging.getLogger('MARKDOWN').setLevel(logging.ERROR) 

23 

24 

25class Offline: 

26 """This class provides the functions used for offline conversions. 

27 

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. 

31 

32 """ 

33 

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"] 

43 

44 @classmethod 

45 def convert(cls, content: str) -> str: 

46 """Convert Markdown file(s) to HTML. 

47 

48 Args: 

49 content (str): Markdown file content to be converted. 

50 

51 Returns: 

52 str: A string containing the Markdown content converted to 

53 HTML. 

54 

55 """ 

56 return mdtex2html.convert(content, extensions=cls._EXTS) 

57 

58 @classmethod 

59 def set_css(cls, theme: str) -> str: 

60 """Get and set the CSS for the HTML output. 

61 

62 Args: 

63 theme (str): Theme to be used (dark | light). 

64 

65 Returns: 

66 str: A string containing either the CSS for the desired 

67 theme. 

68 

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