Skip to content

Commit 639983d

Browse files
committed
Added variant of generateFromTemplate() that takes a page initializer callback function.
1 parent 4a65186 commit 639983d

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/app.lua2p

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -707,24 +707,25 @@ _G.scriptEnvironmentGlobals = {
707707
errorf(2, "Could not read file '%s': %s", path, err)
708708
end
709709

710-
local html = parseAndRunTemplate(getContext().page, path, template, "html", true)
710+
local html = parseAndRunTemplate(getContext().page, path, template, "html", true, nil)
711711
return html
712712
end,
713713

714-
generateFromTemplate = function(sitePathRel, template, params)
714+
generateFromTemplate = function(sitePathRel, template, paramsOrInit)
715715
assertContext("config", "generateFromTemplate")
716-
assertArg(1, sitePathRel, "string")
717-
assertArg(2, template, "string")
718-
assertArg(3, params, "table","nil")
716+
assertArg(1, sitePathRel, "string")
717+
assertArg(2, template, "string")
718+
assertArg(3, paramsOrInit, "table","function","nil")
719719

720720
local pathRel = sitePathToPath(sitePathRel)
721721
local page = newPage(pathRel, false)
722722

723-
if params then
724-
page.params.v = params
723+
if type(paramsOrInit) == "table" then
724+
page.params.v = paramsOrInit
725725
end
726726

727-
generateFromTemplate(page, template, nil)
727+
local onPageInit = (type(paramsOrInit) == "function") and paramsOrInit or nil
728+
generateFromTemplate(page, template, nil, onPageInit)
728729

729730
if page.isPage.v and not page._isSkipped then
730731
table.insert(pages, page)

src/functions.lua2p

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,12 @@ do
652652
return #template+1, #template, "", false -- These return values should never actually be used if everything is working correctly.
653653
end
654654

655-
local function _parseAndRunTemplate(page, path, template, useCache, enableHtmlEncoding)
656-
local luaCode = useCache and templateMetaprogramCache[path] or nil
655+
local function _parseAndRunTemplate(page, path, template, useCache, onPageInit, enableHtmlEncoding)
656+
local luaCode = (
657+
template == "" and ""
658+
or useCache and templateMetaprogramCache[path]
659+
or nil
660+
)
657661

658662
if not luaCode then
659663
local luaBuffer = {}
@@ -695,7 +699,19 @@ do
695699
ctx.out = {}
696700
ctx.enableHtmlEncoding = enableHtmlEncoding
697701

698-
local ok, errObj = xpcall(chunk, xpcallErrorHandler)
702+
local ok = true
703+
local errObj
704+
705+
if onPageInit then
706+
ok, errObj = xpcall(function()
707+
-- Note: We don't strictly need to have the page as an argument for this
708+
-- callback because globals work as usual - it's just in case there's a
709+
-- problem with name shadowing for the user.
710+
onPageInit(ctx._scriptEnvironmentGlobals.page)
711+
end, xpcallErrorHandler)
712+
end
713+
714+
if ok then ok, errObj = xpcall(chunk, xpcallErrorHandler) end
699715

700716
popContext("template")
701717

@@ -709,28 +725,29 @@ do
709725
return templateResult
710726
end
711727

712-
-- stringResult = parseAndRunTemplate( page, path, template, fileType=fromPage, useCache )
713-
function _G.parseAndRunTemplate(page, path, template, fileType, useCache)
714-
if template == "" then return "" end
728+
-- stringResult = parseAndRunTemplate( page, path, template, fileType=fromPage, useCache, onPageInit=nil )
729+
-- onPageInit = function( wrappedPage )
730+
function _G.parseAndRunTemplate(page, path, template, fileType, useCache, onPageInit)
731+
if template == "" and not onPageInit then return "" end
715732

716733
fileType = (fileType or fileTypes[page._extension])
717734

718735
!local PRINT_RESULT = DEV and 1==0
719736
local result
720737

721738
if fileType == "markdown" then
722-
result = _parseAndRunTemplate(page, path, template, useCache, true)
739+
result = _parseAndRunTemplate(page, path, template, useCache, onPageInit, true)
723740
result = markdownToHtml(result)
724741
result = trimNewlines(result).."\n" -- :Beautify
725742
!if PRINT_RESULT then __LUA` print"-- HTML --" print(result) print"-- /HTML --" ` end
726743

727744
elseif fileType == "html" then
728-
result = _parseAndRunTemplate(page, path, template, useCache, true)
745+
result = _parseAndRunTemplate(page, path, template, useCache, onPageInit, true)
729746
result = trimNewlines(result).."\n" -- :Beautify
730747
!if PRINT_RESULT then __LUA` print"-- HTML --" print(result) print"-- /HTML --" ` end
731748

732749
else
733-
result = _parseAndRunTemplate(page, path, template, useCache, false)
750+
result = _parseAndRunTemplate(page, path, template, useCache, onPageInit, false)
734751
result = trimNewlines(result).."\n" -- :Beautify
735752
!if PRINT_RESULT then __LUA` print"-- result --" print(result) print"-- /result --" ` end
736753
end
@@ -1486,8 +1503,9 @@ end
14861503

14871504

14881505

1489-
-- generateFromTemplate( page, template, modificationTime=now )
1490-
function _G.generateFromTemplate(page, template, modTime)
1506+
-- generateFromTemplate( page, template, modificationTime=now, onPageInit=nil )
1507+
-- onPageInit = function( wrappedPage )
1508+
function _G.generateFromTemplate(page, template, modTime, onPageInit)
14911509
assert(type(page) == "table")
14921510
assert(type(template) == "string")
14931511

@@ -1509,7 +1527,7 @@ function _G.generateFromTemplate(page, template, modTime)
15091527
local result
15101528

15111529
if page.isPage.v then
1512-
local pageContent = parseAndRunTemplate(page, page._pathForError, template, nil, false)
1530+
local pageContent = parseAndRunTemplate(page, page._pathForError, template, nil, false, onPageInit)
15131531

15141532
page._isLocked = true
15151533
page._readonly = true
@@ -1533,7 +1551,7 @@ function _G.generateFromTemplate(page, template, modTime)
15331551
page.content.v = pageContent
15341552

15351553
local layoutTemplate, layoutPath = getLayoutTemplate(page)
1536-
result = parseAndRunTemplate(page, layoutPath, layoutTemplate, "html", true)
1554+
result = parseAndRunTemplate(page, layoutPath, layoutTemplate, "html", true, nil)
15371555

15381556
page.content.v = "" -- We don't need this anymore.
15391557
end
@@ -1542,7 +1560,7 @@ function _G.generateFromTemplate(page, template, modTime)
15421560
page._isLocked = true
15431561
page._readonly = true
15441562

1545-
result = parseAndRunTemplate(page, page._pathForError, template, nil, false)
1563+
result = parseAndRunTemplate(page, page._pathForError, template, nil, false, onPageInit)
15461564
end
15471565

15481566
writeOutputFile(page._category, page._pathOut, page.url.v, result, modTime, page._path)
@@ -1564,7 +1582,7 @@ function _G.generateFromTemplateFile(page)
15641582
page.date.v = getDatetime(modTime) -- Default value.
15651583
end
15661584

1567-
generateFromTemplate(page, template, modTime)
1585+
generateFromTemplate(page, template, modTime, nil)
15681586
end
15691587

15701588
function _G.generateRedirection(url, targetUrl, sourcePath)
@@ -1618,7 +1636,7 @@ function _G.generateRedirection(url, targetUrl, sourcePath)
16181636
page.layout.v = redirectionLayout
16191637
page.redirectionTarget.v = targetUrl
16201638

1621-
generateFromTemplate(page, "", nil) -- The content for this super special page is always empty - we only want to process the layout!
1639+
generateFromTemplate(page, "", nil, nil) -- The content for this super special page is always empty - we only want to process the layout!
16221640

16231641
else
16241642
local contents = formatTemplate(

0 commit comments

Comments
 (0)