#2910 closed enhancement (fixed)
[patch] Add extension support to Markdown filter
Reported by: | Owned by: | Waylan Limberg | |
---|---|---|---|
Component: | Contrib apps | Version: | dev |
Severity: | normal | Keywords: | markup markdown |
Cc: | waylan@…, gajon@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Starting with Markdown 1.6 (released Oct 12, 2006) including extentions has become much easier. It would be nice to easily utilize that functionality in Django. Therefore, I propose that my suggestion, which Yuri expounded upon, be included in the markup contrib app. (Note that I am leaving out the use of escape
and linebreaks
on import
failure here - that is another issue for another ticket)
Usage:
- Load extensions by listing them in quotes separated by comas:
{{ post.body|markdown:"imagelinks,footnotes" }}
- To disable HTML, include "safe" as the first argument:
{{ post.body|markdown:"safe" }}
- For no extensions (backword compatible) just do the same as before:
{{ post.body|markdown }}
Attachments (7)
Change History (28)
by , 18 years ago
Attachment: | markup.diff added |
---|
comment:1 by , 18 years ago
Cc: | added |
---|---|
Version: | → SVN |
comment:2 by , 18 years ago
Keywords: | markup markdown added |
---|---|
Summary: | Add extension support to Markdown filter → [patch] Add extension support to Markdown filter |
comment:3 by , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
Are the older versions of MarkDown going to have problems with these extensions?
by , 18 years ago
Attachment: | markup.3.diff added |
---|
Added check for older versions of Markdown, in which case the extensions are ignored silently.
comment:4 by , 18 years ago
Just added a updated patch which checks for older versions of Markdown. Unfortunately, Markdown only identifies the version in the doc string, so is was easier to wrap the call in a try, except block. Perhaps it would be better to wait for the newer versions of Markdown to become more common and defer this for then.
Personally, I copied the contrib app to my project and made the changes there. Anyone else is able to do the same.
comment:5 by , 18 years ago
Added a patch which cobbles together useful features from various patches:
- Exception trapping for no Markdown.
- Not using extensions on older versions of Markdown.
- Running
escape(linebreaks(value))
when no Markdown is available, to safely handle the input.
follow-up: 7 comment:6 by , 18 years ago
just silently ignoring the "safe" extension seems to be a possible source of security problems...
comment:7 by , 18 years ago
Replying to alex@gc-web.de:
just silently ignoring the "safe" extension seems to be a possible source of security problems...
I understand the concern, but allowing raw html in Markdown is generally considered a feature - which some will want/expect. Therefore, it should be left to each site developer to chose for him/herself whether markdown should use safe mode in that particular app according to the needs of that projects users. The above patch does that.
Regarding the fact that safe mode is ignored in older versions, that is no different that the current behavior. If I recall correctly, it was not possible to call safe mode that easily, or at all, in older versions of Markdown. If anything, this adds one more reason to apply this patch.
comment:8 by , 17 years ago
Is there a way we could do the extension loading manually by using extension.extendMarkdown(markdown)?
Currently, python-markdown's extension loading seems to require that packages be on sys.path.
I think it would be nicer if you could do something like
{{ content|markdown:"myproject.utils.markdown_extension" }}
A SETTINGS variable that specifies the default extensions to use would be cool too.
comment:9 by , 17 years ago
Cc: | added |
---|
comment:10 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:11 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
Lets try this again, now that I'm logged in.
comment:12 by , 17 years ago
There is a problem with this patch in combination with mod_wsgi: when using the markdown filter without any arguments, arg
is ''
, so extensions
ends up being ['']
. This causes markdown to print the following on stdout:
couldn't load extension (looking for mdx_ module)
when running under mod_wsgi this results in the following exception:
IOError: sys.stdout access restricted by mod_wsgi
because mod_wsgi disables writing to stdout. see http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
It can be worked around by filtering out empty values: extensions = [e for e in arg.split(",") if e]
by , 17 years ago
Attachment: | 2910-2.diff added |
---|
fix for stdout+mod_wsgi problem, please review wrt. unicode changes
comment:13 by , 17 years ago
To be expected of course, but this will fail with "markdown() takes exactly 1 non-keyword argument" if an older version of markdown is used (for instance 1.4, which is still the latest available in ubuntu feisty, debian etch, etc). Is it worthwhile to check markdown.version (an attr which doesn't exist in the older markdown.py) here and provide a more helpful error?
by , 17 years ago
Attachment: | 2910-3.diff added |
---|
Address older versions of markdown and unicode support.
comment:14 by , 17 years ago
I just updated this patch to support the recently added auto-escaping. I left off the fallback to linebreaks(escape(text))
when markdown fails to import. IMO auto-escaping should be taking care of that - if not, then textile & restructuredtext should be patched as well. That's also why I left off the linebreaks - while its nice to have, it should be applied to all three processors, not just markdown - and therefore isn't relevant to this patch IMO.
comment:15 by , 17 years ago
Triage Stage: | Design decision needed → Ready for checkin |
---|
comment:16 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:17 by , 17 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
I have problems using german umlauts with the markdown markup filter.
Using markdown.markdown(smart_unicode(value) instead of markdown.markdown(smart_str(value) in the returns solves the problem.
s = "öäüß"
print force_unicode(markdown.markdown(smart_str(s)))
MARKDOWN-CRITICAL: "UnicodeDecodeError: Markdown only accepts unicode or ascii input."
print force_unicode(markdown.markdown(smart_unicode(s)))
<p>öäüß</p>
comment:18 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | reopened → closed |
Duplicate: http://code.djangoproject.com/ticket/6387
comment:19 by , 17 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
Triage Stage: | Ready for checkin → Design decision needed |
Oops. 6387 is a duplicate of this. Sorry.
comment:20 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Triage Stage: | Design decision needed → Ready for checkin |
Replying to anonymous:
I have problems using german umlauts with the markdown markup filter.
Using markdown.markdown(smart_unicode(value) instead of markdown.markdown(smart_str(value) in the returns solves the problem.
This is a result of stricter unicode enforcement for the newly released python-markdown 1.7 and not related to the extension support at all. In fact, this would have failed before the patch for this ticket was committed. Therefore, I am restoring this ticket to it's closed/fixed status and have opened a new ticket #6577, which has then been marked a duplicate of #6387.
diff for adding extention support to markdown