datatracker/ietf/utils/markdown.py
Robert Sparks 8cb7f3dcae
feat: Import IESG artifacts into the datatracker (#6908)
* chore: remove unused setting

* feat: initial import of iesg minutes

* fix: let the meetings view show older iesg meetings

* feat: iesg narrative minutes

* feat: import bof coordination call minutes

* wip: import commands for iesg appeals and statements

* feat: import iesg statements.

* feat: import iesg artifacts

* feat: many fewer n+1 queries for the group meetings view

* fix: restore chain of elifs in views_doc

* fix: use self.stdout.write vs print in mgmt commands

* fix: use replace instead of astimezone when appropriate

* chore: refactor new migrations into one

* fix: transcode some old files into utf8

* fix: repair overzealous replace

* chore: black

* fix: address minro review comments

* fix: actually capture transcoding work

* fix: handle multiple iesg statements on the same day

* fix: better titles

* feat: pill badge replaced statements

* fix: consolodate source repos to one

* feat: liberal markdown for secretariat controlled content

* fix: handle (and clean) html narrative minutes

* feat: scrub harder

* fix: simplify and improve a scrubber

* chore: reorder migrations
2024-02-20 16:35:08 -06:00

68 lines
2 KiB
Python

# Copyright The IETF Trust 2021, All Rights Reserved
# -*- coding: utf-8 -*-
"""Markdown wrapper
Use this instead of importing markdown directly to guarantee consistent extensions / options through
the datatracker.
"""
import markdown as python_markdown
from markdown.extensions import Extension
from markdown.postprocessors import Postprocessor
from django.utils.safestring import mark_safe
from ietf.doc.templatetags.ietf_filters import urlize_ietf_docs
from ietf.utils.text import bleach_cleaner, liberal_bleach_cleaner, bleach_linker
class LinkifyExtension(Extension):
"""
Simple Markdown extension inspired by https://github.com/daGrevis/mdx_linkify,
but using our bleach_linker directly. Doing the linkification on the converted
Markdown output introduces artifacts.
"""
def extendMarkdown(self, md):
md.postprocessors.register(LinkifyPostprocessor(md), "linkify", 50)
# disable automatic links via angle brackets for email addresses
md.inlinePatterns.deregister("automail")
# "autolink" for URLs does not seem to cause issues, so leave it on
class LinkifyPostprocessor(Postprocessor):
def run(self, text):
return urlize_ietf_docs(bleach_linker.linkify(text))
def markdown(text):
return mark_safe(
bleach_cleaner.clean(
python_markdown.markdown(
text,
extensions=[
"extra",
"nl2br",
"sane_lists",
"toc",
LinkifyExtension(),
],
)
)
)
def liberal_markdown(text):
return mark_safe(
liberal_bleach_cleaner.clean(
python_markdown.markdown(
text,
extensions=[
"extra",
"nl2br",
"sane_lists",
"toc",
LinkifyExtension(),
],
)
)
)