From a2ed63ca802ee109c1b70ceec56cf874875c37af Mon Sep 17 00:00:00 2001 From: s-ol Date: Thu, 30 Jan 2020 12:13:34 +0100 Subject: fix static rendering --- .dockerignore | 5 +++++ build/render_all.moon | 49 +++++++++++++++++++++++++++++++------------ mmm/mmmfs/layout.moon | 9 ++++---- mmm/mmmfs/plugins/init.moon | 16 ++++++++------ mmm/mmmfs/plugins/static.moon | 28 +++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 mmm/mmmfs/plugins/static.moon diff --git a/.dockerignore b/.dockerignore index 5603375..f55d0d6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,9 @@ +db.sqlite3 +root/static/style/text$css.css +root/static/mmm/text$lua.lua + .tup Dockerfile $bundle.lua .bundle.lua +mmm/**/*.lua diff --git a/build/render_all.moon b/build/render_all.moon index b09957e..57406be 100644 --- a/build/render_all.moon +++ b/build/render_all.moon @@ -7,26 +7,49 @@ add '?.server' add '?/init' add '?/init.server' +-- usage: +-- moon render_all.moon [STORE] [output] [startpath] +{ store, output, startpath } = arg + require 'mmm' -import Fileder from require 'mmm.mmmfs.fileder' +import Fileder, dir_base from require 'mmm.mmmfs.fileder' import get_store from require 'mmm.mmmfs.stores' -import render from require 'mmm.mmmfs.layout' --- usage: --- moon render_all.moon [STORE] [startpath] -{ store, startpath } = arg +export UNSAFE, STATIC, BROWSER + +UNSAFE = true +STATIC = { + spit: (path, val) -> + path = "#{output}/#{path}" + os.execute "mkdir -p '#{dir_base path}'" + with io.open path, 'w' + \write val + \close! +} -export STATIC -STATIC = true +require 'mmm.mmmfs' store = get_store store -tree = Fileder store -tree = tree\walk startpath if startpath +root = Fileder store +BROWSER = :root + +print "rendering to #{output}" + +style_url = (root\walk '/static/style')\gett 'URL -> text/css' +hljs_url = (root\walk '/static/highlight-pack')\gett 'URL -> text/javascript' +STATIC.style = "" +STATIC.scripts = " + + " + +tree = root\walk startpath or '' for fileder in coroutine.wrap tree\iterate print "rendering '#{fileder.path}'..." - os.execute "mkdir -p 'out/#{fileder.path}'" - with io.open "out/#{fileder.path}/index.html", 'w' - \write render (fileder\get 'text/html'), fileder - \close! + ok, val = pcall fileder.gett, fileder, 'text/html' + if not ok + warn "WARN: couldn't render #{fileder}:" + warn val + + STATIC.spit "#{fileder.path}/index.html", val diff --git a/mmm/mmmfs/layout.moon b/mmm/mmmfs/layout.moon index b69cf75..3694ddd 100644 --- a/mmm/mmmfs/layout.moon +++ b/mmm/mmmfs/layout.moon @@ -129,9 +129,9 @@ render = (content, fileder, opts={}) -> buf = [[ - - - ]] + ]] + buf ..= STATIC.style or [[]] + buf ..= [[]] buf ..= " #{get_meta fileder} @@ -141,12 +141,13 @@ render = (content, fileder, opts={}) -> #{content} #{footer}" - buf ..= [[ + buf ..= if STATIC then '' else [[ ]] buf ..= opts.scripts + buf ..= if STATIC then STATIC.scripts else '' buf ..= " " diff --git a/mmm/mmmfs/plugins/init.moon b/mmm/mmmfs/plugins/init.moon index 46e3203..acf96e3 100644 --- a/mmm/mmmfs/plugins/init.moon +++ b/mmm/mmmfs/plugins/init.moon @@ -234,12 +234,6 @@ converts = { headers, stream = assert req\go 8 assert stream\get_body_as_string! } - { - inp: '(.+)', - out: 'URL -> %1', - cost: 5 - transform: (_, fileder, key) => "#{fileder.path}/#{key.name}:#{@from}" - } { inp: 'table', out: 'mmm/dom', @@ -293,6 +287,16 @@ add_converts 'twitter' add_converts 'youtube' add_converts 'cites' +if STATIC + add_converts 'static' +else + table.insert converts, { + inp: '(.+)', + out: 'URL -> %1', + cost: 5 + transform: (_, fileder, key) => "#{fileder.path}/#{key.name}:#{@from}" + } + if MODE == 'SERVER' ok, moon = pcall require, 'moonscript.base' if ok diff --git a/mmm/mmmfs/plugins/static.moon b/mmm/mmmfs/plugins/static.moon new file mode 100644 index 0000000..6ba27fe --- /dev/null +++ b/mmm/mmmfs/plugins/static.moon @@ -0,0 +1,28 @@ +extensions = { + 'image/jpeg': 'jpg' + 'image/png': 'png' + + 'video/webm': 'webm' + 'video/mp4': 'mp4' + + 'text/javascript': 'js' + 'text/css': 'css' +} + +{ + converts: { + { + inp: '(.+)', + out: 'URL -> %1', + cost: 5 + transform: (val, fileder, key) => + escaped_from = @from\gsub '/', '$' + if ext = extensions[@from] + escaped_from ..= ".#{ext}" + + with url = "#{fileder.path}/#{key.name}:#{escaped_from}" + print " rendering asset #{url}" + STATIC.spit url, val + } + } +} -- cgit v1.2.3