pelican-plugins / render-math

Pelican plugin that renders mathematics via the MathJax JavaScript engine
Other
30 stars 7 forks source link

In Python 3.12, distutils is removed, causing Typogrify to be ignored #25

Closed gabindu closed 3 weeks ago

gabindu commented 3 months ago

Since Python 3.12, the distutils package is removed (see https://docs.python.org/3.12/whatsnew/3.10.html#distutils-deprecated), which leads to a surprising problem with Typogrify (took me a while to trace this!), which (even if correctly installed) is not recognized. The culprit is the line marked as "Here lies the rub!!!" in this code in render_math.py:

def configure_typogrify(pelicanobj, mathjax_settings):
    """Instructs Typogrify to ignore math tags - which allows Typogrify
    to play nicely with math related content"""

    # If Typogrify is not being used, then just exit
    if not pelicanobj.settings.get('TYPOGRIFY', False):
        return

    try:
        import typogrify
        from distutils.version import LooseVersion    # Here lies the rub!!!!

        if LooseVersion(typogrify.__version__) < LooseVersion('2.0.7'):
            raise TypeError('Incorrect version of Typogrify')

        from typogrify.filters import typogrify

        # At this point, we are happy to use Typogrify, meaning
        # it is installed and it is a recent enough version
        # that can be used to ignore all math
        # Instantiate markdown extension and append it to the current extensions
        pelicanobj.settings['TYPOGRIFY_IGNORE_TAGS'].extend(['.math', 'script'])  # ignore math class and script

    except (ImportError, TypeError) as e:
        pelicanobj.settings['TYPOGRIFY'] = False  # disable Typogrify

        if isinstance(e, ImportError):
            print("\nTypogrify is not installed, so it is being ignored.\nIf you want to use it, please install via: pip install typogrify\n")

        if isinstance(e, TypeError):
            print("\nA more recent version of Typogrify is needed for the render_math module.\nPlease upgrade Typogrify to the latest version (anything equal or above version 2.0.7 is okay).\nTypogrify will be turned off due to this reason.\n")

The command from distutils.version import LooseVersion fails with an ImportError, causing the (incorrect) error message that Typogrify is not installed.

I'm not an expert, but I think a quick solution could be to use the packaging package and its Version command:

    try:
        import typogrify
        from packaging.version import Version

        if Version(typogrify.__version__) < Version('2.0.7'):
            raise TypeError('Incorrect version of Typogrify')
justinmayer commented 3 weeks ago

This issue was fixed via #24, and the fix was released in Render Math 1.0.4.