Skip to content

Commit

Permalink
Merge branch 'main' of github.com:laws-africa/peachjam
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmwangemi committed Dec 11, 2023
2 parents 94ac6c7 + c56ce72 commit 26d081f
Show file tree
Hide file tree
Showing 12 changed files with 978 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# LibreOffice
RUN apt-get update && apt-get install -y libreoffice poppler-utils
RUN apt-get update && apt-get install -y libreoffice poppler-utils pandoc

# Production-only dependencies
RUN pip install psycopg2==2.9.3 gunicorn==20.1.0
Expand Down
20 changes: 19 additions & 1 deletion peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django.utils.translation import gettext_lazy
from import_export.admin import ImportExportMixin as BaseImportExportMixin
from languages_plus.models import Language
from martor.utils import markdownify
from nonrelated_inlines.admin import NonrelatedTabularInline
from treebeard.admin import TreeAdmin
from treebeard.forms import MoveNodeForm, movenodeform_factory
Expand Down Expand Up @@ -984,7 +985,24 @@ class GazetteAdmin(DocumentAdmin):

@admin.register(Book)
class BookAdmin(DocumentAdmin):
pass
fieldsets = copy.deepcopy(DocumentAdmin.fieldsets)
fieldsets[3][1]["fields"].insert(3, "content_markdown")

class Media:
js = (
"https://cdn.jsdelivr.net/npm/@lawsafrica/law-widgets@latest/dist/lawwidgets/lawwidgets.js",
)

def save_model(self, request, obj, form, change):
if "content_markdown" in form.changed_data:
obj.content_html = markdownify(form.cleaned_data["content_markdown"])

resp = super().save_model(request, obj, form, change)

if "content_markdown" in form.changed_data:
obj.extract_citations()

return resp


@admin.register(Journal)
Expand Down
23 changes: 23 additions & 0 deletions peachjam/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from functools import wraps

import martor.utils
from django.utils.translation import get_language_from_request
from languages_plus.models import Language

Expand Down Expand Up @@ -78,3 +79,25 @@ def parse_utf8_html(html):

parser = lxml.html.HTMLParser(encoding="utf-8")
return lxml.html.fromstring(html, parser=parser)


def markdownify(text):
"""Convert markdown text to html using pandoc on the commandline."""
with tempfile.NamedTemporaryFile(suffix=".md") as inf:
with tempfile.NamedTemporaryFile(suffix=".html") as outf:
inf.write(text.encode("utf-8"))
inf.flush()
cmd = [
"pandoc",
"--from=markdown",
"--to=html",
"--output",
outf.name,
inf.name,
]
subprocess.run(cmd, check=True)
return outf.read().decode("utf-8")


# override martor's markownify to use pandoc, so that we get alpha-numbered list support
martor.utils.markdownify = markdownify
19 changes: 19 additions & 0 deletions peachjam/migrations/0113_book_content_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.20 on 2023-11-20 10:34

import martor.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("peachjam", "0112_peachjamsettings_pagerank_pivot_value"),
]

operations = [
migrations.AddField(
model_name="book",
name="content_markdown",
field=martor.models.MartorField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions peachjam/models/journals_books.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.db import models
from martor.models import MartorField

from peachjam.models import CoreDocument, DocumentNature


class Book(CoreDocument):
publisher = models.CharField(max_length=2048)
content_markdown = MartorField(blank=True, null=True)

def pre_save(self):
self.frbr_uri_doctype = "doc"
Expand Down
20 changes: 20 additions & 0 deletions peachjam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"polymorphic",
"drf_spectacular",
"django_advanced_password_validation",
"martor",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -508,6 +509,8 @@
"Underline",
"Strike",
"Blockquote",
"Superscript",
"Subscript",
"SpellChecker",
"Undo",
"Redo",
Expand Down Expand Up @@ -568,3 +571,20 @@
# Each item in the list should be a tuple of (Full name, email address). Example:
# [('Someone', '[email protected]')]
ADMINS = []

# django-markdown-editor
# https://github.com/agusmakmun/django-markdown-editor
MARTOR_UPLOAD_URL = ""
MARTOR_SEARCH_USERS_URL = ""
MARTOR_ENABLE_LABEL = True
MARTOR_ENABLE_CONFIGS = {
"emoji": "true", # to enable/disable emoji icons.
"imgur": "false", # to enable/disable imgur/custom uploader.
"mention": "false", # to enable/disable mention
"jquery": "true", # to include/revoke jquery (require for admin default django)
"living": "false", # to enable/disable live updates in preview
"spellcheck": "false", # to enable/disable spellcheck in form textareas
"hljs": "false", # to enable/disable hljs highlighting in preview
}
# disable the normal martor theme which pulls in another bootstrap version
MARTOR_ALTERNATIVE_CSS_FILE_THEME = "martor/css/peachjam.css"
1 change: 1 addition & 0 deletions peachjam/static/martor/css/martor-admin.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// deliberately empty to prevent the martor-admin.min.css file from martor from loading since it messes with our styles
4 changes: 4 additions & 0 deletions peachjam/static/martor/css/peachjam.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.ace_heading {
font-weight: bold;
color: darkgreen;
}
Loading

0 comments on commit 26d081f

Please sign in to comment.