Skip to content

Commit

Permalink
use wagtail-markdown instead of custom code (fsr-de#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeriox authored and DrEGZo committed Sep 8, 2022
1 parent 9df7619 commit a1340f4
Show file tree
Hide file tree
Showing 34 changed files with 159 additions and 1,183 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ jobs:
run: |
sudo apt-get install gettext
source env/bin/activate
poetry install -E pgsql
poetry run python -m pip install setuptools -U
poetry install -E mysql -E pgsql
python install-bootstrap.py
- name: Prepare files for test run
continue-on-error: true
Expand All @@ -100,7 +101,7 @@ jobs:
- name: Setup postgres dependency
run: |
source env/bin/activate
poetry add psycopg2
poetry run python -m pip install psycopg2
if: matrix.database == 'postgres'

#- name: Migrate db
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,3 @@ def extendMarkdown(self, md):
"InternalLinkPattern",
200,
)


def makeExtension():
return MinuteExtension()
25 changes: 4 additions & 21 deletions myhpi/wagtail_markdown/fields.py → myhpi/core/markdown/fields.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
# vim:sw=4 ts=4 et:
# Copyright (c) 2015 Torchbox Ltd.
# [email protected] 2015-09-14
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely. This software is provided 'as-is', without any express or implied
# warranty.
#

import html2text
from django.db.models import TextField
from wagtail_localize.segments import (
OverridableSegmentValue,
StringSegmentValue,
Expand All @@ -18,19 +7,13 @@
from wagtail_localize.segments.extract import quote_path_component
from wagtail_localize.segments.ingest import organise_template_segments
from wagtail_localize.strings import extract_strings, restore_strings
from wagtailmarkdown.fields import MarkdownField
from wagtailmarkdown.utils import render_markdown

from .utils import render_markdown
from .widgets import MarkdownTextarea


class MarkdownField(TextField):
def formfield(self, **kwargs):
defaults = {"widget": MarkdownTextarea}
defaults.update(kwargs)
return super(MarkdownField, self).formfield(**defaults)

class CustomMarkdownField(MarkdownField):
def get_translatable_segments(self, value):
template, strings = extract_strings(render_markdown(value, with_abbreveations=False)[0])
template, strings = extract_strings(render_markdown(value))

# Find all unique href values
hrefs = set()
Expand Down
37 changes: 37 additions & 0 deletions myhpi/core/markdown/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import markdown
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
from wagtailmarkdown.utils import _get_markdown_kwargs, _sanitise_markdown_html

from myhpi.core.markdown.extensions import MinuteExtension


def render_markdown(text, context=None, with_abbreveations=True):
"""
Turn markdown into HTML.
"""
if context is None or not isinstance(context, dict):
context = {}
markdown_html, toc = _transform_markdown_into_html(text, with_abbreveations=with_abbreveations)
sanitised_markdown_html = _sanitise_markdown_html(markdown_html)
return mark_safe(sanitised_markdown_html), mark_safe(toc)


def _transform_markdown_into_html(text, with_abbreveations):
from myhpi.core.models import AbbreviationExplanation

markdown_kwargs = _get_markdown_kwargs()
markdown_kwargs["extensions"].append(
MinuteExtension()
) # should be in settings.py, but module lookup doesn't work
md = markdown.Markdown(**markdown_kwargs)
abbreveations = "\n" + (
"\n".join(
[
f"*[{abbr.abbreviation}]: {abbr.explanation}"
for abbr in AbbreviationExplanation.objects.all()
]
)
)
text = smart_str(text) + abbreveations if with_abbreveations else smart_str(text)
return md.convert(text), md.toc
7 changes: 3 additions & 4 deletions myhpi/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import django.db.models.deletion
import modelcluster.contrib.taggit
import modelcluster.fields
import wagtailmarkdown.fields
from django.conf import settings
from django.db import migrations, models

import myhpi.wagtail_markdown.fields


class Migration(migrations.Migration):

Expand Down Expand Up @@ -111,7 +110,7 @@ class Migration(migrations.Migration):
to="core.basepage",
),
),
("body", myhpi.wagtail_markdown.fields.MarkdownField()),
("body", wagtailmarkdown.fields.MarkdownField()),
("author_visible", models.BooleanField()),
],
options={
Expand All @@ -134,7 +133,7 @@ class Migration(migrations.Migration):
),
),
("date", models.DateField()),
("text", myhpi.wagtail_markdown.fields.MarkdownField()),
("text", wagtailmarkdown.fields.MarkdownField()),
(
"author",
models.ForeignKey(
Expand Down
11 changes: 5 additions & 6 deletions myhpi/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
from wagtail.core.models import Page, Site
from wagtail.search import index

from myhpi.core.markdown.fields import CustomMarkdownField
from myhpi.core.utils import get_user_groups
from myhpi.wagtail_markdown.edit_handlers import MarkdownPanel
from myhpi.wagtail_markdown.fields import MarkdownField


class BasePage(Page):
Expand All @@ -38,11 +37,11 @@ class BasePage(Page):


class InformationPage(BasePage):
body = MarkdownField()
body = CustomMarkdownField()
author_visible = BooleanField()

content_panels = Page.content_panels + [
MarkdownPanel("body", classname="full"),
FieldPanel("body", classname="full"),
]
parent_page_types = [
"FirstLevelMenuItem",
Expand Down Expand Up @@ -157,15 +156,15 @@ class Minutes(BasePage):
author = ForeignKey(User, on_delete=models.PROTECT, related_name="author")
participants = ParentalManyToManyField(User, related_name="minutes")
labels = ClusterTaggableManager(through=TaggedMinutes, blank=True)
text = MarkdownField()
text = CustomMarkdownField()

content_panels = Page.content_panels + [
FieldPanel("date"),
FieldPanel("moderator"),
FieldPanel("author"),
FieldPanel("participants", widget=UserSelectWidget),
FieldPanel("labels"),
MarkdownPanel("text", classname="full"),
FieldPanel("text"),
]
parent_page_types = ["MinutesList"]
subpage_types = []
Expand Down
2 changes: 1 addition & 1 deletion myhpi/core/templates/core/information_page.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load wagtailmarkdown %}
{% load core_extras %}

{% block content %}
{% with page.body|markdown as parsed_md %}
Expand Down
2 changes: 1 addition & 1 deletion myhpi/core/templates/core/minutes.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}
{% load static %}
{% load wagtailmarkdown %}
{% load core_extras %}
{% load minutes_tags %}

{% block content %}
Expand Down
7 changes: 7 additions & 0 deletions myhpi/core/templatetags/core_extras.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django import template

from myhpi.core.markdown.utils import render_markdown

register = template.Library()


Expand Down Expand Up @@ -41,3 +43,8 @@ def format_id_history(id_history):
if not id_history:
return "root"
return str(id_history[-1])


@register.filter(name="markdown")
def markdown(value):
return render_markdown(value)
5 changes: 2 additions & 3 deletions myhpi/polls/migrations/0002_auto_20210612_1217.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Generated by Django 3.1.8 on 2021-06-12 10:17

import wagtailmarkdown.fields
from django.db import migrations, models

import myhpi.wagtail_markdown.fields


class Migration(migrations.Migration):

Expand All @@ -15,7 +14,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name="poll",
name="description",
field=myhpi.wagtail_markdown.fields.MarkdownField(default=""),
field=wagtailmarkdown.fields.MarkdownField(default=""),
preserve_default=False,
),
migrations.AddField(
Expand Down
7 changes: 3 additions & 4 deletions myhpi/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
from wagtail.core.models import Orderable, Page
from wagtail.search import index

from myhpi.core.markdown.fields import CustomMarkdownField
from myhpi.core.models import BasePage
from myhpi.wagtail_markdown.edit_handlers import MarkdownPanel
from myhpi.wagtail_markdown.fields import MarkdownField


class PollList(BasePage):
Expand All @@ -30,7 +29,7 @@ def get_context(self, request, *args, **kwargs):

class Poll(BasePage):
question = models.CharField(max_length=254)
description = MarkdownField()
description = CustomMarkdownField()
start_date = models.DateField()
end_date = models.DateField()
max_allowed_answers = models.IntegerField(default=1)
Expand All @@ -39,7 +38,7 @@ class Poll(BasePage):
participants = models.ManyToManyField(User, related_name="polls")

content_panels = Page.content_panels + [
MarkdownPanel("description", classname="full"),
FieldPanel("description", classname="full"),
FieldPanel("question"),
FieldPanel("start_date"),
FieldPanel("end_date"),
Expand Down
2 changes: 1 addition & 1 deletion myhpi/polls/templates/polls/poll.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load wagtailmarkdown %}
{% load core_extras %}

{% block content %}
<div class="row">
Expand Down
8 changes: 7 additions & 1 deletion myhpi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
"wagtail.users",
"wagtail_localize",
"wagtail_localize.locales",
"wagtailmarkdown",
"myhpi.core",
"myhpi.polls",
"myhpi.search",
"myhpi.wagtail_markdown",
"static_precompiler",
]

Expand Down Expand Up @@ -229,6 +229,12 @@
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
INTERNAL_IPS = env.str("INTERNAL_IPS")

WAGTAILMARKDOWN = {
"allowed_tags": ["abbr"],
"allowed_attributes": {"abbr": ["title"]},
"extensions": ["toc", "abbr"],
}

ENABLE_MAILING_LISTS = env.bool("ENABLE_MAILING_LISTS", False)

# The mailing lists library (Tenca) has a django-like settings module.
Expand Down
3 changes: 0 additions & 3 deletions myhpi/tenca_django/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import urllib.parse

from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
Expand Down
2 changes: 1 addition & 1 deletion myhpi/tests/test_minutes_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

django.setup()

from myhpi.wagtail_markdown.minutes_extensions import EnterLeavePreprocessor
from myhpi.core.markdown.extensions import EnterLeavePreprocessor


class TestMinuteExtensions(TestCase):
Expand Down
17 changes: 0 additions & 17 deletions myhpi/wagtail_markdown/edit_handlers.py

This file was deleted.

Empty file.
48 changes: 0 additions & 48 deletions myhpi/wagtail_markdown/mdx/linker/__init__.py

This file was deleted.

Empty file.
Loading

0 comments on commit a1340f4

Please sign in to comment.