File: common.py

package info (click to toggle)
pymarkups 4.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: python: 1,156; sh: 7; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,601 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
# This file is part of python-markups module
# License: 3-clause BSD, see LICENSE file
# Copyright: (C) Dmitry Shachnev, 2012-2025

import os.path

# Some common constants and functions
(LANGUAGE_HOME_PAGE, MODULE_HOME_PAGE, SYNTAX_DOCUMENTATION) = range(3)
CONFIGURATION_DIR = (
    os.getenv("XDG_CONFIG_HOME")
    or os.getenv("APPDATA")
    or os.path.expanduser("~/.config")
)
MATHJAX2_LOCAL_FILES = (
    "/usr/share/javascript/mathjax/MathJax.js",  # Debian libjs-mathjax
    "/usr/share/mathjax2/MathJax.js",  # Arch Linux mathjax2
)
MATHJAX3_LOCAL_FILES = (
    "/usr/share/nodejs/mathjax-full/es5/tex-chtml.js",  # Debian node-mathjax-full
    "/usr/share/mathjax/tex-chtml.js",  # Arch Linux mathjax
)
MATHJAX_WEB_URL = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"

PYGMENTS_STYLE = "default"


def get_pygments_stylesheet(selector: str | None, style: str | None = None) -> str:
    if style is None:
        style = PYGMENTS_STYLE
    if style == "":
        return ""
    try:
        from pygments.formatters import HtmlFormatter
    except ImportError:
        return ""
    else:
        defs = HtmlFormatter(style=style).get_style_defs(selector)
        assert isinstance(defs, str)
        return defs + "\n"


def get_mathjax_url_and_version(webenv: bool) -> tuple[str, int]:
    if not webenv:
        for path in MATHJAX3_LOCAL_FILES:
            if os.path.exists(path):
                return f"file://{path}", 3
        for path in MATHJAX2_LOCAL_FILES:
            if os.path.exists(path):
                return f"file://{path}", 2
    return MATHJAX_WEB_URL, 3