forked from jgm/lunamark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunamark.lua
94 lines (90 loc) · 3.4 KB
/
lunamark.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- (c) 2009-2011 John MacFarlane. Released under MIT license.
-- See the file LICENSE in the source for details.
--- Copyright © 2009-2011 John MacFarlane.
--
-- Released under the MIT license (see LICENSE in the source for details).
--
-- ## Description
--
-- Lunamark is a lua library for conversion of markdown to
-- other textual formats. Currently HTML, Docbook, ConTeXt,
-- LaTeX, and Groff man are the supported output formats,
-- but lunamark's modular architecture makes it easy to add
-- writers and modify the markdown parser (written with a PEG
-- grammar).
--
-- Lunamark's markdown parser currently supports the following
-- extensions (which can be turned on or off individually):
--
-- - Smart typography (fancy quotes, dashes, ellipses)
-- - Significant start numbers in ordered lists
-- - Footnotes
-- - Definition lists
--
-- More extensions will be supported in later versions.
--
-- The library is as portable as lua and has very good performance.
-- It is about as fast as the author's own C library
-- [peg-markdown](http://github.com/jgm/peg-markdown).
--
-- ## Simple usage example
--
-- local lunamark = require("lunamark")
-- local writer = lunamark.writer.html.new()
-- local parse = lunamark.reader.markdown.new(writer, { smart = true })
-- local result, metadata = parse("Here's 'my *text*'...")
-- print(result)
-- assert(result == 'Here’s ‘my <em>text</em>’…')
--
-- ## Customizing the writer
--
-- Render emphasized text using `<u>` tags rather than `<em>`.
--
-- local unicode = require("unicode")
-- function writer.emphasis(s)
-- return {"<u>",s,"</u>"}
-- end
-- local parse = lunamark.reader.markdown.new(writer, { smart = true })
-- local result, metadata = parse("*Beiß* nicht in die Hand, die dich *füttert*.")
-- print(result)
-- assert(result == '<u>Beiß</u> nicht in die Hand, die dich <u>füttert</u>.')
--
-- Eliminate hyperlinks:
--
-- function writer.link(lab,url,tit)
-- return lab
-- end
-- local parse = lunamark.reader.markdown.new(writer, { smart = true })
-- local result, metadata = parse("[hi](/url) there")
-- print(result)
-- assert(result == 'hi there')
--
-- ## Customizing the parser
--
-- Parse CamelCase words as wikilinks:
--
-- lpeg = require("lpeg")
-- local writer = lunamark.writer.html.new()
-- function add_wikilinks(syntax)
-- local capword = lpeg.R("AZ")^1 * lpeg.R("az")^1
-- local parse_wikilink = lpeg.C(capword^2)
-- / function(wikipage)
-- return writer.link(writer.string(wikipage),
-- "/" .. wikipage,
-- "Go to " .. wikipage)
-- end
-- syntax.Inline = parse_wikilink + syntax.Inline
-- return syntax
-- end
-- local parse = lunamark.reader.markdown.new(writer, { alter_syntax = add_wikilinks })
-- local result, metadata = parse("My text with WikiLinks.\n")
-- print(result)
-- assert(result == 'My text with <a href="/WikiLinks" title="Go to WikiLinks">WikiLinks</a>.')
--
local G = {}
setmetatable(G,{ __index = function(t,name)
local mod = require("lunamark." .. name)
rawset(t,name,mod)
return t[name]
end })
return G