forked from icdocsoc/digest-emails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.coffee
executable file
·71 lines (60 loc) · 2.1 KB
/
compile.coffee
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
# Packages
path = require("path")
jade = require("jade")
marked = require("meta-marked")
fs = require("fs")
juice = require("juice")
stylus = require("stylus")
jsdom = require("jsdom")
moment = require("moment")
# Other Variables
templatesDir = path.join(__dirname, "templates")
file = "./emails/test/index.md"
# Render CSS
getCSS = (callback) ->
fs.readFile __dirname + "/template.styl", "utf8", (err, data) ->
throw err if err
stylus.render data, (err, css) ->
throw err if err
callback css
getHTML = (file, callback) ->
fs.readFile file, "utf8", (err, rawMarkdown) ->
throw err if err
data = marked(rawMarkdown) # Markdown -> HTML
# Extract H1 contents to create agenda, add to meta
jsdom.env
html: data.html
done: (errors, window) ->
data.meta.agenda = []
for h in window.document.querySelectorAll("h1")
data.meta.agenda.push h.textContent
data.meta.show_social = (data.meta.show_social isnt false)
data.meta.show_sponsorship = (data.meta.show_sponsorship isnt false)
if data.meta.date and data.meta.date isnt ""
data.meta.date = moment(data.meta.date).format("MMMM Do YYYY")
html = jade.renderFile "template.jade",
page: data.meta
content: data.html
callback html
renderFile = (file, callback) ->
getHTML file, (html) ->
getCSS (css) ->
callback juice.inlineContent(html, css)
outputHTML = (name, html, callback) ->
fs.writeFile name, html, (err) ->
throw err if err
callback() if callback
fs.readdir "./emails/", (err, files) ->
throw err if err
console.log "Rendering Emails"
files.forEach (file) ->
return if file[0] is "." # Ignore .DS_Store
inPath = "emails/" + file
outPath = "compiled/" + file.replace("md", "html")
fs.stat inPath, (inErr, inStats) ->
throw inErr if inErr
fs.stat outPath, (outErr, outStats) ->
#if outErr or inStats.mtime.getTime() > outStats.ctime.getTime()
console.log " - " + file + " -> " + outPath
renderFile inPath, (html) ->
outputHTML outPath, html, ->