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 = {}
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
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 )
15681586end
15691587
15701588function _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