Permalink
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (85 sloc) 2.68 KB
CoffeeScript = require 'coffee-script'
cjsxTransform = require 'coffee-react-transform'
pug = require 'pug'
sass = require 'node-sass'
VERSION = require('../package.json').version
{ getCacheKey } = require './cache'
compileScriptFile = (script_source) ->
return CoffeeScript.compile(
cjsxTransform(
script_source.toString()
)
)
compileMarkupFile = (markup_source) ->
template = pug.compile(markup_source.toString())
return template()
compileStyleFile = (style_source) ->
output = sass.renderSync
data: style_source.toString()
indentedSyntax: true
return output.css
compileScriptLibraries = (script_libraries) ->
script_libs = ''
for lib in script_libraries
key = getCacheKey(lib)
if key
url = key
cached_from = "data-cached_from='#{ lib }'"
else
url = lib
cached_from = ''
script_libs += "<script src='#{ url }' #{ cached_from }></script>"
return script_libs
compileStyleLibraries = (style_libraries) ->
style_libs = ''
for lib in style_libraries
key = getCacheKey(lib)
if key
url = key
else
url = lib
style_libs += "<link rel='stylesheet' href='#{ url }' type='text/css'>"
return style_libs
compileExtraHeadMarkup = (markup) ->
if not markup
return ''
else
return markup
compositePage = (ctx) ->
page = """
<!-- Generated by http://proto.es v#{ VERSION } -->
<!doctype html>
<html>
<head>
<title>(Proto) #{ ctx.title }</title>
<meta charset="utf-8">
#{ ctx.script_libraries }
#{ ctx.style_libraries }
#{ ctx.extra_head_markup }
<style>
#{ ctx.style }
</style>
</head>
<body>
#{ ctx.markup }
<script>
#{ ctx.script }
</script>
#{ ctx.extra_body_markup }
</body>
</html>
"""
return page
doCompilation = (sources) ->
output = compositePage
title : sources.settings.name
style : compileStyleFile(sources.style)
script : compileScriptFile(sources.script)
markup : compileMarkupFile(sources.markup)
script_libraries : compileScriptLibraries(sources.settings.script_libraries)
style_libraries : compileStyleLibraries(sources.settings.style_libraries)
extra_head_markup : compileExtraHeadMarkup(sources.settings.extra_head_markup)
extra_body_markup : sources.extra_body or ''
return output
module.exports = (sources) ->
return doCompilation(sources)