from django import template
from django.template.defaultfilters import stringfilter

import markdown as md
from markdown.extensions import Extension

import calendar

register = template.Library()

# See https://python-markdown.github.io/change_log/release-3.0/#safe_mode-and-html_replacement_text-keywords-deprecated
class EscapeHtml(Extension):
    def extendMarkdown(self, md):
        md.preprocessors.deregister('html_block')
        md.inlinePatterns.deregister('html')

@register.filter
@stringfilter
def markdown(value):
    return md.markdown(value, extensions=[EscapeHtml(), 'nl2br', 'fenced_code'])

@register.filter
def month_name(value):
    return calendar.month_name[value]