~swisschili/blackjack

ref: b9c62f550dfd3033dc068aa3f17abee2a8aa63fe blackjack/blackjack.lua -rw-r--r-- 4.8 KiB View raw
b9c62f55swissChili Use same extension for verbatim files 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- blackjack.lua
-- Copyright (c) 2020 swissChili <swisschili.sh> all rights reserved.

function htmlProcessor(site, template, parameters)
  local parent = ""
  -- #{ key = value }
  template = template:gsub("#{%s*([%w._]+)%s*=%s*([%w._]+)%s*}",
    function(k, v)
      if (k == "parent")
      then
        parent = v
      else
        parameters[k] = v
      end
      return ""
    end
  )

  -- ${ variable }
  template = template:gsub("%${%s*([%w_%.]+)%s*}", function (word)
    return parameters[word]
  end)

  if parent ~= "" then
    parameters["body"] = template
    return site:renderTemplate(parent, parameters)
  end

  return template
end

function mdProcessor(site, temp, parameters)
  local toReplace = {
    { "`", "code" },
    { "%*%*", "b" },
    { "%*", "i" },
    { "~~", "s" }
  }

  local parent = ""
  -- @ must be at start of a line, so adding \n makes it work at
  -- the start of a file.
  local template = '\n' .. temp
  -- @key = value
  template = template:gsub("\n%s*@([%w%._]+)%s*=%s*([%w%._]+)",
    function (k, v)
      if k == "parent" then
        parent = v
      else
        parameters[k] = v
        print(k .. ' = ' .. v)
      end

      return ''
    end
  )

  template = template:gsub("%${%s*([%w_%.]+)%s*}", function (word)
    return parameters[word]
  end)

  template = template:gsub("(#+)([^=.^\n]+)\n", function (depth, header)
    return "<h" .. #depth .. ">" .. header .. "</h" .. #depth .. ">"
  end)

  template = template:gsub("```\n+(.-)\n+```", function (code)
    return "<pre><code class>" .. code .. '</code></pre>'
  end)

  for i = 1, #toReplace do
    local k = toReplace[i][1]
    local v = toReplace[i][2]
    template = template:gsub(k .. "(.-)" .. k, function (body)
      return "<" .. v .. ">" .. body .. "</" .. v .. ">"
    end)
  end

  template = template:match( "^%s*(.-)%s*$"):gsub("\n\n+", "\n<br>\n")

  if parent ~= "" then
    parameters["body"] = template
    return site:renderTemplate(parent, parameters)
  end

  return template
end

CommandProcessor = {
  command = "",
  parent = nil,
}

function CommandProcessor:new(command)
  cmdp = {}
  setmetatable(cmdp, self)
  self.__index = self
  cmdp.command = command
  return cmdp
end

function cmdProcessor(command)
  return function (site, template, parameters)
    local p = io.popen(command .. ' > /tmp/blackjack-stdout.html', 'w')
    p:write(template)
    p:close()
    local output = io.open("/tmp/blackjack-stdout.html", "r")
    if output ~= nil then
      local out = output:read("all")
      output:close()
      return out
    else
      print("    Error: Could not read from temporary file.")
      os.exit(1)
    end
  end
end

Site = {
  -- Template source directory
  templates = "templates",
  -- Content source directory
  content = "content",
  -- Output directory
  output = "site",
  -- Static file directory
  static = nil,
  -- File processor objects
  processors = {
    html = {
      process = htmlProcessor,
      extension = 'html'
    },
    md = {
      process = mdProcessor,
      extension = 'html'
    }
  },
  -- Global config data
  global = {}
}

local function getExtension(file)
  return file:match(".(%w+)$")
end

function Site:renderTemplate(templateFile, body)
  local fp = self.templates .. '/' .. templateFile
  return self:render(fp, body)
end

function Site:render(fp, body)
  local f = io.open(fp, "r")
  if f ~= nil then
    local text = f:read("all")
    f:close()
    local extension = getExtension(fp)
    if self.processors[extension] ~= nil then
      return self.processors[extension].process(self, text, body)
    else
      -- print("    Error: There is no processor for ." .. extension .. " files")
      -- os.exit(1)
      return text
    end
  else
    print("    Error: Tried to open template that does not exist")
    print(fp)
    os.exit(1)
  end
end

local function replaceExtension(file, ext)
  return file:gsub(".%w+$", '.' .. ext)
end

local function getFileName(site, file)
  return file:sub(#site.content + 1)
end

function Site:build()
  local p = io.popen('find "'.. self.content ..'" -type f')
  for file in p:lines() do
    print(" Building: ".. file)
    local html = self:render(file, self.global)
    local ext = getExtension(file)
    local newext
    if self.processors[ext] then
      newext = replaceExtension(file, self.processors[ext].extension)
    else
      newext = ext
    end
    local outFile = self.output .. getFileName(
      self,
      newext
    )
    print("  Writing: " .. outFile)
    local file = io.open(outFile, "w")
    if file ~= nil then
      file:write(html)
    else
      print("    Error: Could not write file. Does "
        .. self.output .. " directory exist?")
      os.exit(1)
    end
  end

  if self.static ~= nil then
    print("  Copying: " .. self.static)
    io.popen('cp -r "' .. self.static .. '" "' .. self.output .. '/static/"')
  end
end