Skip to content

Commit 9763e3e

Browse files
committed
Normalizing line endings in read text files.
Added printObject().
1 parent 77f6f73 commit 9763e3e

File tree

3 files changed

+63
-35
lines changed

3 files changed

+63
-35
lines changed

build/Changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Embedded code:
66
- Whitespace around code blocks can be trimmed away with '{{*' and '*}}'.
77
- Code block parsing is a lot more robust (e.g. '{{fori {8,4,11}}}' now works).
88
- Code blocks like '{{--foo}}' are no longer valid as '}}' is now part of the Lua comment.
9-
- Added '{{for < n}}' and 'fori < arr' for iterating backwards.
9+
- Added '{{for < n}}' and '{{fori < arr}}' for iterating backwards.
1010
- Removed '{{for -n}}' (which used to iterate backwards).
1111
- Fixed '{{some-url-here}}' not working with absolute URLs.
1212
API:

src/app.lua2p

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ _G.scriptEnvironmentGlobals = {
373373
prettyUrl = toPrettyUrl,
374374
printf = printf,
375375
printfOnce = printfOnce,
376+
printObject = printObject, -- @Doc
376377
printOnce = printOnce,
377378
removeItem = removeItem,
378379
round = round,
@@ -670,10 +671,10 @@ _G.scriptEnvironmentGlobals = {
670671
s = tostringForTemplates(s)
671672

672673
local ctx = getContext"template"
673-
674674
if ctx.enableHtmlEncoding then
675675
s = encodeHtmlEntities(s)
676676
end
677+
677678
table.insert(ctx.out, s)
678679
end,
679680

@@ -684,11 +685,13 @@ _G.scriptEnvironmentGlobals = {
684685

685686
echof = function(s, ...)
686687
assertContext("template", "echof")
688+
assertArg(1, s, "string")
687689
scriptEnvironmentGlobals.echo(s:format(...))
688690
end,
689691

690692
echofRaw = function(s, ...)
691693
assertContext("template", "echofRaw")
694+
assertArg(1, s, "string")
692695
scriptEnvironmentGlobals.echoRaw(s:format(...))
693696
end,
694697

@@ -699,7 +702,7 @@ _G.scriptEnvironmentGlobals = {
699702
end
700703

701704
local path = F("%s/%s.html", DIR_LAYOUTS, htmlFileBasename)
702-
local template, err = getFileContents(path)
705+
local template, err = getFileContentsText(path)
703706
if not template then
704707
errorf(2, "Could not read file '%s': %s", path, err)
705708
end
@@ -1152,7 +1155,7 @@ local function buildWebsite()
11521155
if modTime and modTime == oldModTime then
11531156
preserveExistingOutputFile("raw", pathRel, "/"..pathRel, pathRel)
11541157
else
1155-
local contents = assert(getFileContents(path))
1158+
local contents = assert(getFileContentsBinary(path))
11561159
writeOutputFile("raw", pathRel, "/"..pathRel, contents, modTime, pathRel)
11571160
end
11581161
end
@@ -1259,7 +1262,7 @@ local function buildWebsite()
12591262
local handlingSpecialRedirections = false
12601263

12611264
if handleHtaccess then
1262-
local contents = getFileContents(DIR_CONTENT.."/.htaccess")
1265+
local contents = getFileContentsText(DIR_CONTENT.."/.htaccess")
12631266
local fileExisted = (contents ~= nil)
12641267
contents = contents or ""
12651268

src/functions.lua2p

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
generatorMeta
2525
getCwd
2626
getDirectory, getFilename, getExtension, getBasename
27-
getFileContents
27+
getFileContentsBinary, getFileContentsText
2828
getKeys
2929
getLayoutTemplate
3030
getLineNumber
@@ -48,7 +48,7 @@
4848
pairsSorted
4949
parseAndRunTemplate
5050
pathToSitePath, sitePathToPath
51-
printNoLog, printfNoLog, log, print, printOnce, printf, printfOnce, timestampPrint, timestampPrintOnce, timestampPrintVerbose, timestampPrintError, timestampPrintWarning, timestampPrintWarningOnce, printobj
51+
printNoLog, printfNoLog, log, print, printOnce, printf, printfOnce, timestampPrint, timestampPrintOnce, timestampPrintVerbose, timestampPrintError, timestampPrintWarning, timestampPrintWarningOnce, printObject
5252
pushContext, popContext, assertContext, getContext
5353
removeItem
5454
rewriteOutputPath
@@ -139,12 +139,27 @@ end
139139

140140

141141

142-
function _G.getFileContents(path)
142+
function _G.getFileContentsBinary(path)
143143
local file, err = io.open(path, "rb")
144144
if not file then return nil, err end
145145

146-
local contents = file:read("*all")
146+
local contents = file:read"*a"
147147
file:close()
148+
149+
return contents
150+
end
151+
152+
function _G.getFileContentsText(path)
153+
local file, err = io.open(path, "r")
154+
if not file then return nil, err end
155+
156+
local contents = file:read"*a"
157+
file:close()
158+
159+
if contents:find("\r", 1, true) then
160+
contents = contents:gsub("\n?\r\n?", "\n")
161+
end
162+
148163
return contents
149164
end
150165

@@ -153,6 +168,9 @@ end
153168
do
154169
-- isValueExpression = insertLuaForEchoingIfExpression( luaOutputBuffer, luaCode )
155170
local function insertLuaForEchoingIfExpression(out, luaCode)
171+
-- Note: echo() and echoRaw() do the same thing in e.g. CSS files, so we
172+
-- can output the same code here no matter what file is being processed.
173+
-- It is a thing we can optimize though. @Speed
156174
local doBlock = F("do local v = (%s) if type__(v) == 'string' and v:find'^%%s*<[!%%a/].*>%%s*$' then echoRaw__(v) elseif v ~= nil then echo__(v) end end ", luaCode)
157175
if not loadstring(doBlock) then return false end
158176

@@ -351,7 +369,7 @@ do
351369

352370
if pos <= echoI2 then
353371
local plainSegment = template:sub(pos, echoI2)
354-
table.insert(out, "echoRaw__'") -- Note: Templates will probably have more double quotes than single, that's why we use single quotes here - for nicer metaprogram output.
372+
table.insert(out, "echoRaw__'") -- Note: Templates will probably have more double quotes than single, that's why we use single quotes here - for nicer metaprogram output. :TemplateOutputQuotes
355373
table.insert(out, (plainSegment:gsub("[\\'\n]", "\\%0")))
356374
table.insert(out, "'")
357375
end
@@ -622,9 +640,9 @@ do
622640

623641
if pos <= #template then
624642
local plainSegment = template:sub(pos)
625-
table.insert(out, 'echoRaw__"')
626-
table.insert(out, (plainSegment:gsub("[\\\"\n]", "\\%0")))
627-
table.insert(out, '"')
643+
table.insert(out, "echoRaw__'") -- :TemplateOutputQuotes
644+
table.insert(out, (plainSegment:gsub("[\\'\n]", "\\%0")))
645+
table.insert(out, "'")
628646
end
629647

630648
if tcsLevel > 1 then
@@ -1040,65 +1058,71 @@ function _G.timestampPrintWarningOnce(s, ...)
10401058
log(s)
10411059
end
10421060

1043-
-- printobj( ... )
1061+
-- printObject( value1, ... )
10441062
-- Note: Does not write to log.
10451063
do
10461064
local stdoutWrite = io.write
10471065

10481066
local function toStringBetter(v)
1049-
return (tostring(v):gsub("^table: ", ""))
1067+
local s = tostring(v)
1068+
return s:find"^table: "
1069+
and s:gsub("^table: ", ""):gsub("^0x", ""):gsub("^%w+", string.upper)
1070+
or s
10501071
end
10511072

10521073
local function compareKeys(a, b)
10531074
return compareNatural(toStringBetter(a), toStringBetter(b))
10541075
end
10551076

1056-
local function _printobj(v, tables)
1077+
local function _printObject(v, printedTables, firstLevel)
10571078
local vType = type(v)
10581079

1059-
if vType == "table" then
1060-
if tables[v] then
1061-
stdoutWrite(toStringBetter(v), " ")
1062-
return
1063-
end
1080+
if printedTables[v] then
1081+
stdoutWrite(toStringBetter(v))
10641082

1065-
stdoutWrite(toStringBetter(v), "{ ")
1066-
tables[v] = true
1083+
elseif vType == "table" then
1084+
printedTables[v] = true
1085+
stdoutWrite(toStringBetter(v), (firstLevel and "{\n" or "{ "))
10671086

10681087
local indices = {}
10691088
for i = 1, #v do indices[i] = true end
10701089

10711090
for _, k in ipairs(sort(getKeys(v), compareKeys)) do
10721091
if not indices[k] then
1092+
if firstLevel then stdoutWrite("\t") end
10731093
stdoutWrite(toStringBetter(k), "=")
1074-
_printobj(v[k], tables)
1094+
_printObject(v[k], printedTables, false)
1095+
if firstLevel then stdoutWrite("\n") end
10751096
end
10761097
end
10771098

10781099
for i = 1, #v do
1100+
if firstLevel then stdoutWrite("\t") end
10791101
stdoutWrite(i, "=")
1080-
_printobj(v[i], tables)
1102+
_printObject(v[i], printedTables, false)
1103+
if firstLevel then stdoutWrite("\n") end
10811104
end
10821105

1083-
stdoutWrite("} ")
1106+
stdoutWrite("}")
10841107

10851108
elseif vType == "number" then
1086-
stdoutWrite(F("%g ", v))
1109+
stdoutWrite(tostring(v))
10871110

10881111
elseif vType == "string" then
1089-
stdoutWrite('"', v:gsub("%z", "\\0"):gsub("\n", "\\n"), '" ')
1112+
stdoutWrite('"', v:gsub("%z", "\\0"):gsub("\n", "\\n"), '"')
10901113

10911114
else
1092-
stdoutWrite(toStringBetter(v), " ")
1115+
stdoutWrite(toStringBetter(v))
10931116
end
10941117

1118+
if not firstLevel then stdoutWrite(" ") end
10951119
end
10961120

1097-
function _G.printobj(...)
1121+
function _G.printObject(...)
10981122
for i = 1, select("#", ...) do
10991123
if i > 1 then stdoutWrite("\t") end
11001124

1101-
_printobj(select(i, ...), {})
1125+
_printObject(select(i, ...), {}, true)
11021126
end
11031127
stdoutWrite("\n")
11041128
end
@@ -1321,12 +1345,12 @@ function _G.newDataFolderReader(path, checkDirExistance)
13211345

13221346
elseif isFile(F("%s/%s.toml", path, k)) then
13231347
local filePath = F("%s/%s.toml", path, k)
1324-
local contents = assert(getFileContents(filePath))
1348+
local contents = assert(getFileContentsText(filePath))
13251349
dataObj = assert(tomlLib.parse(contents))
13261350

13271351
elseif isFile(F("%s/%s.xml", path, k)) then
13281352
local filePath = F("%s/%s.xml", path, k)
1329-
local contents = assert(getFileContents(filePath))
1353+
local contents = assert(getFileContentsText(filePath))
13301354
dataObj = assert(xmlLib.parse(contents, false))
13311355

13321356
elseif isDirectory(F("%s/%s", path, k)) then
@@ -1533,7 +1557,7 @@ function _G.generateFromTemplateFile(page)
15331557
if page._isGenerating and page._isLocked then return end -- Allowed recursion.
15341558

15351559
local path = DIR_CONTENT.."/"..page._path
1536-
local template = assert(getFileContents(path))
1560+
local template = assert(getFileContentsText(path))
15371561
local modTime = lfs.attributes(path, "modification")
15381562

15391563
if modTime then
@@ -1721,7 +1745,7 @@ function _G.getLayoutTemplate(page)
17211745
local template = layoutTemplates[path]
17221746
if template then return template, path end
17231747

1724-
local template, err = getFileContents(path)
1748+
local template, err = getFileContentsText(path)
17251749
if not template then
17261750
errorf("%s: Could not load layout '%s'. (%s)", page._path, page.layout.v, err)
17271751
end
@@ -2257,6 +2281,7 @@ end
22572281

22582282

22592283
-- removeItem( array, value1, ... )
2284+
-- @Incomplete: Change into this: anythingGotRemoved = removeItem( array, value1, ... )
22602285
function _G.removeItem(t, ...)
22612286
for i = 1, select("#", ...) do
22622287
local iToRemove = indexOf(t, select(i, ...))

0 commit comments

Comments
 (0)