forked from galaxyproject/galaxy-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.coffee
209 lines (194 loc) · 7.45 KB
/
build.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Build with Metalsmith
metalsmith = require('metalsmith')
minimatch = require('minimatch')
# Plugin for Bower support
bower = (files, metalsmith, done) ->
bower_files = require( 'bower-files' )()
{ readFileSync } = require 'fs'
{ basename } = require 'path'
include = (root, included) ->
for file in included
contents = readFileSync(file)
files["#{root}/#{basename(file)}"] =
contents: contents
include('css', bower_files.self().ext('css').files)
include('js', bower_files.self().ext('js').files)
include('fonts', bower_files.self().ext(['eot','otf','ttf','woff','woff2']).files)
done()
set_metadata_defaults = (files, metalsmith, done) ->
# Simple way to apply metadata defaults
for k, v of files
if k.endsWith('.md')
# Link to original path
files[k].orig_path = k
# Autotoc defaults to true
# Set domain templates
if 'events' in v.collection
files[k].layout = 'events.pug' if files[k].layout == undefined
files[k].autotoc = false if files[k].autotoc == undefined
else if 'news' in v.collection
files[k].layout = 'news.pug' if files[k].layout == undefined
files[k].autotoc = false if files[k].autotoc == undefined
else
files[k].autotoc = true if files[k].autotoc == undefined
done()
fs = require('fs')
path = require('path')
hb_partials = require("handlebars")
partials_from_dir = (source, dir) ->
for i, p_f of fs.readdirSync source
p_path = path.join(source, p_f)
stats = fs.statSync(p_path)
if stats.isDirectory()
partials_from_dir(p_path, dir)
else
contents = fs.readFileSync(p_path, 'utf8')
p_path = p_path.replace("partials/", "").replace(".html", "")
dir[p_path] = contents
hb_partials.registerPartial(p_path, contents)
partials = {}
partials_from_dir('partials', partials)
md_link_pattern = /\[([^\]]*)\]\(([^\)]*)\)/g
html_link_pattern = /href=[\'"]?([^\'" >]+)[\'"]/g
html_img_pattern = /src=[\'"]\/src?([^\'" >]+)[\'"]/g
handlebars_partial_handling = (files, metalsmith, done) ->
for file, c of files
do (file, c) ->
if file.endsWith('.md')
contents = files[file].contents.toString()
template = hb_partials.compile(contents)
contents = template({})
files[file].contents = contents
done()
subs = (files, metalsmith, done) ->
# Quick hack to temporarily handle INCLUDE migration
# Followed by another set of hacks to strip /src and index.md out of
# source. We have these full references in the source to make GitHub
# render correctly in the preview and web editor. TODO: Come up with a
# better long term solution that renders both in github, and correctly for
# publishing, that isn't a big nest of regexes and special cases.
for file, c of files
do (file, c) ->
if file.endsWith('.md')
contents = files[file].contents.toString()
matches = []
matches.push(match) while match = md_link_pattern.exec(contents)
for match in matches
rep = match[2]
#TODO: Do this with a regex too
if rep.startsWith('/src')
# Drop leading /src
rep = rep.substr(4)
if rep.startsWith('/')
# If it's a local URL, drop index.md's when they exist.
# Replace is simpler here because we have to consider
# in-page anchors.
rep = rep.replace('index.md', '')
contents = contents.split(match[0]).join("["+match[1]+"]("+rep+")")
matches = []
matches.push(match) while match = html_link_pattern.exec(contents)
for match in matches
rep = match[1]
if rep.startsWith('/src')
rep = rep.substr(4)
if rep.startsWith('/')
rep = rep.replace('index.md', '')
contents = contents.split(match[0]).join('href="'+rep+'"')
matches = []
matches.push(match) while match = html_img_pattern.exec(contents)
for match in matches
# Simply match and drop leading /src/ from images.
contents = contents.split(match[0]).join('src="'+match[1]+'"')
files[file].contents = contents
done()
# Extend `marked.Renderer` to increase all heading levels by 1 since we reserve
# h1 for the page title. Will be passed to `metalsmith-markdown` plugin.
marked = require("marked")
class Renderer extends marked.Renderer
heading: ( text, level, raw ) =>
super( text, level + 1, raw )
table: (header, body) =>
return """<table class="table table-striped">
<thead>
#{header}
</thead>
<tbody>
#{body}
</tbody>
</table>"""
image: (href, title, text) =>
out = '<img class="img-responsive" src="' + href + '" alt="' + text + '"'
if title
out += ' title="' + title + '"'
out += '/>'
return out
timer = require( "metalsmith-timer" )
ms = metalsmith(__dirname)
.use require('metalsmith-metadata')
menu: "config/menu.yaml"
.use timer 'metalsmith-metadata'
.use require('metalsmith-collections')
news:
pattern: "news/*/*.md"
sortBy: "date"
reverse: true
events:
pattern: "events/*/*.md"
sortBy: "date"
reverse: true
publications:
pattern: "publications/*/*.md"
sortBy: "date"
reverse: true
splash:
pattern: "splash/*/*.md"
sortBy: "date"
reverse: true
.use timer 'metalsmith-collections'
.use set_metadata_defaults
.use timer 'set_metadata_defaults'
.use handlebars_partial_handling
.use timer 'handlebars_partial_handling'
.use subs
.use timer 'subs'
.use require('metalsmith-markdown')
gfm: true
renderer: new Renderer()
.use timer 'metalsmith-markdown'
.use require('metalsmith-autotoc')
selector: "h2, h3, h4"
.use timer 'metalsmith-autotoc'
.use require('metalsmith-alias')()
.use timer 'metalsmith-alias'
.use require('metalsmith-filepath')
absolute: true
permalinks: true
.use timer 'metalsmith-filepath'
.use require('metalsmith-layouts')
engine: "pug"
cache: true
default: "default.pug"
pattern: "**/*.html"
helpers:
moment: require('moment')
marked: require('marked')
_: require('lodash')
.use timer 'metalsmith-layouts'
.use require('metalsmith-less')()
.use timer 'metalsmith-less'
.use bower
.use timer 'bower'
.use require('metalsmith-uglify')()
.use timer 'metalsmith-uglify'
argv = require('minimist')(process.argv.slice(2))
if argv['serve']
ms.use( require('metalsmith-serve')( { port: 8080 } ) )
if argv['check']
ms.use require('metalsmith-broken-link-checker')
allowRedirects: true
warn: true
ms.build (e) ->
if e
throw e
else
console.log("Done")