mirror of
https://github.com/TangentFoxy/markdown.lua.git
synced 2025-07-27 17:42:16 +00:00
More whitespace fixes
This commit is contained in:
146
markdown.lua
146
markdown.lua
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<http://www.frykholm.se/files/markdown.lua>
|
<http://www.frykholm.se/files/markdown.lua>
|
||||||
|
|
||||||
**Author:** Niklas Frykholm, <niklas@frykholm.se>
|
**Author:** Niklas Frykholm, <niklas@frykholm.se>
|
||||||
**Date:** 31 May 2008
|
**Date:** 31 May 2008
|
||||||
|
|
||||||
This is an implementation of the popular text markup language Markdown in pure Lua.
|
This is an implementation of the popular text markup language Markdown in pure Lua.
|
||||||
@@ -47,10 +47,10 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|||||||
software and associated documentation files (the "Software"), to deal in the Software
|
software and associated documentation files (the "Software"), to deal in the Software
|
||||||
without restriction, including without limitation the rights to use, copy, modify, merge,
|
without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
to whom the Software is furnished to do so, subject to the following conditions:
|
to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies
|
The above copyright notice and this permission notice shall be included in all copies
|
||||||
or substantial portions of the Software.
|
or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
@@ -205,7 +205,7 @@ local function indent(text)
|
|||||||
return text
|
return text
|
||||||
end
|
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).
|
||||||
local function tokenize_html(html)
|
local function tokenize_html(html)
|
||||||
@@ -218,7 +218,7 @@ local function tokenize_html(html)
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
if start ~= pos then table.insert(tokens, {type="text", text = html:sub(pos, start-1)}) end
|
if start ~= pos then table.insert(tokens, {type="text", text = html:sub(pos, start-1)}) end
|
||||||
|
|
||||||
local _, stop
|
local _, stop
|
||||||
if html:match("^<!%-%-", start) then
|
if html:match("^<!%-%-", start) then
|
||||||
_,stop = html:find("%-%->", start)
|
_,stop = html:find("%-%->", start)
|
||||||
@@ -228,7 +228,7 @@ local function tokenize_html(html)
|
|||||||
_,stop = html:find("%b<>", start)
|
_,stop = html:find("%b<>", start)
|
||||||
end
|
end
|
||||||
if not stop then
|
if not stop then
|
||||||
-- error("Could not match html tag " .. html:sub(start,start+30))
|
-- error("Could not match html tag " .. html:sub(start,start+30))
|
||||||
table.insert(tokens, {type="text", text=html:sub(start, start)})
|
table.insert(tokens, {type="text", text=html:sub(start, start)})
|
||||||
pos = start + 1
|
pos = start + 1
|
||||||
else
|
else
|
||||||
@@ -251,15 +251,15 @@ end
|
|||||||
local HASH = {
|
local HASH = {
|
||||||
-- Has the hash been inited.
|
-- Has the hash been inited.
|
||||||
inited = false,
|
inited = false,
|
||||||
|
|
||||||
-- The unique string prepended to all hash values. This is to ensure
|
-- The unique string prepended to all hash values. This is to ensure
|
||||||
-- that hash values do not accidently coincide with an actual existing
|
-- that hash values do not accidently coincide with an actual existing
|
||||||
-- string in the document.
|
-- string in the document.
|
||||||
identifier = "",
|
identifier = "",
|
||||||
|
|
||||||
-- Counter that counts up for each new hash instance.
|
-- Counter that counts up for each new hash instance.
|
||||||
counter = 0,
|
counter = 0,
|
||||||
|
|
||||||
-- Hash table.
|
-- Hash table.
|
||||||
table = {}
|
table = {}
|
||||||
}
|
}
|
||||||
@@ -271,7 +271,7 @@ local function init_hash(text)
|
|||||||
HASH.identifier = ""
|
HASH.identifier = ""
|
||||||
HASH.counter = 0
|
HASH.counter = 0
|
||||||
HASH.table = {}
|
HASH.table = {}
|
||||||
|
|
||||||
local s = "HASH"
|
local s = "HASH"
|
||||||
local counter = 0
|
local counter = 0
|
||||||
local id
|
local id
|
||||||
@@ -299,7 +299,7 @@ end
|
|||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
|
|
||||||
-- The protection module is used to "protect" parts of a document
|
-- The protection module is used to "protect" parts of a document
|
||||||
-- so that they are not modified by subsequent processing steps.
|
-- so that they are not modified by subsequent processing steps.
|
||||||
-- Protected parts are saved in a table for later unprotection
|
-- Protected parts are saved in a table for later unprotection
|
||||||
|
|
||||||
-- Protection data
|
-- Protection data
|
||||||
@@ -398,13 +398,13 @@ end
|
|||||||
-- Identifies the block level formatting present in the line
|
-- Identifies the block level formatting present in the line
|
||||||
local 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
|
||||||
info.type = "indented"
|
info.type = "indented"
|
||||||
info.outdented = line:sub(5)
|
info.outdented = line:sub(5)
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
for _,c in ipairs({'*', '-', '_', '='}) do
|
for _,c in ipairs({'*', '-', '_', '='}) do
|
||||||
if is_ruler_of(line, c) then
|
if is_ruler_of(line, c) then
|
||||||
info.type = "ruler"
|
info.type = "ruler"
|
||||||
@@ -412,12 +412,12 @@ local function classify(line)
|
|||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if line == "" then
|
if line == "" then
|
||||||
info.type = "blank"
|
info.type = "blank"
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
if line:match("^(#+)[ \t]*(.-)[ \t]*#*[ \t]*$") then
|
if line:match("^(#+)[ \t]*(.-)[ \t]*#*[ \t]*$") then
|
||||||
local m1, m2 = line:match("^(#+)[ \t]*(.-)[ \t]*#*[ \t]*$")
|
local m1, m2 = line:match("^(#+)[ \t]*(.-)[ \t]*#*[ \t]*$")
|
||||||
info.type = "header"
|
info.type = "header"
|
||||||
@@ -425,7 +425,7 @@ local function classify(line)
|
|||||||
info.text = m2
|
info.text = m2
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
if line:match("^ ? ? ?(%d+)%.[ \t]+(.+)") then
|
if line:match("^ ? ? ?(%d+)%.[ \t]+(.+)") then
|
||||||
local number, text = line:match("^ ? ? ?(%d+)%.[ \t]+(.+)")
|
local number, text = line:match("^ ? ? ?(%d+)%.[ \t]+(.+)")
|
||||||
info.type = "list_item"
|
info.type = "list_item"
|
||||||
@@ -434,7 +434,7 @@ local function classify(line)
|
|||||||
info.text = text
|
info.text = text
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
if line:match("^ ? ? ?([%*%+%-])[ \t]+(.+)") then
|
if line:match("^ ? ? ?([%*%+%-])[ \t]+(.+)") then
|
||||||
local bullet, text = line:match("^ ? ? ?([%*%+%-])[ \t]+(.+)")
|
local bullet, text = line:match("^ ? ? ?([%*%+%-])[ \t]+(.+)")
|
||||||
info.type = "list_item"
|
info.type = "list_item"
|
||||||
@@ -443,19 +443,19 @@ local function classify(line)
|
|||||||
info.text= text
|
info.text= text
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
if line:match("^>[ \t]?(.*)") then
|
if line:match("^>[ \t]?(.*)") then
|
||||||
info.type = "blockquote"
|
info.type = "blockquote"
|
||||||
info.text = line:match("^>[ \t]?(.*)")
|
info.text = line:match("^>[ \t]?(.*)")
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_protected(line) then
|
if is_protected(line) then
|
||||||
info.type = "raw"
|
info.type = "raw"
|
||||||
info.html = unprotect(line)
|
info.html = unprotect(line)
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
|
|
||||||
info.type = "normal"
|
info.type = "normal"
|
||||||
return info
|
return info
|
||||||
end
|
end
|
||||||
@@ -465,7 +465,7 @@ end
|
|||||||
local 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
|
||||||
(array[i+1].ruler_char == "-" or array[i+1].ruler_char == "=") then
|
(array[i+1].ruler_char == "-" or array[i+1].ruler_char == "=") then
|
||||||
local info = {line = array[i].line}
|
local info = {line = array[i].line}
|
||||||
info.text = info.line
|
info.text = info.line
|
||||||
@@ -494,12 +494,12 @@ local function blocks_to_html(lines, no_paragraphs)
|
|||||||
table.insert(out, line.html)
|
table.insert(out, line.html)
|
||||||
elseif line.type == "normal" then
|
elseif line.type == "normal" then
|
||||||
local s = line.line
|
local s = line.line
|
||||||
|
|
||||||
while i+1 <= #lines and lines[i+1].type == "normal" do
|
while i+1 <= #lines and lines[i+1].type == "normal" do
|
||||||
i = i + 1
|
i = i + 1
|
||||||
s = s .. "\n" .. lines[i].line
|
s = s .. "\n" .. lines[i].line
|
||||||
end
|
end
|
||||||
|
|
||||||
if no_paragraphs then
|
if no_paragraphs then
|
||||||
table.insert(out, span_transform(s))
|
table.insert(out, span_transform(s))
|
||||||
else
|
else
|
||||||
@@ -525,7 +525,7 @@ local function lists(array, sublist)
|
|||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local function split_list_items(arr)
|
local function split_list_items(arr)
|
||||||
local acc = {arr[1]}
|
local acc = {arr[1]}
|
||||||
local res = {}
|
local res = {}
|
||||||
@@ -540,12 +540,12 @@ local function lists(array, sublist)
|
|||||||
table.insert(res, acc)
|
table.insert(res, acc)
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
|
|
||||||
local function process_list_item(lines, block)
|
local function process_list_item(lines, block)
|
||||||
while lines[#lines].type == "blank" do
|
while lines[#lines].type == "blank" do
|
||||||
table.remove(lines)
|
table.remove(lines)
|
||||||
end
|
end
|
||||||
|
|
||||||
local itemtext = lines[1].text
|
local itemtext = lines[1].text
|
||||||
for i=2,#lines do
|
for i=2,#lines do
|
||||||
itemtext = itemtext .. "\n" .. outdent(lines[i].line)
|
itemtext = itemtext .. "\n" .. outdent(lines[i].line)
|
||||||
@@ -564,7 +564,7 @@ local function lists(array, sublist)
|
|||||||
return " <li>" .. itemtext .. "</li>"
|
return " <li>" .. itemtext .. "</li>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local block_list = any_blanks(arr)
|
local block_list = any_blanks(arr)
|
||||||
local items = split_list_items(arr)
|
local items = split_list_items(arr)
|
||||||
local out = ""
|
local out = ""
|
||||||
@@ -577,7 +577,7 @@ local function lists(array, sublist)
|
|||||||
return "<ul>\n" .. out .. "</ul>"
|
return "<ul>\n" .. out .. "</ul>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Finds the range of lines composing the first list in the array. A list
|
-- Finds the range of lines composing the first list in the array. A list
|
||||||
-- starts with (^ list_item) or (blank list_item) and ends with
|
-- starts with (^ list_item) or (blank list_item) and ends with
|
||||||
-- (blank* $) or (blank normal).
|
-- (blank* $) or (blank normal).
|
||||||
@@ -602,7 +602,7 @@ local function lists(array, sublist)
|
|||||||
local function find_list_end(array, start)
|
local function find_list_end(array, start)
|
||||||
local pos = #array
|
local pos = #array
|
||||||
for i = start, #array-1 do
|
for i = start, #array-1 do
|
||||||
if array[i].type == "blank" and array[i+1].type ~= "list_item"
|
if array[i].type == "blank" and array[i+1].type ~= "list_item"
|
||||||
and array[i+1].type ~= "indented" and array[i+1].type ~= "blank" then
|
and array[i+1].type ~= "indented" and array[i+1].type ~= "blank" then
|
||||||
pos = i-1
|
pos = i-1
|
||||||
break
|
break
|
||||||
@@ -613,12 +613,12 @@ local function lists(array, sublist)
|
|||||||
end
|
end
|
||||||
return pos
|
return pos
|
||||||
end
|
end
|
||||||
|
|
||||||
local start = find_list_start(array, sublist)
|
local start = find_list_start(array, sublist)
|
||||||
if not start then return nil end
|
if not start then return nil end
|
||||||
return start, find_list_end(array, start)
|
return start, find_list_end(array, start)
|
||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local start, stop = find_list(array, sublist)
|
local start, stop = find_list(array, sublist)
|
||||||
if not start then break end
|
if not start then break end
|
||||||
@@ -630,12 +630,12 @@ local function lists(array, sublist)
|
|||||||
}
|
}
|
||||||
array = splice(array, start, stop, {info})
|
array = splice(array, start, stop, {info})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Convert any remaining list items to normal
|
-- Convert any remaining list items to normal
|
||||||
for _,line in ipairs(array) do
|
for _,line in ipairs(array) do
|
||||||
if line.type == "list_item" then line.type = "normal" end
|
if line.type == "list_item" then line.type = "normal" end
|
||||||
end
|
end
|
||||||
|
|
||||||
return array
|
return array
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -650,7 +650,7 @@ local function blockquotes(lines)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not start then return nil end
|
if not start then return nil end
|
||||||
|
|
||||||
local stop = #lines
|
local stop = #lines
|
||||||
for i = start+1, #lines do
|
for i = start+1, #lines do
|
||||||
if lines[i].type == "blank" or lines[i].type == "blockquote" then
|
if lines[i].type == "blank" or lines[i].type == "blockquote" then
|
||||||
@@ -663,7 +663,7 @@ local function blockquotes(lines)
|
|||||||
while lines[stop].type == "blank" do stop = stop - 1 end
|
while lines[stop].type == "blank" do stop = stop - 1 end
|
||||||
return start, stop
|
return start, stop
|
||||||
end
|
end
|
||||||
|
|
||||||
local function process_blockquote(lines)
|
local function process_blockquote(lines)
|
||||||
local raw = lines[1].text
|
local raw = lines[1].text
|
||||||
for i = 2,#lines do
|
for i = 2,#lines do
|
||||||
@@ -674,7 +674,7 @@ local function blockquotes(lines)
|
|||||||
return "<blockquote>\n " .. bt ..
|
return "<blockquote>\n " .. bt ..
|
||||||
"\n</blockquote>"
|
"\n</blockquote>"
|
||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local start, stop = find_blockquote(lines)
|
local start, stop = find_blockquote(lines)
|
||||||
if not start then break end
|
if not start then break end
|
||||||
@@ -697,7 +697,7 @@ local function codeblocks(lines)
|
|||||||
if line.type == "indented" then start = i break end
|
if line.type == "indented" then start = i break end
|
||||||
end
|
end
|
||||||
if not start then return nil end
|
if not start then return nil end
|
||||||
|
|
||||||
local stop = #lines
|
local stop = #lines
|
||||||
for i = start+1, #lines do
|
for i = start+1, #lines do
|
||||||
if lines[i].type ~= "indented" and lines[i].type ~= "blank" then
|
if lines[i].type ~= "indented" and lines[i].type ~= "blank" then
|
||||||
@@ -708,7 +708,7 @@ local function codeblocks(lines)
|
|||||||
while lines[stop].type == "blank" do stop = stop - 1 end
|
while lines[stop].type == "blank" do stop = stop - 1 end
|
||||||
return start, stop
|
return start, stop
|
||||||
end
|
end
|
||||||
|
|
||||||
local function process_codeblock(lines)
|
local function process_codeblock(lines)
|
||||||
local raw = detab(encode_code(outdent(lines[1].line)))
|
local raw = detab(encode_code(outdent(lines[1].line)))
|
||||||
for i = 2,#lines do
|
for i = 2,#lines do
|
||||||
@@ -716,7 +716,7 @@ local function codeblocks(lines)
|
|||||||
end
|
end
|
||||||
return "<pre><code>" .. raw .. "\n</code></pre>"
|
return "<pre><code>" .. raw .. "\n</code></pre>"
|
||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local start, stop = find_codeblock(lines)
|
local start, stop = find_codeblock(lines)
|
||||||
if not start then break end
|
if not start then break end
|
||||||
@@ -783,7 +783,7 @@ end
|
|||||||
-- Escape characters that should not be disturbed by markdown.
|
-- Escape characters that should not be disturbed by markdown.
|
||||||
local 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 = ""
|
||||||
for _, token in ipairs(tokens) do
|
for _, token in ipairs(tokens) do
|
||||||
local t = token.text
|
local t = token.text
|
||||||
@@ -879,7 +879,7 @@ local function images(text)
|
|||||||
if title then title = " title=\"" .. title .. "\"" else title = "" end
|
if title then title = " title=\"" .. title .. "\"" else title = "" end
|
||||||
return add_escape ('<img src="' .. url .. '" alt="' .. alt .. '"' .. title .. "/>")
|
return add_escape ('<img src="' .. url .. '" alt="' .. alt .. '"' .. title .. "/>")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function inline_link(alt, link)
|
local function inline_link(alt, link)
|
||||||
alt = encode_alt(alt:match("%b[]"):sub(2,-2))
|
alt = encode_alt(alt:match("%b[]"):sub(2,-2))
|
||||||
local url, title = link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]")
|
local url, title = link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]")
|
||||||
@@ -892,7 +892,7 @@ local function images(text)
|
|||||||
return add_escape('<img src="' .. url .. '" alt="' .. alt .. '"/>')
|
return add_escape('<img src="' .. url .. '" alt="' .. alt .. '"/>')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
text = text:gsub("!(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link)
|
text = text:gsub("!(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link)
|
||||||
text = text:gsub("!(%b[])(%b())", inline_link)
|
text = text:gsub("!(%b[])(%b())", inline_link)
|
||||||
return text
|
return text
|
||||||
@@ -912,7 +912,7 @@ local function anchors(text)
|
|||||||
if title then title = " title=\"" .. title .. "\"" else title = "" end
|
if title then title = " title=\"" .. title .. "\"" else title = "" end
|
||||||
return add_escape("<a href=\"" .. url .. "\"" .. title .. ">") .. text .. add_escape("</a>")
|
return add_escape("<a href=\"" .. url .. "\"" .. title .. ">") .. text .. add_escape("</a>")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function inline_link(text, link)
|
local function inline_link(text, link)
|
||||||
text = text:match("%b[]"):sub(2,-2)
|
text = text:match("%b[]"):sub(2,-2)
|
||||||
local url, title = link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]")
|
local url, title = link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]")
|
||||||
@@ -925,7 +925,7 @@ local function anchors(text)
|
|||||||
return add_escape("<a href=\"" .. url .. "\">") .. text .. add_escape("</a>")
|
return add_escape("<a href=\"" .. url .. "\">") .. text .. add_escape("</a>")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
text = text:gsub("(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link)
|
text = text:gsub("(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link)
|
||||||
text = text:gsub("(%b[])(%b())", inline_link)
|
text = text:gsub("(%b[])(%b())", inline_link)
|
||||||
return text
|
return text
|
||||||
@@ -946,21 +946,21 @@ local function auto_links(text)
|
|||||||
local plain = {code = function(c) return c end, count = 0, rate = 0.1}
|
local plain = {code = function(c) return c end, count = 0, rate = 0.1}
|
||||||
local codes = {hex, dec, plain}
|
local codes = {hex, dec, plain}
|
||||||
local function swap(t,k1,k2) local temp = t[k2] t[k2] = t[k1] t[k1] = temp end
|
local function swap(t,k1,k2) local temp = t[k2] t[k2] = t[k1] t[k1] = temp end
|
||||||
|
|
||||||
local out = ""
|
local out = ""
|
||||||
for i = 1,s:len() do
|
for i = 1,s:len() do
|
||||||
for _,code in ipairs(codes) do code.count = code.count + code.rate end
|
for _,code in ipairs(codes) do code.count = code.count + code.rate end
|
||||||
if codes[1].count < codes[2].count then swap(codes,1,2) end
|
if codes[1].count < codes[2].count then swap(codes,1,2) end
|
||||||
if codes[2].count < codes[3].count then swap(codes,2,3) end
|
if codes[2].count < codes[3].count then swap(codes,2,3) end
|
||||||
if codes[1].count < codes[2].count then swap(codes,1,2) end
|
if codes[1].count < codes[2].count then swap(codes,1,2) end
|
||||||
|
|
||||||
local code = codes[1]
|
local code = codes[1]
|
||||||
local c = s:sub(i,i)
|
local c = s:sub(i,i)
|
||||||
-- Force encoding of "@" to make email address more invisible.
|
-- Force encoding of "@" to make email address more invisible.
|
||||||
if c == "@" and code == plain then code = codes[2] end
|
if c == "@" and code == plain then code = codes[2] end
|
||||||
out = out .. code.code(c)
|
out = out .. code.code(c)
|
||||||
code.count = code.count - 1
|
code.count = code.count - 1
|
||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
end
|
end
|
||||||
local function mail(s)
|
local function mail(s)
|
||||||
@@ -972,7 +972,7 @@ local function auto_links(text)
|
|||||||
-- links
|
-- links
|
||||||
text = text:gsub("<(https?:[^'\">%s]+)>", link)
|
text = text:gsub("<(https?:[^'\">%s]+)>", link)
|
||||||
text = text:gsub("<(ftp:[^'\">%s]+)>", link)
|
text = text:gsub("<(ftp:[^'\">%s]+)>", link)
|
||||||
|
|
||||||
-- mail
|
-- mail
|
||||||
text = text:gsub("<mailto:([^'\">%s]+)>", mail)
|
text = text:gsub("<mailto:([^'\">%s]+)>", mail)
|
||||||
text = text:gsub("<([-.%w]+%@[-.%w]+)>", mail)
|
text = text:gsub("<([-.%w]+%@[-.%w]+)>", mail)
|
||||||
@@ -996,11 +996,11 @@ local function amps_and_angles(s)
|
|||||||
pos = amp+1
|
pos = amp+1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- encode naked <'s
|
-- encode naked <'s
|
||||||
s = s:gsub("<([^a-zA-Z/?$!])", "<%1")
|
s = s:gsub("<([^a-zA-Z/?$!])", "<%1")
|
||||||
s = s:gsub("<$", "<")
|
s = s:gsub("<$", "<")
|
||||||
|
|
||||||
-- what about >, nothing done in the original markdown source to handle them
|
-- what about >, nothing done in the original markdown source to handle them
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
@@ -1048,24 +1048,24 @@ 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
|
||||||
|
|
||||||
-- Convert all tabs to spaces
|
-- Convert all tabs to spaces
|
||||||
text = detab(text)
|
text = detab(text)
|
||||||
|
|
||||||
-- Strip lines with only spaces and tabs
|
-- Strip lines with only spaces and tabs
|
||||||
while true do
|
while true do
|
||||||
local subs
|
local subs
|
||||||
text, subs = text:gsub("\n[ \t]+\n", "\n\n")
|
text, subs = text:gsub("\n[ \t]+\n", "\n\n")
|
||||||
if subs == 0 then break end
|
if subs == 0 then break end
|
||||||
end
|
end
|
||||||
|
|
||||||
return "\n" .. text .. "\n"
|
return "\n" .. text .. "\n"
|
||||||
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.
|
||||||
local 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)
|
||||||
id = id:match("%[(.+)%]"):lower()
|
id = id:match("%[(.+)%]"):lower()
|
||||||
linkdb[id] = linkdb[id] or {}
|
linkdb[id] = linkdb[id] or {}
|
||||||
@@ -1078,7 +1078,7 @@ local function strip_link_definitions(text)
|
|||||||
local def_title1 = def_no_title .. "[ \t]+\n?[ \t]*[\"'(]([^\n]+)[\"')][ \t]*"
|
local def_title1 = def_no_title .. "[ \t]+\n?[ \t]*[\"'(]([^\n]+)[\"')][ \t]*"
|
||||||
local def_title2 = def_no_title .. "[ \t]*\n[ \t]*[\"'(]([^\n]+)[\"')][ \t]*"
|
local def_title2 = def_no_title .. "[ \t]*\n[ \t]*[\"'(]([^\n]+)[\"')][ \t]*"
|
||||||
local def_title3 = def_no_title .. "[ \t]*\n?[ \t]+[\"'(]([^\n]+)[\"')][ \t]*"
|
local def_title3 = def_no_title .. "[ \t]*\n?[ \t]+[\"'(]([^\n]+)[\"')][ \t]*"
|
||||||
|
|
||||||
text = text:gsub(def_title1, link_def)
|
text = text:gsub(def_title1, link_def)
|
||||||
text = text:gsub(def_title2, link_def)
|
text = text:gsub(def_title2, link_def)
|
||||||
text = text:gsub(def_title3, link_def)
|
text = text:gsub(def_title3, link_def)
|
||||||
@@ -1090,7 +1090,7 @@ end
|
|||||||
local function markdown(text)
|
local function markdown(text)
|
||||||
init_hash(text)
|
init_hash(text)
|
||||||
init_escape_table()
|
init_escape_table()
|
||||||
|
|
||||||
text = cleanup(text)
|
text = cleanup(text)
|
||||||
text = protect(text)
|
text = protect(text)
|
||||||
text, link_database = strip_link_definitions(text)
|
text, link_database = strip_link_definitions(text)
|
||||||
@@ -1197,7 +1197,7 @@ local function run_command_line(arg)
|
|||||||
if not options.wrap_header then return s end
|
if not options.wrap_header then return s end
|
||||||
local header
|
local header
|
||||||
if options.header then
|
if options.header then
|
||||||
local f = io.open(options.header) or error("Could not open file: " .. options.header)
|
local f = io.open(options.header) or error("Could not open file: " .. options.header)
|
||||||
header = f:read("*a")
|
header = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
else
|
else
|
||||||
@@ -1205,20 +1205,20 @@ local function run_command_line(arg)
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="content-type" content="text/html; charset=CHARSET" />
|
<meta http-equiv="content-type" content="text/html; charset=CHARSET" />
|
||||||
<title>TITLE</title>
|
<title>TITLE</title>
|
||||||
<link rel="stylesheet" type="text/css" href="STYLESHEET" />
|
<link rel="stylesheet" type="text/css" href="STYLESHEET" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
]]
|
]]
|
||||||
local title = options.title or s:match("<h1>(.-)</h1>") or s:match("<h2>(.-)</h2>") or
|
local title = options.title or s:match("<h1>(.-)</h1>") or s:match("<h2>(.-)</h2>") or
|
||||||
s:match("<h3>(.-)</h3>") or "Untitled"
|
s:match("<h3>(.-)</h3>") or "Untitled"
|
||||||
header = header:gsub("TITLE", title)
|
header = header:gsub("TITLE", title)
|
||||||
if options.inline_style then
|
if options.inline_style then
|
||||||
local style = ""
|
local style = ""
|
||||||
local f = io.open(options.stylesheet)
|
local f = io.open(options.stylesheet)
|
||||||
if f then
|
if f then
|
||||||
style = f:read("*a") f:close()
|
style = f:read("*a") f:close()
|
||||||
else
|
else
|
||||||
error("Could not include style sheet " .. options.stylesheet .. ": File not found")
|
error("Could not include style sheet " .. options.stylesheet .. ": File not found")
|
||||||
end
|
end
|
||||||
@@ -1237,15 +1237,15 @@ local function run_command_line(arg)
|
|||||||
end
|
end
|
||||||
return header .. s .. footer
|
return header .. s .. footer
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Generate output path name from input path name given options.
|
-- Generate output path name from input path name given options.
|
||||||
local function outpath(path, options)
|
local function outpath(path, options)
|
||||||
if options.append then return path .. ".html" end
|
if options.append then return path .. ".html" end
|
||||||
local m = path:match("^(.+%.html)[^/\\]+$") if m then return m end
|
local m = path:match("^(.+%.html)[^/\\]+$") if m then return m end
|
||||||
m = path:match("^(.+%.)[^/\\]*$") if m and path ~= m .. "html" then return m .. "html" end
|
m = path:match("^(.+%.)[^/\\]*$") if m and path ~= m .. "html" then return m .. "html" end
|
||||||
return path .. ".html"
|
return path .. ".html"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Default commandline options
|
-- Default commandline options
|
||||||
local options = {
|
local options = {
|
||||||
wrap_header = true,
|
wrap_header = true,
|
||||||
@@ -1288,18 +1288,18 @@ Other options:
|
|||||||
op:param("s", "style", function(x) options.stylesheet = x end)
|
op:param("s", "style", function(x) options.stylesheet = x end)
|
||||||
op:flag("l", "inline-style", function() options.inline_style = true end)
|
op:flag("l", "inline-style", function() options.inline_style = true end)
|
||||||
op:flag("a", "append", function() options.append = true end)
|
op:flag("a", "append", function() options.append = true end)
|
||||||
op:flag("t", "test", function()
|
op:flag("t", "test", function()
|
||||||
local n = arg[0]:gsub("markdown.lua", "markdown-tests.lua")
|
local n = arg[0]:gsub("markdown.lua", "markdown-tests.lua")
|
||||||
local f = io.open(n)
|
local f = io.open(n)
|
||||||
if f then
|
if f then
|
||||||
f:close() dofile(n)
|
f:close() dofile(n)
|
||||||
else
|
else
|
||||||
error("Cannot find markdown-tests.lua")
|
error("Cannot find markdown-tests.lua")
|
||||||
end
|
end
|
||||||
run_stdin = false
|
run_stdin = false
|
||||||
end)
|
end)
|
||||||
op:flag("h", "help", function() print(help) run_stdin = false end)
|
op:flag("h", "help", function() print(help) run_stdin = false end)
|
||||||
op:arg(function(path)
|
op:arg(function(path)
|
||||||
local file = io.open(path) or error("Could not open file: " .. path)
|
local file = io.open(path) or error("Could not open file: " .. path)
|
||||||
local s = file:read("*a")
|
local s = file:read("*a")
|
||||||
file:close()
|
file:close()
|
||||||
@@ -1310,7 +1310,7 @@ Other options:
|
|||||||
run_stdin = false
|
run_stdin = false
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
if not op:run(arg) then
|
if not op:run(arg) then
|
||||||
print(help)
|
print(help)
|
||||||
run_stdin = false
|
run_stdin = false
|
||||||
@@ -1322,7 +1322,7 @@ Other options:
|
|||||||
io.write(s)
|
io.write(s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If we are being run from the command-line, act accordingly
|
-- If we are being run from the command-line, act accordingly
|
||||||
if arg and arg[0]:find("markdown%.lua$") then
|
if arg and arg[0]:find("markdown%.lua$") then
|
||||||
run_command_line(arg)
|
run_command_line(arg)
|
||||||
|
Reference in New Issue
Block a user