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. Is there any special syntax that should be used for such 3rd party extensions? How would you do it? Thanks, Florian
Which implementation of markdown are you using (perl, php, python, ruby...)? The answer would depend to a large extent upon that. For example, Python-Markdown has a fairly simple extension api [1] for just that sort of thing, but to my knowledge, no other implementation shares anything that closely resembles it. [1]: http://www.freewisdom.org/projects/python-markdown/Writing_Extensions On Feb 13, 2008 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. > > Is there any special syntax that should be used for such 3rd party extensions? > How would you do it? > > Thanks, > > Florian > > _______________________________________________ > Markdown-Discuss mailing list > Markdown-Discuss at six.pairlist.net > http://six.pairlist.net/mailman/listinfo/markdown-discuss >-- ---- Waylan Limberg waylan at gmail.com
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
* Florian Lindner <mailinglists at xgm.de> [2008-02-13 20:35]:> Is there any special syntax that should be used for such 3rd > party extensions? How would you do it?I?ve found that if you don?t mind a somewhat nasty-looking syntax, plain links are a nice extension point that can be used for simple functionality without adding any new syntax to Markdown. F.ex. to support abbreviations, I invented an `abbr:` link scheme, so that I could write [RDF](abbr: "Reality Distortion Field") which Markdown would then translate to <a href="abbr:" title="Reality Distortion Field">RDF</a> which a trivial bit of XSLT would turn into <abbr title="Reality Distortion Field">RDF</abbr> Pretty? Not at all? but it worked, it was completely independent of Markdown as such, and also completely independent of any one particular Markdown implementation. In your particular use, the use of the URI would not even be a hack the way it was for mine. Just write your image tags something like so: ![foo](replace:foo.jpg) and then use script to replace any `<img src>` URIs in the output that have the `replace:` scheme with whatever post-determined URI is appropriate. You don?t need to meddle with Markdown at all. Regards, -- Aristotle Pagaltzis // <http://plasmasturm.org/>