Syntax for extensions

Waylan Limberg waylan at gmail.com
Thu Feb 14 14:29:26 EST 2008


Ah, so you are using python-markdown

On Wed, Feb 13, 2008 at 2:25 PM, Florian Lindner <mailinglists at xgm.de> wrote:

> Hello,

> I need to to slightly extend the markdown syntax. (place an image (img tag)

> in text which URL has not yet been determined). Therefore I want to define

> something like $[Alt text](img.jpg) which would be replaced by my

> pre-processor with ![Alt text](/path/to/img.jpg) and then sent to markdown.

>


Upon determining your syntax, write a regular expression that will
match it, but not some other markdown syntax. So using your proposed
$[Alt text](img.jpg) , just use the regex that matches an image and
replace the `!` for a `$`. The regex for imagelinks can be found on
line 679 of markdown.py version 1.7:

IMAGE_LINK_RE = r'\!' + BRK + r'\s*\(([^\)]*)\)' #
![alttxt](http://x.com/)

Now, I realize a string concocation is happening there where `BRK` is
a regex string to allow multiple levels of brackets. Probably more
than you need. This should do the trick for you:

REPLACE_IMG_RE = r'\$\[([^\]]*)\]\s*\(([^\)]*)\)' # $[Alt text](img.jpg)

Note that that regex will not match a regular link or an img link.
Also note that

match.group(1) == 'Alt text'
match.group(2) == 'img.jpg`

I would suggest using that in a InlinePattern rather than a
preprocessor. Just be sure to insert it in the beginning of the
patterns so it runs first.

The page I pointed you to earlier should get you started. You may want
to look at the InlinePatterns in markdown.py for more ideas. You can
also find a number of extensions listed here [1]. I'd start with the
wikilink extension as a base to build upon.

[1]: http://www.freewisdom.org/projects/python-markdown/Available_Extensions

--
----
Waylan Limberg
waylan at gmail.com


More information about the Markdown-Discuss mailing list