~dmbaturin/soupault

1

tree-template-include.lua

Details
Message ID
<54014f1d-b4d8-7dfe-261a-e39a8ba72273@aoirthoir.com>
DKIM signature
pass
Download raw message
-- IDENTIFICATION DIVISION.
-- PROGRAM-ID. TREE-TEMPLATE-INCLUDE.LUA
-- AUTHOR. AOIRTHOIR.
--
-- PLAGIARIZED-FROM. DANILL BATURIN.
-- ORIGINAL-PLUGIN: INLINE-INCLUDE.LUA
--
-- LICENSE: MIT
--
-- *********************************************************************
-- * COMMENTS
-- *********************************************************************
-- * Tree Template Include searches a series of directories from the
-- * templates directory based on the directory of the source file in
-- * site directory.
-- *
-- * For instance the widget option file = "x.html" will give the
-- * following results
-- *
-- * +------------------------+----------------------------------------+
-- * | SITE FILE              | TEMPLATE USED                          |
-- * +------------------------+----------------------------------------+
-- * | site/index.html        | templates/x.html if exists             |
-- * +------------------------+----------------------------------------+
-- * | site/cat.html          | templates/x.html if exists             |
-- * +------------------------+----------------------------------------+
-- * | site/cat/bla.html      | templates/cat/x.html if exists or      |
-- * |                        | templates/x.html if not.               |
-- * +------------------------+----------------------------------------+
-- * | site/cat/dog/bla.html  | templates/cat/dog/x.html if exists or  |
-- * |                        | templates/cat/x.html if exists or      |
-- * |                        | templates/x.html if not.               |
-- * +------------------------+----------------------------------------+
-- *
-- * This tree search continues all the way up starting at the furthest
-- * endpoint of the directory and working its way back until finally
-- * it is at templates/ to validate the existence of x.html. if x.html
-- * does not exist anywhere in the tree then obviously it is not used
-- * and does not throw an error, but does throw a tantrum. not really.
-- *
-- * The base templates directory can be altered with templates="dir".
-- *
-- * To run it, you need to add something like this to soupault.conf:
-- * [plugins.tree-template-include]
-- *   file      = "plugins/tree-template-include.lua"
-- * (this can be omitted if your plugins directory is plugins/ due to
-- *  auto discovery of plugins)
-- *
-- * [widgets.insert-footer]
-- *   widget    = "tree-template-include"
-- *   file      = "footer.html"
-- *   selector  = "footer"
-- *   templates = "somedir"  (default is templates)
-- *   parse     = "false"/"true" (default is true)
-- *   action    = "any_accepted_action" (default is append_child)
-- *   apply     = "all","first" (default is all)

-- ENVIRONMENT DIVISION.
-- SYSTEM. SOUPAULT, A STATIC WEBSITE MANAGEMENT TOOL
-- REQUIRE-VERSION. 1.8
Plugin.require_version("1.8")

-- DATA DIVISION.
-- LINKAGE SECTION.

file      = config["file"]
selector  = config["selector"]
templates = config["templates"]
parse     = config["parse"]
action    = config["action"]
apply     = config["apply"]

-- WORKING STORAGE SECTION.

paths = {}
currentpath = ""

-- PROCEDURE DIVISION USING CONFIG.
-- DATA-VALIDATION SECTION.
-- REQUIRED-ITEMS.

if not file then
   Plugin.exit("No file chosen")
elseif not selector then
   Plugin.exit("No selector chosen")
end

-- DEFAULT-ITEMS.

if not templates then
   templates = "templates"
end
if not parse then
   parse = true
elseif parse == "false" then
   parse = false
end
if not action then
   action = "append_child"
end
if not apply then
   apply = "all"
end

-- PROCESSING SECTION.
-- GET-THE-PATHS.

directories = Regex.split(target_dir, "/")
directories[1] = templates
count = size(directories)

index = 1
while (index <= count) do
   currentpath = currentpath..directories[index].."/"
   paths[index] = currentpath..file
   index = index + 1
end

-- INCLUDING-THE-FILE.

index = count
while (index > 0) do
   file_path = paths[index]
   data = Sys.read_file(file_path)
   if not data then
     Log.info(format("Could not get any data from file %s, nothing to 
include", file_path))
   else
     index = 0
     if parse then
       html = HTML.create_text(data)
     else
       html = HTML.parse(data)
     end

     elements = HTML.select(page, selector)
     counts   = size(elements)
     indexes  = 1

     while (indexes <= counts) do
       element = elements[indexes]
       if     action == "append_child"    then
         HTML.append_child(element, html)
       elseif action == "prepend_child"   then
         HTML.prepend_child(element, html)
       elseif action == "insert_before"   then
         HTML.insert_before(element, html)
       elseif action == "insert_after"    then
         HTML.insert_after(element, html)
       elseif action == "replace_element" then
         HTML.replace_element(element, html)
       elseif action == "replace_content" then
         HTML.replace_content(element, html)
       end
       if apply == "all" then
         indexes = indexes + 1
       else
         indexes = counts + 1
       end
     end
   end

   index = index - 1
end

-- STOP RUN.
Details
Message ID
<e7f6c577-f318-cfa9-6ceb-92531c9bfd57@aoirthoir.com>
In-Reply-To
<54014f1d-b4d8-7dfe-261a-e39a8ba72273@aoirthoir.com> (view parent)
DKIM signature
pass
Download raw message
Had the parse in reverse order. this fixes it.

-- IDENTIFICATION DIVISION.
-- PROGRAM-ID. TREE-TEMPLATE-INCLUDE.LUA
-- AUTHOR. AOIRTHOIR.
--
-- PLAGIARIZED-FROM. DANILL BATURIN.
-- ORIGINAL-PLUGIN: INLINE-INCLUDE.LUA
--
-- LICENSE: MIT
--
-- *********************************************************************
-- * COMMENTS
-- *********************************************************************
-- * Tree Template Include searches a series of directories from the
-- * templates directory based on the directory of the source file in
-- * site directory.
-- *
-- * For instance the widget option file = "x.html" will give the
-- * following results
-- *
-- * +------------------------+----------------------------------------+
-- * | SITE FILE              | TEMPLATE USED                          |
-- * +------------------------+----------------------------------------+
-- * | site/index.html        | templates/x.html if exists             |
-- * +------------------------+----------------------------------------+
-- * | site/cat.html          | templates/x.html if exists             |
-- * +------------------------+----------------------------------------+
-- * | site/cat/bla.html      | templates/cat/x.html if exists or      |
-- * |                        | templates/x.html if not.               |
-- * +------------------------+----------------------------------------+
-- * | site/cat/dog/bla.html  | templates/cat/dog/x.html if exists or  |
-- * |                        | templates/cat/x.html if exists or      |
-- * |                        | templates/x.html if not.               |
-- * +------------------------+----------------------------------------+
-- *
-- * This tree search continues all the way up starting at the furthest
-- * endpoint of the directory and working its way back until finally
-- * it is at templates/ to validate the existence of x.html. if x.html
-- * does not exist anywhere in the tree then obviously it is not used
-- * and does not throw an error, but does throw a tantrum. not really.
-- *
-- * The base templates directory can be altered with templates="dir".
-- *
-- * To run it, you need to add something like this to soupault.conf:
-- * [plugins.tree-template-include]
-- *   file      = "plugins/tree-template-include.lua"
-- * (this can be omitted if your plugins directory is plugins/ due to
-- *  auto discovery of plugins)
-- *
-- * [widgets.insert-footer]
-- *   widget    = "tree-template-include"
-- *   file      = "footer.html"
-- *   selector  = "footer"
-- *   templates = "somedir"  (default is templates)
-- *   parse     = "false"/"true" (default is true)
-- *   action    = "any_accepted_action" (default is append_child)
-- *   apply     = "all","first" (default is all)

-- ENVIRONMENT DIVISION.
-- SYSTEM. SOUPAULT, A STATIC WEBSITE MANAGEMENT TOOL
-- REQUIRE-VERSION. 1.8
Plugin.require_version("1.8")

-- DATA DIVISION.
-- LINKAGE SECTION.

file      = config["file"]
selector  = config["selector"]
templates = config["templates"]
parse     = config["parse"]
action    = config["action"]
apply     = config["apply"]

-- WORKING STORAGE SECTION.

paths = {}
currentpath = ""

-- PROCEDURE DIVISION USING CONFIG.
-- DATA-VALIDATION SECTION.
-- REQUIRED-ITEMS.

if not file then
   Plugin.exit("No file chosen")
elseif not selector then
   Plugin.exit("No selector chosen")
end

-- DEFAULT-ITEMS.

if not templates then
   templates = "templates"
end
if not parse then
   parse = true
elseif parse == "false" then
   parse = false
end
if not action then
   action = "append_child"
end
if not apply then
   apply = "all"
end

-- PROCESSING SECTION.
-- GET-THE-PATHS.

directories = Regex.split(target_dir, "/")
directories[1] = templates
count = size(directories)

index = 1
while (index <= count) do
   currentpath = currentpath..directories[index].."/"
   paths[index] = currentpath..file
   index = index + 1
end

-- INCLUDING-THE-FILE.

index = count
while (index > 0) do
   file_path = paths[index]
   data = Sys.read_file(file_path)
   if not data then
     Log.info(format("Could not get any data from file %s, nothing to 
include", file_path))
   else
     index = 0
     if parse then
       html = HTML.parse(data)
     else
       html = HTML.create_text(data)
     end

     elements = HTML.select(page, selector)
     counts   = size(elements)
     indexes  = 1

     while (indexes <= counts) do
       element = elements[indexes]
       if     action == "append_child"    then
         HTML.append_child(element, html)
       elseif action == "prepend_child"   then
         HTML.prepend_child(element, html)
       elseif action == "insert_before"   then
         HTML.insert_before(element, html)
       elseif action == "insert_after"    then
         HTML.insert_after(element, html)
       elseif action == "replace_element" then
         HTML.replace_element(element, html)
       elseif action == "replace_content" then
         HTML.replace_content(element, html)
       end
       if apply == "all" then
         indexes = indexes + 1
       else
         indexes = counts + 1
       end
     end
   end

   index = index - 1
end

-- STOP RUN.
Export thread (mbox)