Coverage for src/markdown_katex/__main__.py: 32%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

41 statements  

1#!/usr/bin/env python 

2# This file is part of the markdown-katex project 

3# https://github.com/mbarkhau/markdown-katex 

4# 

5# Copyright (c) 2019-2021 Manuel Barkhau (mbarkhau@gmail.com) - MIT License 

6# SPDX-License-Identifier: MIT 

7import sys 

8import json 

9import typing as typ 

10import subprocess as sp 

11 

12import markdown_katex 

13from markdown_katex import html 

14 

15try: 

16 import pretty_traceback 

17 

18 pretty_traceback.install() 

19except ImportError: 

20 pass # no need to fail because of missing dev dependency 

21 

22 

23ExitCode = int 

24 

25 

26def _selftest() -> ExitCode: 

27 # pylint:disable=import-outside-toplevel ; lazy import to improve cli responsiveness 

28 from markdown_katex import wrapper 

29 

30 print("Command options:") 

31 print(json.dumps(wrapper.parse_options(), indent=4)) 

32 print() 

33 

34 html_parts: typ.List[str] = [] 

35 test_formulas = markdown_katex.TEST_FORMULAS 

36 

37 for tex_formula in test_formulas: 

38 html_part = wrapper.tex2html(tex_formula) 

39 if not html_part: 

40 return 1 

41 

42 html_parts.append(html_part) 

43 

44 formula_html = "\n<hr/>\n".join(html_parts) 

45 html_text = html.HTML_TEMPLATE.replace("{{content}}", formula_html) 

46 

47 with open("test.html", mode="wb") as fobj: 

48 fobj.write(html_text.encode("utf-8")) 

49 

50 print("Created 'test.html'") 

51 return 0 

52 

53 

54def main(args: typ.Sequence[str] = sys.argv[1:]) -> ExitCode: 

55 """Basic wrapper around the katex command. 

56 

57 This is mostly just used for self testing. 

58 $ python -m markdown_katex 

59 """ 

60 # pylint:disable=dangerous-default-value ; mypy will catch mutations of args 

61 

62 if "--markdown-katex-selftest" in args: 

63 return _selftest() 

64 

65 bin_cmd = markdown_katex.get_bin_cmd() 

66 

67 if "--version" in args or "-V" in args: 

68 version = markdown_katex.__version__ 

69 bin_str = " ".join(bin_cmd) 

70 print("markdown-katex version: ", version, f"(using binary: {bin_str})") 

71 

72 return sp.check_call(bin_cmd + list(args)) 

73 

74 

75if __name__ == '__main__': 

76 sys.exit(main())