Skip to content

Commit d9025a6

Browse files
committed
Exposed Markdown and XML settings.
'toml' and 'json' are now aliases for their .parse() functions. Fixed xml.htmlScrambleEmailAddresses=false not being fully effective.
1 parent 3a62de8 commit d9025a6

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

examples/testsite/scripts/tests/runTests.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ return function()
129129
[620] = '<p><a href="mailto:[email protected]">[email protected]</a></p>\n', -- [email protected] -> <p>[email protected]</p>
130130
}
131131

132-
xml.htmlAllowNoAttributeValue = false
132+
markdown.addIdsToHeadings = false
133+
xml.htmlAllowNoAttributeValue = false
134+
xml.htmlScrambleEmailAddresses = false
133135

134136
local docHtml = assert(readTextFile"../../local/gfm-spec.html")
135137
local doc = assert(xml.parseHtml(docHtml))
@@ -190,7 +192,7 @@ return function()
190192
return false
191193
end
192194

193-
-- markdownRunInternalTests()
195+
-- markdown.runInternalTests()
194196
timerStart("markdown")
195197

196198
for _, test in ipairs(tests) do
@@ -202,7 +204,7 @@ return function()
202204
then
203205
-- print("Test#"..test.n)
204206

205-
local html = (markdown(test.input)
207+
local html = (markdown.parse(test.input)
206208
-- This is just so we pass tests we really should have passed. Sigh...
207209
:gsub("'", "&apos;")
208210
)

src/app.lua2p

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,16 @@ local function setup()
446446
utf8 = utf8,
447447
xml = xmlLib,
448448

449-
toml = { -- @Cleanup: Don't use 3rd party TOML library.
450-
parse = dataParsers.toml,
451-
},
449+
toml = setmetatable( -- @Cleanup: Don't use 3rd party TOML library.
450+
{
451+
parse = dataParsers.toml,
452+
},
453+
{
454+
__call = function(_, ...)
455+
return dataParsers.toml(...)
456+
end,
457+
}
458+
),
452459

453460
-- Site objects. (Create at site generation.)
454461
site = nil,
@@ -479,7 +486,7 @@ local function setup()
479486
ipairsr = ipairsr,
480487
isAny = isAny,
481488
isValueHtml = doesValueLookLikeHtml,
482-
markdown = markdownToHtml, !!(DEV and `markdownRunInternalTests=markdownLib.runInternalTests,` or ``)
489+
markdown = markdownLib,
483490
markdownOld = markdownOldLib, -- @Renamed @Deprecated
484491
max = math.max,
485492
min = math.min,

src/json.lua2p

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ local function parseValue(s, path, pos)
253253
end
254254

255255
-- value = json.parse( jsonString [, filePathForErrors ] )
256+
-- value = json ( jsonString [, filePathForErrors ] )
256257
-- Returns nil and a message on error.
257258
function json.parse(s, path)
258259
path = path or "<json>"
@@ -274,4 +275,8 @@ end
274275

275276

276277

277-
return json
278+
return setmetatable(json, {
279+
__call = function(_, ...)
280+
return json.parse(...)
281+
end,
282+
})

src/markdown.lua2p

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
--============================================================]]
1616

1717
local markdown = {
18-
addIdsToHeadings = true, -- @Incomplete: API. @Doc
18+
addIdsToHeadings = true,
1919
}
2020

2121
!(
@@ -1688,6 +1688,7 @@ end
16881688

16891689

16901690
-- html = markdown.parse( markdownString )
1691+
-- html = markdown ( markdownString )
16911692
function markdown.parse(s)
16921693
@@PRINT"==================="
16931694
@@PRINT(showWhitespace(s))
@@ -2637,4 +2638,8 @@ end
26372638

26382639

26392640

2640-
return markdown
2641+
return setmetatable(markdown, {
2642+
__call = function(_, ...)
2643+
return markdown.parse(...)
2644+
end,
2645+
})

src/utf8.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
--============================================================]]
2121

2222
local utf8 = {
23-
CHARACTER_PATTERN = "[%z\1-\127\194-\244][\128-\191]*", -- @Doc
23+
CHARACTER_PATTERN = "[%z\1-\127\194-\244][\128-\191]*",
2424
}
2525

2626
local stringByte = string.byte

src/xml.lua2p

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ local HTML_END_TAG_PATTERNS_FOR_TAGS_WITH_UNENCODED_CONTENTS = {
9191

9292

9393
local xml = {
94-
htmlAllowNoAttributeValue = true, -- @Doc (Maybe this should be an argument for toHtml()?)
95-
htmlScrambleEmailAddresses = true, -- @Doc (Maybe this should be an argument for toHtml()?)
94+
htmlAllowNoAttributeValue = true, -- @Cleanup: Maybe this should actually be an argument for toHtml()?
95+
htmlScrambleEmailAddresses = true, -- @Cleanup: Maybe this should actually be an argument for toHtml()?
9696
}
9797
xml.__index = xml
9898

@@ -528,7 +528,7 @@ local function parseElementContents(s, pos, pathForError, el, isHtml, isTop)
528528
elseif s:find("^&", pos) then
529529
if isTop then fileError(pathForError, s, posOffsetForError+pos, "Invalid data outside root element.") end
530530

531-
pos = _decodeEntities(s, pos, s, 0, pathForError, isHtml, el, true)
531+
pos = _decodeEntities(s, pos,s, 0,pathForError, isHtml, el, true)
532532

533533
-- Text.
534534
else
@@ -1304,6 +1304,7 @@ local function nodeToHtml(buffer, node, encodeTextAsEmail)
13041304

13051305
local tagName = el.tag -- (Don't call :lower() because that may make HTML_XML_ENTRY_TAGS[tagName] true and we end up in a weird situation. Only normalize when parsing!)
13061306
local allowNoAttrValue = xml.htmlAllowNoAttributeValue
1307+
local scrambleEmails = xml.htmlScrambleEmailAddresses
13071308

13081309
table.insert(buffer, "<")
13091310
table.insert(buffer, tagName)
@@ -1314,9 +1315,9 @@ local function nodeToHtml(buffer, node, encodeTextAsEmail)
13141315

13151316
if not (attrValue == "" and allowNoAttrValue) then
13161317
table.insert(buffer, '="')
1317-
if attrName == "href" and el.tag == "a" and attrValue:find"^[Mm][Aa][Ii][Ll][Tt][Oo]:" then
1318+
if attrName == "href" and el.tag == "a" and scrambleEmails and attrValue:find"^[Mm][Aa][Ii][Ll][Tt][Oo]:" then
13181319
encodeEmailValue(buffer, attrValue)
1319-
encodeTextAsEmail = xml.htmlScrambleEmailAddresses
1320+
encodeTextAsEmail = true
13201321
else
13211322
table.insert(buffer, xml.encodeMoreEntities(attrValue))
13221323
end
@@ -1747,11 +1748,11 @@ function xml.encodeMoreEntities(s)
17471748
return s
17481749
end
17491750

1750-
-- text = decodeEntities( htmlText [, strict=true ] ) -- @Doc
1751+
-- text = decodeEntities( htmlText [, strict=true ] )
17511752
-- Returns nil and a message on error.
17521753
function xml.decodeEntities(s, strict)
17531754
-- Note: The pcall() is useless if 'strict' is false because we will never get an error.
1754-
local ok, textOrMessage = pcall(_decodeEntities, s, 1, stringForError, positionOffsetForError, pathForError, isHtml, nil, strict~=false)
1755+
local ok, textOrMessage = pcall(_decodeEntities, s,1, s,0, "<html>", true, nil, strict~=false)
17551756
if not ok then return nil, textOrMessage end
17561757

17571758
return textOrMessage

0 commit comments

Comments
 (0)