Opened 17 years ago

Closed 16 years ago

Last modified 15 years ago

#2910 closed enhancement (fixed)

[patch] Add extension support to Markdown filter

Reported by: waylan@… 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)

markup.diff (421 bytes ) - added by waylan@… 17 years ago.
diff for adding extention support to markdown
markup.2.diff (910 bytes ) - added by waylan@… 17 years ago.
markup.diff in proper format
markup.3.diff (1.2 KB ) - added by Waylan Limberg <waylan@…> 17 years ago.
Added check for older versions of Markdown, in which case the extensions are ignored silently.
2910.diff (1.9 KB ) - added by James Bennett 17 years ago.
Alternate version of other patches
2910-2.diff (1.9 KB ) - added by alex@… 16 years ago.
fix for stdout+mod_wsgi problem, please review wrt. unicode changes
2910-3.diff (2.2 KB ) - added by Waylan Limberg 16 years ago.
Address older versions of markdown and unicode support.
2910-4.diff (2.1 KB ) - added by Waylan Limberg 16 years ago.
Patch updated to support the new auto-escaping

Download all attachments as: .zip

Change History (28)

by waylan@…, 17 years ago

Attachment: markup.diff added

diff for adding extention support to markdown

by waylan@…, 17 years ago

Attachment: markup.2.diff added

markup.diff in proper format

comment:1 by waylan@…, 17 years ago

Cc: waylan@… added
Version: SVN

comment:2 by Waylan Limberg <waylan@…>, 17 years ago

Keywords: markup markdown added
Summary: Add extension support to Markdown filter[patch] Add extension support to Markdown filter

comment:3 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedDesign decision needed

Are the older versions of MarkDown going to have problems with these extensions?

by Waylan Limberg <waylan@…>, 17 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 Waylan Limberg <waylan@…>, 17 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.

by James Bennett, 17 years ago

Attachment: 2910.diff added

Alternate version of other patches

comment:5 by James Bennett, 17 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.

comment:6 by alex@…, 17 years ago

just silently ignoring the "safe" extension seems to be a possible source of security problems...

in reply to:  6 comment:7 by Waylan Limberg <waylan@…>, 17 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 sciyoshi@…, 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 Jorge Gajon <gajon@…>, 17 years ago

Cc: gajon@… added

comment:10 by Waylan Limberg <waylan@…>, 17 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:11 by Waylan Limberg, 17 years ago

Owner: changed from anonymous to Waylan Limberg
Status: assignednew

Lets try this again, now that I'm logged in.

comment:12 by alex@…, 16 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 alex@…, 16 years ago

Attachment: 2910-2.diff added

fix for stdout+mod_wsgi problem, please review wrt. unicode changes

comment:13 by jpd <jd@…>, 16 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 Waylan Limberg, 16 years ago

Attachment: 2910-3.diff added

Address older versions of markdown and unicode support.

by Waylan Limberg, 16 years ago

Attachment: 2910-4.diff added

Patch updated to support the new auto-escaping

comment:14 by Waylan Limberg, 16 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 Jacob, 16 years ago

Triage Stage: Design decision neededReady for checkin

comment:16 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [6834]) Fixed #2910 -- Added support for markdown extensions to the markdown markup
filter (try saying that ten times quickly!). Thanks, Waylan Limberg.

comment:17 by anonymous, 16 years ago

Resolution: fixed
Status: closedreopened

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 jokull@…, 16 years ago

Resolution: duplicate
Status: reopenedclosed

comment:19 by jokull@…, 16 years ago

Resolution: duplicate
Status: closedreopened
Triage Stage: Ready for checkinDesign decision needed

Oops. 6387 is a duplicate of this. Sorry.

comment:20 by Waylan Limberg, 16 years ago

Resolution: fixed
Status: reopenedclosed
Triage Stage: Design decision neededReady 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.

Note: See TracTickets for help on using tickets.
Back to Top