lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


+++ Yuri Takhteyev [Sep 14 09 11:12 ]:
> > I'll look into releasing on luaforge.  The project needs some polishing
> > first, e.g. better error handling and documentation.
> 
> It can always be made better, but please don't let this stop you from
> making a release.

Okay, I've tagged 0.1 and made a rock available on github: you should
now be able to install the library using

luarocks --from=http://github.com/jgm/rocks/raw/master install lunamark

> > No, currently you'd have to modify the module code. In the definition
> > of 'parser' in markdown_parser.lua, you'd add a clause for your new
> > pattern: something like
> >
> > MyPattern = p"[[" * lpeg.Ct((_"Inline" - p"]]")^0) * p"]]" / writer.mypattern
> 
> If this is the only thing that needs to be done, shouldn't it be easy
> to expose the table that is used to create the parser, to allow adding
> a pattern by simply setting a field? Perhaps something like:
> 
>     require("lunamark")
>     require("lpeg")
>     local wikilink_handler = function(s) return {"<span>", s, "</span>"} end
>     my_parser = lunamark.parsers.markdown() -- or something similar
>     my_parser.WikiLink = lpeg.P"[[" * lpeg.Ct((_"Inline" -
> lpeg.P"]]")^0) * lpeg.P"]]" / wikilink_handler
>     local converter = lunamark.converter(my_parser, "html")

Good thought.  I've done something similar.  You can now pass in a
modify_syntax function as an option.  This is a function from a table
to a table, so you can perform arbitrary transformations on the input
format grammar.  A simple example:

    function capify(t)
      t.Str = t.Str / string.upper
      return t
    end

    markdown2htmlCAPS = lunamark.converter("markdown", "html", { modify_syntax = capify })

This gives you a markdown variant in which all strings are capitalized.

Adding a handler for, say, wikilinks would be slightly more complicated.
You'd have to do two things to the grammar table:
(1) insert an entry for WikiLink
(2) register WikiLink as an Inline element by modifying Inline

So, something like this:

    function add_wikilink(t)
      t.WikiLink = --[[ whatever... ]] / write_wikilink
      t.Inline = lpeg.V("WikiLink") + t.Inline
      return t
    end

    function write_wikilink(s)
      -- code to write the wikilink as HTML
    end

I hope this is flexible enough.

John