0
votes

I'm following this tutorial to add LaTeX support to markdown, and I get this error:

Environment:

Request Method: GET Request URL: http://localhost/posts/ttttt/

Django Version: 2.2.10 Python Version: 3.7.3 Installed Applications: ['home', 'search', 'info', 'list', 'streams', 'post', 'faq', 'registration', 'snippet', 'wagtailmd', 'wagtailcodeblock', 'wagtail.contrib.forms', 'wagtail.contrib.redirects', 'wagtail.embeds', 'wagtail.sites', 'wagtail.users', 'wagtail.snippets', 'wagtail.documents', 'wagtail.images', 'wagtail.search', 'wagtail.admin', 'wagtail.core', 'wagtail.contrib.modeladmin', 'wagtail.contrib.styleguide', 'modelcluster', 'taggit', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'django.contrib.humanize', 'allauth', 'allauth.account', 'allauth.socialaccount', 'crispy_forms', 'wagtailmenus', 'captcha', 'wagtailcaptcha'] Installed Middleware: ['django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', 'wagtail.core.middleware.SiteMiddleware', 'wagtail.contrib.redirects.middleware.RedirectMiddleware']

Template error: In template C:\Users\abdom\OneDrive\Desktop\project\website\templates\base.html, error at line 0 init() takes 1 positional argument but 2 were given 1 : {% load static wagtailuserbar %} 2 : {% load menu_tags %} 3 : 4 : 5 : 6 :
7 : 8 : 9 : {% block title %} 10 : {% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}

Traceback:

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\wagtail\core\models.py" in _get_response 1254. response = response.render()

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\response.py" in render 106. self.content = self.rendered_content

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\response.py" in rendered_content 83. content = template.render(context, self._request)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\backends\django.py" in render 61. return self.template.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render 171. return self._render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in _render 163. return self.nodelist.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render 937. bit = node.render_annotated(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render_annotated 904. return self.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\loader_tags.py" in render 150. return compiled_parent._render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in _render 163. return self.nodelist.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render 937. bit = node.render_annotated(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render_annotated 904. return self.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\loader_tags.py" in render 62. result = block.nodelist.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render 937. bit = node.render_annotated(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render_annotated 904. return self.render(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in render 987. output = self.filter_expression.resolve(context)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\django\template\base.py" in resolve 698. new_obj = func(obj, *arg_vals)

File "C:\Users\abdom\OneDrive\Desktop\project\wagtailmd\templatetags\wagtailmd.py" in markdown_filter 14. output_format='html5'

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\markdown\core.py" in markdown 387. md = Markdown(**kwargs)

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\markdown\core.py" in init 97. configs=kwargs.get('extension_configs', {}))

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\markdown\core.py" in registerExtensions 123. ext = self.build_extension(ext, configs.get(ext, {}))

File "C:\Users\abdom\OneDrive\Desktop\project.venv\lib\site-packages\markdown\core.py" in build_extension 178. return module.makeExtension(**configs)

File "C:\Users\abdom\OneDrive\Desktop\project\wagtailmd\mdx\mdx_mathjax.py" in makeExtension 23. return MathJaxExtension(configs)

Exception Type: TypeError at /posts/ttttt/ Exception Value: init() takes 1 positional argument but 2 were given

mdx_mathjax.py :

import markdown
import cgi

class MathJaxPattern(markdown.inlinepatterns.Pattern):
    def __init__(self, md):
        markdown.inlinepatterns.Pattern.__init__(
            self, r'(?<!\\)(\$\$?)(.+?)\2', md)

    def handleMatch(self, m):
        # Pass the math code through, unmodified except for basic entity 
        # substitutions. 
        # Stored in htmlStash so it doesn't get further processed by Markdown.
        text = cgi.escape(m.group(2) + m.group(3) + m.group(2))
        return self.markdown.htmlStash.store(text)

class MathJaxExtension(markdown.Extension):
    def extendMarkdown(self, md, md_globals):
        # Needs to come before escape matching because \ is pretty important
        # in LaTeX
        md.inlinePatterns.add('mathjax', MathJaxPattern(md), '<escape')

def makeExtension(configs=[]):
    return MathJaxExtension(configs)

wagtailmd.py :

import markdown
from django import template

register = template.Library()

@register.filter(name='markdown')
def markdown_filter(value):
    return markdown.markdown(
    value,
    extensions=[
        'wagtailmd.mdx.mdx_mathjax',
    ],
    output_format='html5'
)

my post_page.html

{% extends 'base.html' %}
{% load static %}

{% load wagtailcore_tags wagtailimages_tags wagtailmd %}


{% block content %}

<!-- main -->

<main>
    <div class="uk-container uk-container-small" style="margin: 0 auto;">

        <article>
            <div class="math-editor">
                {{ self.math_editor|markdown|safe }}
            </div>
        </article>
    </div>
</main>

<!-- /main -->

{% endblock content %}

{% block extra_js %}
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      extensions: ["tex2jax.js"],
      jax: ["input/TeX", "output/HTML-CSS"],
      tex2jax: {
        inlineMath: [['$','$']],
        displayMath: [['$$','$$']] ,
        processEscapes: true
      },
      "HTML-CSS": { availableFonts: ["TeX"] }
    });
  </script>

  <script type="text/javascript"
     src="//cdn.mathjax.org/mathjax/latest/MathJax.js">
  </script>
{% endblock %}

thanks.

1
Please provide the stack trace for your error. What you have provided now is not enough to see where the problem is. You should also include your usage of the template tag, e.g your html file.nima
Why instead of this markdown.inlinepatterns.Pattern.__init__(...) just havent use super like this super().__init__(r'(?<!\\)(\$\$?)(.+?)\2', md)?Charnel
@nima , I updated the question, thanks for your replayDAMAR225

1 Answers

2
votes

You have an implementation error in your code. According to markdown's documentation, you should define what parameters you want to provide to your custom Extension.

And if you do not have any configurations yet, you should use something like below:

def makeExtension(configs={}):
    return MathJaxExtension(**configs)

The complete implementation should look something like this:

import markdown
import cgi

class MathJaxPattern(markdown.inlinepatterns.Pattern):
    def __init__(self, md):
        markdown.inlinepatterns.Pattern.__init__(
            self, r'(?<!\\)(\$\$?)(.+?)\2', md)

    def handleMatch(self, m):
        # Pass the math code through, unmodified except for basic entity 
        # substitutions. 
        # Stored in htmlStash so it doesn't get further processed by Markdown.
        text = cgi.escape(m.group(2) + m.group(3) + m.group(2))
        return self.markdown.htmlStash.store(text)

class MathJaxExtension(markdown.Extension):
    def __init__(self, **kwargs):
        self.config = {'option1' : ['value1', 'description1'],
                       'option2' : ['value2', 'description2'] }
        super(MathJaxExtension, self).__init__(**kwargs)

    def extendMarkdown(self, md, md_globals):
        # Needs to come before escape matching because \ is pretty important
        # in LaTeX
        md.inlinePatterns.add('mathjax', MathJaxPattern(md), '<escape')

def makeExtension(configs={}):
    return MathJaxExtension(**configs)