blog

Apply temporary hack/fix for Pandoc bug

When Pandoc sees a link like this: [[https://website.com/image.jpg][text that should be displayed]], it blatantly removes the text, and just puts the image inline, same for .png links. I've inserted a hack that changes its behaviour to what it should be, awaiting a future Pandoc release with a bug fix.

Author
Maarten 'Vngngdn' Vangeneugden
Date
April 11, 2018, 8:12 p.m.
Hash
face3a0fcd24330d1e428e6e283a827c72390ec6
Parent
822ac1493a49d83d8ea46ae148399b2f4df133b3
Modified file
views.py

views.py

24 additions and 3 deletions.

View changes Hide changes
1
1
import subprocess
2
2
3
3
from django.utils.translation import ugettext as _
4
4
from django.shortcuts import get_object_or_404, render # This allows to render the template with the view here. It's pretty cool and important.
5
5
from django.http import HttpResponseRedirect, HttpResponse
6
6
from django.core.urlresolvers import reverse
7
7
from django.template import loader # This allows to actually load the template.
8
8
from .models import *
9
9
from .forms import CommentForm
10
10
from django.core.exceptions import ObjectDoesNotExist
11
11
from django.utils import translation
12
12
13
13
GERMAN = "de"
14
14
SPANISH = "es"
15
15
FRENCH = "fr"
16
16
DUTCH = "nl"
17
17
ENGLISH = "en"
18
18
19
19
footer_links = [
20
20
        [_("Back to main page"), "/blog"],
21
21
        [_("Contact"), "mailto:maarten.vangeneugden@student.uhasselt.be"],
22
22
        ]
23
23
footer_description = _("Maarten's personal blog, with sprinkles and a dollop of healthy bugs.")
24
24
25
25
def org_to_html(file_path):
26
26
    """ Converts the given org formatted file to HTML.
27
27
    This function directly returns the resulting HTML code. This function uses
28
28
    the amazing Haskell library Pandoc to convert the file (and takes care
29
29
    of header id's and all that stuff).
30
30
    """
31
31
    # FIXME: Remove hardcoded link to media. Replace with media tag!
32
32
    return subprocess.check_output(["pandoc", "--from=org", "--to=html", "/srv/django/website/media/"+file_path])
33
-
+
33
    # to .jpgPANDOCBUG][ and .pngPANDOCBUG][, is because of a Pandoc bug that
+
34
    # removes the text links for images. It is afterwards converted back, no
+
35
    # worries.
+
36
    file = open("/srv/django/website/media/"+file_path, "r", encoding="utf-8")
+
37
    text = file.read()
+
38
    file.close()
+
39
    text = text.replace(".jpg][", ".jpgPANDOCBUG][")
+
40
    text = text.replace(".png][", ".pngPANDOCBUG][")
+
41
    file = open("/tmp/blog-file.org", "w", encoding="utf-8")
+
42
    file.write(text)
+
43
    file.close()
+
44
    html_text = subprocess.check_output(["pandoc", "--from=org", "--to=html","/tmp/blog-file.org"])
+
45
    html_text = html_text.replace(".jpgPANDOCBUG", ".jpg")
+
46
    html_text = html_text.replace(".pngPANDOCBUG", ".png")
+
47
    return html_text
+
48
34
49
def get_available_post_languages(post):
35
50
    """ Returns the language codes for which a blog post exists. This function
36
51
    always returns English (because that field mustn't be empty).
37
52
    So say a blog post has an English, Dutch and French version (which means
38
53
    english_file, french_file and dutch_file aren't empty), the function will return {"en",
39
54
    "fr", "nl"}. """
40
55
    available_languages = {ENGLISH}
41
56
    if post.german_file != "":
42
57
        available_languages.add(GERMAN)
43
58
    if post.spanish_file != "":
44
59
        available_languages.add(SPANISH)
45
60
    if post.french_file != "":
46
61
        available_languages.add(FRENCH)
47
62
    if post.dutch_file != "":
48
63
        available_languages.add(DUTCH)
49
64
    return available_languages
50
65
51
66
def index(request):
52
67
    template = "blog/index.html"
53
68
    posts = Post.objects.all()
54
69
    language = translation.get_language()
55
70
56
71
    post_links = []
57
72
    for post in posts:
58
73
        blog_file = post.text_file()
59
74
        blog_text = org_to_html(blog_file.name)
60
75
        # TODO: The link can possibly be reversed in the DTL using the title, which is actually
61
76
        # a cleaner way to do it. Investigate.
62
77
        link = reverse("blog-post-language", args=[language, post.slug()])
63
78
        post_links.append([post.title(), post.published, blog_text, link])
64
79
65
80
    context = {
66
81
            'posts': post_links,
67
82
            'materialDesign_color': "brown",
68
83
            'materialDesign_accentColor': "yellow",
69
84
            'navbar_title': _("Notepad from a student"),
70
85
            'navbar_backArrow': True,
71
86
            'footer_links': footer_links,
72
87
            'footer_description': footer_description,
73
88
            }
74
89
    if not request.session.get("feed-fab-introduction-seen", default=False):
75
90
        context['introduce_feed'] = True
76
91
        request.session['feed-fab-introduction-seen'] = True
77
92
    return render(request, template, context)
78
93
79
94
def post(request, post_slug, language=None):
80
95
    if request.method == "POST":  # Handling a reply if one is sent
81
96
        form = CommentForm(request.POST)
82
97
        if form.is_valid():
83
98
            form.save()
84
-
+
99
            for post in posts:
+
100
                if post.slug(language) == post_slug:
+
101
                    new_comment.post = post
+
102
                    new_comment.save()
+
103
+
104
+
105
85
106
    if language is not None:
86
107
        if translation.check_for_language(language):
87
108
            translation.activate(language)
88
109
            request.session[translation.LANGUAGE_SESSION_KEY] = language
89
110
            #return post(request, post_slug)
90
111
    else:
91
112
        language = translation.get_language()
92
113
93
114
    template = "blog/post.html"
94
115
    posts = Post.objects.all()
95
116
    #comments = Comment.objects.filter(post
96
117
    for post in posts:
97
118
        if post.slug(language) == post_slug:
98
119
            comments = Comment.objects.filter(post=post)
99
120
            form = CommentForm(initial={'post': post})
100
-
            post_file = post.text_file(language)
+
121
            post_file = post.text_file(language)
101
122
            post_text = org_to_html(post_file.name)
102
123
            context = {
103
124
                'comments': comments,
104
125
                'form' : form,
105
126
                'human_post_title': post.title(language),
106
127
                'materialDesign_color': "brown",
107
128
                'materialDesign_accentColor': "yellow",
108
129
                'article': post_text,
109
130
                'title': post.title(language),
110
131
                'navbar_title': post.title(language),
111
132
                'navbar_backArrow': False,
112
133
                'post_slug': post_slug,
113
134
                'footer_links': footer_links,
114
135
                'footer_description': footer_description,
115
136
                }
116
137
117
138
            # Getting all available article links
118
139
            available = get_available_post_languages(post)
119
140
            if ENGLISH in available:
120
141
                context['english_link'] = reverse("blog-post-language", args=[ENGLISH, post.slug(ENGLISH)])
121
142
            if DUTCH in available:
122
143
                context['dutch_link'] = reverse("blog-post-language", args=[DUTCH, post.slug(DUTCH)])
123
144
124
145
            if FRENCH in available:
125
146
                context['french_link'] = reverse("blog-post-language", args=[FRENCH, post.slug(FRENCH)])
126
147
127
148
            if SPANISH in available:
128
149
                context['spanish_link'] = reverse("blog-post-language", args=[SPANISH, post.slug(SPANISH)])
129
150
130
151
            if GERMAN in available:
131
152
                context['german_link'] = reverse("blog-post-language", args=[GERMAN, post.slug(GERMAN)])
132
153
133
154
            return render(request, template, context)
134
155
135
156
def rss(request):
136
157
    template = "blog/feed.rss"
137
158
    context = {
138
159
        'items': FeedItem.objects.all(),
139
160
        }
140
161
    return render(request, template, context)
141
162