mirror of
https://github.com/TangentFoxy/markdown.lua.git
synced 2025-07-27 17:42:16 +00:00
Do not use setfenv in markdown.lua
Use local variables instead.
This commit is contained in:
206
markdown.lua
206
markdown.lua
@@ -116,13 +116,6 @@ THE SOFTWARE.
|
|||||||
// Niklas
|
// Niklas
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
|
||||||
-- Set up a table for holding local functions to avoid polluting the global namespace
|
|
||||||
local M = {}
|
|
||||||
local MT = {__index = _G}
|
|
||||||
setmetatable(M, MT)
|
|
||||||
setfenv(1, M)
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
-- Utility functions
|
-- Utility functions
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
@@ -130,8 +123,8 @@ setfenv(1, M)
|
|||||||
-- Locks table t from changes, writes an error if someone attempts to change the table.
|
-- Locks table t from changes, writes an error if someone attempts to change the table.
|
||||||
-- This is useful for detecting variables that have "accidently" been made global. Something
|
-- This is useful for detecting variables that have "accidently" been made global. Something
|
||||||
-- I tend to do all too much.
|
-- I tend to do all too much.
|
||||||
function lock(t)
|
local function lock(t)
|
||||||
function lock_new_index(t, k, v)
|
local function lock_new_index(t, k, v)
|
||||||
error("module has been locked -- " .. k .. " must be declared local", 2)
|
error("module has been locked -- " .. k .. " must be declared local", 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -141,20 +134,20 @@ function lock(t)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Returns the result of mapping the values in table t through the function f
|
-- Returns the result of mapping the values in table t through the function f
|
||||||
function map(t, f)
|
local function map(t, f)
|
||||||
local out = {}
|
local out = {}
|
||||||
for k,v in pairs(t) do out[k] = f(v,k) end
|
for k,v in pairs(t) do out[k] = f(v,k) end
|
||||||
return out
|
return out
|
||||||
end
|
end
|
||||||
|
|
||||||
-- The identity function, useful as a placeholder.
|
-- The identity function, useful as a placeholder.
|
||||||
function identity(text) return text end
|
local function identity(text) return text end
|
||||||
|
|
||||||
-- Functional style if statement. (NOTE: no short circuit evaluation)
|
-- Functional style if statement. (NOTE: no short circuit evaluation)
|
||||||
function iff(t, a, b) if t then return a else return b end end
|
local function iff(t, a, b) if t then return a else return b end end
|
||||||
|
|
||||||
-- Splits the text into an array of separate lines.
|
-- Splits the text into an array of separate lines.
|
||||||
function split(text, sep)
|
local function split(text, sep)
|
||||||
sep = sep or "\n"
|
sep = sep or "\n"
|
||||||
local lines = {}
|
local lines = {}
|
||||||
local pos = 1
|
local pos = 1
|
||||||
@@ -168,7 +161,7 @@ function split(text, sep)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Converts tabs to spaces
|
-- Converts tabs to spaces
|
||||||
function detab(text)
|
local function detab(text)
|
||||||
local tab_width = 4
|
local tab_width = 4
|
||||||
local function rep(match)
|
local function rep(match)
|
||||||
local spaces = -match:len()
|
local spaces = -match:len()
|
||||||
@@ -180,7 +173,7 @@ function detab(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Applies string.find for every pattern in the list and returns the first match
|
-- Applies string.find for every pattern in the list and returns the first match
|
||||||
function find_first(s, patterns, index)
|
local function find_first(s, patterns, index)
|
||||||
local res = {}
|
local res = {}
|
||||||
for _,p in ipairs(patterns) do
|
for _,p in ipairs(patterns) do
|
||||||
local match = {s:find(p, index)}
|
local match = {s:find(p, index)}
|
||||||
@@ -192,7 +185,7 @@ end
|
|||||||
-- If a replacement array is specified, the range [start, stop] in the array is replaced
|
-- If a replacement array is specified, the range [start, stop] in the array is replaced
|
||||||
-- with the replacement array and the resulting array is returned. Without a replacement
|
-- with the replacement array and the resulting array is returned. Without a replacement
|
||||||
-- array the section of the array between start and stop is returned.
|
-- array the section of the array between start and stop is returned.
|
||||||
function splice(array, start, stop, replacement)
|
local function splice(array, start, stop, replacement)
|
||||||
if replacement then
|
if replacement then
|
||||||
local n = stop - start + 1
|
local n = stop - start + 1
|
||||||
while n > 0 do
|
while n > 0 do
|
||||||
@@ -213,7 +206,7 @@ function splice(array, start, stop, replacement)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Outdents the text one step.
|
-- Outdents the text one step.
|
||||||
function outdent(text)
|
local function outdent(text)
|
||||||
text = "\n" .. text
|
text = "\n" .. text
|
||||||
text = text:gsub("\n ? ? ?", "\n")
|
text = text:gsub("\n ? ? ?", "\n")
|
||||||
text = text:sub(2)
|
text = text:sub(2)
|
||||||
@@ -221,7 +214,7 @@ function outdent(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Indents the text one step.
|
-- Indents the text one step.
|
||||||
function indent(text)
|
local function indent(text)
|
||||||
text = text:gsub("\n", "\n ")
|
text = text:gsub("\n", "\n ")
|
||||||
return text
|
return text
|
||||||
end
|
end
|
||||||
@@ -229,7 +222,7 @@ end
|
|||||||
-- Does a simple tokenization of html data. Returns the data as a list of tokens.
|
-- Does a simple tokenization of html data. Returns the data as a list of tokens.
|
||||||
-- Each token is a table with a type field (which is either "tag" or "text") and
|
-- Each token is a table with a type field (which is either "tag" or "text") and
|
||||||
-- a text field (which contains the original token data).
|
-- a text field (which contains the original token data).
|
||||||
function tokenize_html(html)
|
local function tokenize_html(html)
|
||||||
local tokens = {}
|
local tokens = {}
|
||||||
local pos = 1
|
local pos = 1
|
||||||
while true do
|
while true do
|
||||||
@@ -287,7 +280,7 @@ local HASH = {
|
|||||||
|
|
||||||
-- Inits hashing. Creates a hash_identifier that doesn't occur anywhere
|
-- Inits hashing. Creates a hash_identifier that doesn't occur anywhere
|
||||||
-- in the text.
|
-- in the text.
|
||||||
function init_hash(text)
|
local function init_hash(text)
|
||||||
HASH.inited = true
|
HASH.inited = true
|
||||||
HASH.identifier = ""
|
HASH.identifier = ""
|
||||||
HASH.counter = 0
|
HASH.counter = 0
|
||||||
@@ -305,7 +298,7 @@ function init_hash(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Returns the hashed value for s.
|
-- Returns the hashed value for s.
|
||||||
function hash(s)
|
local function hash(s)
|
||||||
assert(HASH.inited)
|
assert(HASH.inited)
|
||||||
if not HASH.table[s] then
|
if not HASH.table[s] then
|
||||||
HASH.counter = HASH.counter + 1
|
HASH.counter = HASH.counter + 1
|
||||||
@@ -342,18 +335,18 @@ local PD = {
|
|||||||
-- Nested data.
|
-- Nested data.
|
||||||
-- </div>
|
-- </div>
|
||||||
-- </div>
|
-- </div>
|
||||||
function block_pattern(tag)
|
local function block_pattern(tag)
|
||||||
return "\n<" .. tag .. ".-\n</" .. tag .. ">[ \t]*\n"
|
return "\n<" .. tag .. ".-\n</" .. tag .. ">[ \t]*\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Pattern for matching a block tag that begins and ends with a newline
|
-- Pattern for matching a block tag that begins and ends with a newline
|
||||||
function line_pattern(tag)
|
local function line_pattern(tag)
|
||||||
return "\n<" .. tag .. ".-</" .. tag .. ">[ \t]*\n"
|
return "\n<" .. tag .. ".-</" .. tag .. ">[ \t]*\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Protects the range of characters from start to stop in the text and
|
-- Protects the range of characters from start to stop in the text and
|
||||||
-- returns the protected string.
|
-- returns the protected string.
|
||||||
function protect_range(text, start, stop)
|
local function protect_range(text, start, stop)
|
||||||
local s = text:sub(start, stop)
|
local s = text:sub(start, stop)
|
||||||
local h = hash(s)
|
local h = hash(s)
|
||||||
PD.blocks[h] = s
|
PD.blocks[h] = s
|
||||||
@@ -363,7 +356,7 @@ end
|
|||||||
|
|
||||||
-- Protect every part of the text that matches any of the patterns. The first
|
-- Protect every part of the text that matches any of the patterns. The first
|
||||||
-- matching pattern is protected first, etc.
|
-- matching pattern is protected first, etc.
|
||||||
function protect_matches(text, patterns)
|
local function protect_matches(text, patterns)
|
||||||
while true do
|
while true do
|
||||||
local start, stop = find_first(text, patterns)
|
local start, stop = find_first(text, patterns)
|
||||||
if not start then break end
|
if not start then break end
|
||||||
@@ -373,7 +366,7 @@ function protect_matches(text, patterns)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Protects blocklevel tags in the specified text
|
-- Protects blocklevel tags in the specified text
|
||||||
function protect(text)
|
local function protect(text)
|
||||||
-- First protect potentially nested block tags
|
-- First protect potentially nested block tags
|
||||||
text = protect_matches(text, map(PD.tags, block_pattern))
|
text = protect_matches(text, map(PD.tags, block_pattern))
|
||||||
-- Then protect block tags at the line level.
|
-- Then protect block tags at the line level.
|
||||||
@@ -385,12 +378,12 @@ function protect(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Returns true if the string s is a hash resulting from protection
|
-- Returns true if the string s is a hash resulting from protection
|
||||||
function is_protected(s)
|
local function is_protected(s)
|
||||||
return PD.blocks[s]
|
return PD.blocks[s]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Unprotects the specified text by expanding all the nonces
|
-- Unprotects the specified text by expanding all the nonces
|
||||||
function unprotect(text)
|
local function unprotect(text)
|
||||||
for k,v in pairs(PD.blocks) do
|
for k,v in pairs(PD.blocks) do
|
||||||
v = v:gsub("%%", "%%%%")
|
v = v:gsub("%%", "%%%%")
|
||||||
text = text:gsub(k, v)
|
text = text:gsub(k, v)
|
||||||
@@ -410,14 +403,14 @@ end
|
|||||||
-- Returns true if the line is a ruler of (char) characters.
|
-- Returns true if the line is a ruler of (char) characters.
|
||||||
-- The line must contain at least three char characters and contain only spaces and
|
-- The line must contain at least three char characters and contain only spaces and
|
||||||
-- char characters.
|
-- char characters.
|
||||||
function is_ruler_of(line, char)
|
local function is_ruler_of(line, char)
|
||||||
if not line:match("^[ %" .. char .. "]*$") then return false end
|
if not line:match("^[ %" .. char .. "]*$") then return false end
|
||||||
if not line:match("%" .. char .. ".*%" .. char .. ".*%" .. char) then return false end
|
if not line:match("%" .. char .. ".*%" .. char .. ".*%" .. char) then return false end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Identifies the block level formatting present in the line
|
-- Identifies the block level formatting present in the line
|
||||||
function classify(line)
|
local function classify(line)
|
||||||
local info = {line = line, text = line}
|
local info = {line = line, text = line}
|
||||||
|
|
||||||
if line:match("^ ") then
|
if line:match("^ ") then
|
||||||
@@ -483,7 +476,7 @@ end
|
|||||||
|
|
||||||
-- Find headers constisting of a normal line followed by a ruler and converts them to
|
-- Find headers constisting of a normal line followed by a ruler and converts them to
|
||||||
-- header entries.
|
-- header entries.
|
||||||
function headers(array)
|
local function headers(array)
|
||||||
local i = 1
|
local i = 1
|
||||||
while i <= #array - 1 do
|
while i <= #array - 1 do
|
||||||
if array[i].type == "normal" and array[i+1].type == "ruler" and
|
if array[i].type == "normal" and array[i+1].type == "ruler" and
|
||||||
@@ -500,8 +493,45 @@ function headers(array)
|
|||||||
return array
|
return array
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Forward declarations
|
||||||
|
local block_transform, span_transform, encode_code
|
||||||
|
|
||||||
|
-- Convert lines to html code
|
||||||
|
local function blocks_to_html(lines, no_paragraphs)
|
||||||
|
local out = {}
|
||||||
|
local i = 1
|
||||||
|
while i <= #lines do
|
||||||
|
local line = lines[i]
|
||||||
|
if line.type == "ruler" then
|
||||||
|
table.insert(out, "<hr/>")
|
||||||
|
elseif line.type == "raw" then
|
||||||
|
table.insert(out, line.html)
|
||||||
|
elseif line.type == "normal" then
|
||||||
|
local s = line.line
|
||||||
|
|
||||||
|
while i+1 <= #lines and lines[i+1].type == "normal" do
|
||||||
|
i = i + 1
|
||||||
|
s = s .. "\n" .. lines[i].line
|
||||||
|
end
|
||||||
|
|
||||||
|
if no_paragraphs then
|
||||||
|
table.insert(out, span_transform(s))
|
||||||
|
else
|
||||||
|
table.insert(out, "<p>" .. span_transform(s) .. "</p>")
|
||||||
|
end
|
||||||
|
elseif line.type == "header" then
|
||||||
|
local s = "<h" .. line.level .. ">" .. span_transform(line.text) .. "</h" .. line.level .. ">"
|
||||||
|
table.insert(out, s)
|
||||||
|
else
|
||||||
|
table.insert(out, line.line)
|
||||||
|
end
|
||||||
|
i = i + 1
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
-- Find list blocks and convert them to protected data blocks
|
-- Find list blocks and convert them to protected data blocks
|
||||||
function lists(array, sublist)
|
local function lists(array, sublist)
|
||||||
local function process_list(arr)
|
local function process_list(arr)
|
||||||
local function any_blanks(arr)
|
local function any_blanks(arr)
|
||||||
for i = 1, #arr do
|
for i = 1, #arr do
|
||||||
@@ -624,7 +654,7 @@ function lists(array, sublist)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Find and convert blockquote markers.
|
-- Find and convert blockquote markers.
|
||||||
function blockquotes(lines)
|
local function blockquotes(lines)
|
||||||
local function find_blockquote(lines)
|
local function find_blockquote(lines)
|
||||||
local start
|
local start
|
||||||
for i,line in ipairs(lines) do
|
for i,line in ipairs(lines) do
|
||||||
@@ -674,7 +704,7 @@ function blockquotes(lines)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Find and convert codeblocks.
|
-- Find and convert codeblocks.
|
||||||
function codeblocks(lines)
|
local function codeblocks(lines)
|
||||||
local function find_codeblock(lines)
|
local function find_codeblock(lines)
|
||||||
local start
|
local start
|
||||||
for i,line in ipairs(lines) do
|
for i,line in ipairs(lines) do
|
||||||
@@ -715,40 +745,6 @@ function codeblocks(lines)
|
|||||||
return lines
|
return lines
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Convert lines to html code
|
|
||||||
function blocks_to_html(lines, no_paragraphs)
|
|
||||||
local out = {}
|
|
||||||
local i = 1
|
|
||||||
while i <= #lines do
|
|
||||||
local line = lines[i]
|
|
||||||
if line.type == "ruler" then
|
|
||||||
table.insert(out, "<hr/>")
|
|
||||||
elseif line.type == "raw" then
|
|
||||||
table.insert(out, line.html)
|
|
||||||
elseif line.type == "normal" then
|
|
||||||
local s = line.line
|
|
||||||
|
|
||||||
while i+1 <= #lines and lines[i+1].type == "normal" do
|
|
||||||
i = i + 1
|
|
||||||
s = s .. "\n" .. lines[i].line
|
|
||||||
end
|
|
||||||
|
|
||||||
if no_paragraphs then
|
|
||||||
table.insert(out, span_transform(s))
|
|
||||||
else
|
|
||||||
table.insert(out, "<p>" .. span_transform(s) .. "</p>")
|
|
||||||
end
|
|
||||||
elseif line.type == "header" then
|
|
||||||
local s = "<h" .. line.level .. ">" .. span_transform(line.text) .. "</h" .. line.level .. ">"
|
|
||||||
table.insert(out, s)
|
|
||||||
else
|
|
||||||
table.insert(out, line.line)
|
|
||||||
end
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
return out
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Perform all the block level transforms
|
-- Perform all the block level transforms
|
||||||
function block_transform(text, sublist)
|
function block_transform(text, sublist)
|
||||||
local lines = split(text)
|
local lines = split(text)
|
||||||
@@ -764,7 +760,7 @@ end
|
|||||||
|
|
||||||
-- Debug function for printing a line array to see the result
|
-- Debug function for printing a line array to see the result
|
||||||
-- of partial transforms.
|
-- of partial transforms.
|
||||||
function print_lines(lines)
|
local function print_lines(lines)
|
||||||
for i, line in ipairs(lines) do
|
for i, line in ipairs(lines) do
|
||||||
print(i, line.type, line.text or line.line)
|
print(i, line.type, line.text or line.line)
|
||||||
end
|
end
|
||||||
@@ -778,10 +774,10 @@ end
|
|||||||
|
|
||||||
-- These characters may need to be escaped because they have a special
|
-- These characters may need to be escaped because they have a special
|
||||||
-- meaning in markdown.
|
-- meaning in markdown.
|
||||||
escape_chars = "'\\`*_{}[]()>#+-.!'"
|
local escape_chars = "'\\`*_{}[]()>#+-.!'"
|
||||||
escape_table = {}
|
local escape_table = {}
|
||||||
|
|
||||||
function init_escape_table()
|
local function init_escape_table()
|
||||||
escape_table = {}
|
escape_table = {}
|
||||||
for i = 1,#escape_chars do
|
for i = 1,#escape_chars do
|
||||||
local c = escape_chars:sub(i,i)
|
local c = escape_chars:sub(i,i)
|
||||||
@@ -790,15 +786,24 @@ function init_escape_table()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Adds a new escape to the escape table.
|
-- Adds a new escape to the escape table.
|
||||||
function add_escape(text)
|
local function add_escape(text)
|
||||||
if not escape_table[text] then
|
if not escape_table[text] then
|
||||||
escape_table[text] = hash(text)
|
escape_table[text] = hash(text)
|
||||||
end
|
end
|
||||||
return escape_table[text]
|
return escape_table[text]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Encode backspace-escaped characters in the markdown source.
|
||||||
|
local function encode_backslash_escapes(t)
|
||||||
|
for i=1,escape_chars:len() do
|
||||||
|
local c = escape_chars:sub(i,i)
|
||||||
|
t = t:gsub("\\%" .. c, escape_table[c])
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
-- Escape characters that should not be disturbed by markdown.
|
-- Escape characters that should not be disturbed by markdown.
|
||||||
function escape_special_chars(text)
|
local function escape_special_chars(text)
|
||||||
local tokens = tokenize_html(text)
|
local tokens = tokenize_html(text)
|
||||||
|
|
||||||
local out = ""
|
local out = ""
|
||||||
@@ -816,17 +821,8 @@ function escape_special_chars(text)
|
|||||||
return out
|
return out
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Encode backspace-escaped characters in the markdown source.
|
|
||||||
function encode_backslash_escapes(t)
|
|
||||||
for i=1,escape_chars:len() do
|
|
||||||
local c = escape_chars:sub(i,i)
|
|
||||||
t = t:gsub("\\%" .. c, escape_table[c])
|
|
||||||
end
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Unescape characters that have been encoded.
|
-- Unescape characters that have been encoded.
|
||||||
function unescape_special_chars(t)
|
local function unescape_special_chars(t)
|
||||||
local tin = t
|
local tin = t
|
||||||
for k,v in pairs(escape_table) do
|
for k,v in pairs(escape_table) do
|
||||||
k = k:gsub("%%", "%%%%")
|
k = k:gsub("%%", "%%%%")
|
||||||
@@ -850,7 +846,7 @@ function encode_code(s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Handle backtick blocks.
|
-- Handle backtick blocks.
|
||||||
function code_spans(s)
|
local function code_spans(s)
|
||||||
s = s:gsub("\\\\", escape_table["\\"])
|
s = s:gsub("\\\\", escape_table["\\"])
|
||||||
s = s:gsub("\\`", escape_table["`"])
|
s = s:gsub("\\`", escape_table["`"])
|
||||||
|
|
||||||
@@ -880,7 +876,7 @@ function code_spans(s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Encode alt text... enodes &, and ".
|
-- Encode alt text... enodes &, and ".
|
||||||
function encode_alt(s)
|
local function encode_alt(s)
|
||||||
if not s then return s end
|
if not s then return s end
|
||||||
s = s:gsub('&', '&')
|
s = s:gsub('&', '&')
|
||||||
s = s:gsub('"', '"')
|
s = s:gsub('"', '"')
|
||||||
@@ -888,8 +884,11 @@ function encode_alt(s)
|
|||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Forward declaration for link_db as returned by strip_link_definitions.
|
||||||
|
local link_database
|
||||||
|
|
||||||
-- Handle image references
|
-- Handle image references
|
||||||
function images(text)
|
local function images(text)
|
||||||
local function reference_link(alt, id)
|
local function reference_link(alt, id)
|
||||||
alt = encode_alt(alt:match("%b[]"):sub(2,-2))
|
alt = encode_alt(alt:match("%b[]"):sub(2,-2))
|
||||||
id = id:match("%[(.*)%]"):lower()
|
id = id:match("%[(.*)%]"):lower()
|
||||||
@@ -922,7 +921,7 @@ function images(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Handle anchor references
|
-- Handle anchor references
|
||||||
function anchors(text)
|
local function anchors(text)
|
||||||
local function reference_link(text, id)
|
local function reference_link(text, id)
|
||||||
text = text:match("%b[]"):sub(2,-2)
|
text = text:match("%b[]"):sub(2,-2)
|
||||||
id = id:match("%b[]"):sub(2,-2):lower()
|
id = id:match("%b[]"):sub(2,-2):lower()
|
||||||
@@ -955,7 +954,7 @@ function anchors(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Handle auto links, i.e. <http://www.google.com/>.
|
-- Handle auto links, i.e. <http://www.google.com/>.
|
||||||
function auto_links(text)
|
local function auto_links(text)
|
||||||
local function link(s)
|
local function link(s)
|
||||||
return add_escape("<a href=\"" .. s .. "\">") .. s .. "</a>"
|
return add_escape("<a href=\"" .. s .. "\">") .. s .. "</a>"
|
||||||
end
|
end
|
||||||
@@ -1004,7 +1003,7 @@ end
|
|||||||
|
|
||||||
-- Encode free standing amps (&) and angles (<)... note that this does not
|
-- Encode free standing amps (&) and angles (<)... note that this does not
|
||||||
-- encode free >.
|
-- encode free >.
|
||||||
function amps_and_angles(s)
|
local function amps_and_angles(s)
|
||||||
-- encode amps not part of &..; expression
|
-- encode amps not part of &..; expression
|
||||||
local pos = 1
|
local pos = 1
|
||||||
while true do
|
while true do
|
||||||
@@ -1029,7 +1028,7 @@ function amps_and_angles(s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Handles emphasis markers (* and _) in the text.
|
-- Handles emphasis markers (* and _) in the text.
|
||||||
function emphasis(text)
|
local function emphasis(text)
|
||||||
for _, s in ipairs {"%*%*", "%_%_"} do
|
for _, s in ipairs {"%*%*", "%_%_"} do
|
||||||
text = text:gsub(s .. "([^%s][%*%_]?)" .. s, "<strong>%1</strong>")
|
text = text:gsub(s .. "([^%s][%*%_]?)" .. s, "<strong>%1</strong>")
|
||||||
text = text:gsub(s .. "([^%s][^<>]-[^%s][%*%_]?)" .. s, "<strong>%1</strong>")
|
text = text:gsub(s .. "([^%s][^<>]-[^%s][%*%_]?)" .. s, "<strong>%1</strong>")
|
||||||
@@ -1044,7 +1043,7 @@ function emphasis(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Handles line break markers in the text.
|
-- Handles line break markers in the text.
|
||||||
function line_breaks(text)
|
local function line_breaks(text)
|
||||||
return text:gsub(" +\n", " <br/>\n")
|
return text:gsub(" +\n", " <br/>\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1067,7 +1066,7 @@ end
|
|||||||
|
|
||||||
-- Cleanup the text by normalizing some possible variations to make further
|
-- Cleanup the text by normalizing some possible variations to make further
|
||||||
-- processing easier.
|
-- processing easier.
|
||||||
function cleanup(text)
|
local function cleanup(text)
|
||||||
-- Standardize line endings
|
-- Standardize line endings
|
||||||
text = text:gsub("\r\n", "\n") -- DOS to UNIX
|
text = text:gsub("\r\n", "\n") -- DOS to UNIX
|
||||||
text = text:gsub("\r", "\n") -- Mac to UNIX
|
text = text:gsub("\r", "\n") -- Mac to UNIX
|
||||||
@@ -1086,7 +1085,7 @@ function cleanup(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Strips link definitions from the text and stores the data in a lookup table.
|
-- Strips link definitions from the text and stores the data in a lookup table.
|
||||||
function strip_link_definitions(text)
|
local function strip_link_definitions(text)
|
||||||
local linkdb = {}
|
local linkdb = {}
|
||||||
|
|
||||||
local function link_def(id, url, title)
|
local function link_def(id, url, title)
|
||||||
@@ -1109,10 +1108,8 @@ function strip_link_definitions(text)
|
|||||||
return text, linkdb
|
return text, linkdb
|
||||||
end
|
end
|
||||||
|
|
||||||
link_database = {}
|
|
||||||
|
|
||||||
-- Main markdown processing function
|
-- Main markdown processing function
|
||||||
function markdown(text)
|
local function markdown(text)
|
||||||
init_hash(text)
|
init_hash(text)
|
||||||
init_escape_table()
|
init_escape_table()
|
||||||
|
|
||||||
@@ -1128,11 +1125,8 @@ end
|
|||||||
-- End of module
|
-- End of module
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
|
|
||||||
setfenv(1, _G)
|
-- For compatibility, set markdown function as a global
|
||||||
M.lock(M)
|
_G.markdown = markdown
|
||||||
|
|
||||||
-- Expose markdown function to the world
|
|
||||||
markdown = M.markdown
|
|
||||||
|
|
||||||
-- Class for parsing command-line options
|
-- Class for parsing command-line options
|
||||||
local OptionParser = {}
|
local OptionParser = {}
|
||||||
@@ -1183,7 +1177,7 @@ function OptionParser:run(args)
|
|||||||
info.f()
|
info.f()
|
||||||
pos = pos + 1
|
pos = pos + 1
|
||||||
else
|
else
|
||||||
param = args[pos+1]
|
local param = args[pos+1]
|
||||||
if not param then print("No parameter for flag: " .. arg) return false end
|
if not param then print("No parameter for flag: " .. arg) return false end
|
||||||
info.f(param)
|
info.f(param)
|
||||||
pos = pos+2
|
pos = pos+2
|
||||||
@@ -1197,12 +1191,12 @@ function OptionParser:run(args)
|
|||||||
info.f()
|
info.f()
|
||||||
else
|
else
|
||||||
if i == arg:len() then
|
if i == arg:len() then
|
||||||
param = args[pos+1]
|
local param = args[pos+1]
|
||||||
if not param then print("No parameter for flag: -" .. c) return false end
|
if not param then print("No parameter for flag: -" .. c) return false end
|
||||||
info.f(param)
|
info.f(param)
|
||||||
pos = pos + 1
|
pos = pos + 1
|
||||||
else
|
else
|
||||||
param = arg:sub(i+1)
|
local param = arg:sub(i+1)
|
||||||
info.f(param)
|
info.f(param)
|
||||||
end
|
end
|
||||||
break
|
break
|
||||||
|
Reference in New Issue
Block a user