1 Star 6 Fork 4

dforel / codecombat

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
brunch-static-stuff.coffee 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
cgh 提交于 2020-01-26 13:38 . first commit!
pug = require 'pug'
path = require 'path'
cheerio = require 'cheerio'
en = require './app/locale/en'
basePath = path.join(__dirname, 'app')
_ = require 'lodash'
class BrunchStaticStuff
constructor: (config) ->
@locals = config.locals or {}
handles: /.static.pug$/
compile: (contents, filename, cb) ->
console.log "Compile", filename, basePath
outFile = filename.replace /.static.pug$/, '.html'
out = pug.compileClientWithDependenciesTracked contents,
pretty: true
filename: filename
basedir: basePath
translate = (key) ->
html = /^\[html\]/.test(key)
key = key.substring(6) if html
t = en.translation
#TODO: Replace with _.property when we get modern lodash
path = key.split(/[.]/)
while path.length > 0
k = path.shift()
t = t[k]
return key unless t?
return out =
text: t
html: html
i18n = (k,v) ->
return k.i18n.en[a] if 'i18n' in k
k[v]
try
fn = new Function(out.body + '\n return template;')()
str = fn(_.merge {_, i18n}, @locals, require './static-mock')
catch e
return cb(e.message)
c = cheerio.load(str)
elms = c('[data-i18n]')
elms.each (i, e) ->
i = c(@)
t = translate(i.data('i18n'))
if t.html
i.html(t.text)
else
i.text(t.text)
deps = ['static-mock.coffee'].concat(out.dependencies)
console.log "Wrote to #{outFile}", deps
cb(null, [{filename: outFile, content: c.html()}], deps)
module.exports = (config) ->
console.log "Loaded brunch static stuff"
new BrunchStaticStuff(config)
CoffeeScript
1
https://gitee.com/dforel/codecombat.git
git@gitee.com:dforel/codecombat.git
dforel
codecombat
codecombat
master

搜索帮助