Created
September 14, 2018 06:47
-
-
Save coffee-mug/8153847d0b5b3b12e540f22550816c1c to your computer and use it in GitHub Desktop.
Static generator first try
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const DEV_MODE = true; | |
// utils | |
debug = (...messages) => DEV_MODE ? console.log(messages) : ""; | |
const parser = { | |
feed(filename, opts) { | |
let file = fs.createReadStream(path.join(__dirname, filename.endsWith('.html') ? filename : filename + ".html")) | |
file.setEncoding('utf8') | |
let content = ''; | |
file.on('data', chunk => { | |
content += chunk; | |
}) | |
file.on('end', chunk => { | |
debug(`Parsed ${content.length} bytes`) | |
debug("--------------------") | |
debug("Starting to parse") | |
this.parse(content, opts) | |
}) | |
}, | |
parse(content) { | |
const GET_VARIABLE_NAME = /\{\{(\w+)\}\}/gi | |
const CONVERT_VARIABLE = /(\{\{\w+\}\})/gi | |
// Parse variables and turn them into objects | |
debug("Found following variables") | |
let matched; | |
while(matched = GET_VARIABLE_NAME.exec(content)){ | |
// use RegExp constructor to dynamically construct | |
// a matching rule with opts.variableName to be replaced | |
// and match globally | |
debug(matched[0]) | |
} | |
} | |
} | |
parser.feed("title", { title: "Static parser", name: "Lucas"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment