Skip to content

Commit

Permalink
static: Add static site generator
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jun 15, 2024
1 parent 49c6a61 commit 8773935
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ var fs = require("fs")
serve: {
port: 8080
},
static: {
base: "",
min: true,
out: "_site/",
},
test_t: {
coverage: false,
lcov: true,
Expand Down
87 changes: 87 additions & 0 deletions lib/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//-
//- Usage
//- lj static [input file]
//-
//- Examples
//- lj static --out=_site index.html
//-


var UI
, cli = require("..")
, path = require("path")
, dom = require("@litejs/dom")
, parser = new dom.DOMParser()

module.exports = function(opts) {

cli.rmrf(opts.out)

global.document = dom.document
global.history = global.location = global.localStorage = global.navigator = { href: "" }

try {
var loc = path.resolve("node_modules/@litejs/ui")
global.xhr = require(loc + "/load.js").xhr
UI = require(loc)
} catch(e) {
console.error("@litejs/ui not found")
throw e
}

start(opts)
}

function start(opts) {
var fileName = opts._[0] || "index.html"
, doc = parser.parseFromString(cli.readFile(fileName))
, ui = UI.LiteJS({
root: doc.body,
})

cli.rmrf(opts.out)

if (opts.base) {
doc.querySelector("base").href = opts.base
}

doc.querySelectorAll("script[type=ui]").forEach(parseEl)

// LiteJS router not started, try to find script with content
if (!UI.onhashchange) {
var el = doc.querySelector("script[src][cat],script[src][min]")
if (el.innerHTML.trim()) parseEl(el)
}

ui.show("")
createFile("index.html")

Object.keys(ui.views).forEach(function(view) {
if (view.charAt(0) === "#" || view.indexOf("{") > -1 || view === ui.home) return
ui.show(view)
var outName = view + "/index.html"
createFile(outName)
})

cli.ls("site/**.md").forEach(function(file) {
var slug = "blog/" + file.slice(5, -3)
var outName = slug + "/index.html"

ui.$d.content = cli.readFile(file)
ui.show(slug)
createFile(outName)
})

function createFile(outName) {
var content = doc.toString(opts.min)
cli.writeFile(opts.out + "/" + outName, content)
console.log("Write", outName, content.length)
}

function parseEl(el) {
ui.parse(el.parentNode.removeChild(el).innerHTML.trim() || el.src && cli.readFile(el.src))
}
}



0 comments on commit 8773935

Please sign in to comment.