Skip to content

Commit

Permalink
Fixes wierdness from last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Nov 16, 2015
1 parent e5ef747 commit 4146a89
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-template-helper",
"description": "Grunt template processor and wrapper.",
"version": "0.2.0",
"version": "0.2.1",
"homepage": "https://github.com/ciddan/grunt-template-helper",
"author": {
"name": "Mikael Selander",
Expand All @@ -17,13 +17,13 @@
},
"main": "Gruntfile.js",
"engines": {
"node": "~0.10.0"
"node": "^4.0.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {
"prettydiff": "1.14.7"
"prettydiff": "1.15.16"
},
"devDependencies": {
"grunt": "~0.4.2",
Expand All @@ -33,7 +33,7 @@
"chai": "~3.4.0",
"coffee-script": "~1.10.0"
},
"_id": "[email protected].0",
"_id": "[email protected].1",
"_from": "grunt-template-helper0.1.0",
"keywords": [
"gruntplugin",
Expand Down
78 changes: 78 additions & 0 deletions tasks/template.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'

module.exports = (grunt) ->
minifier = require 'prettydiff'

grunt.registerMultiTask 'template', 'Process grunt template files', () ->
options = @options()

@.files.forEach((f) ->
grunt.log.writeln "src: #{f.src}, dest: #{f.dest}"

out = f.src.filter((filepath) ->
grunt.file.exists filepath
).map((filepath) ->
compileTemplate filepath, options, f.dest
).join grunt.util.normalizelf grunt.util.linefeed

if out.length < 1
grunt.log.warn 'Nothing written because compiled files were empty.'
else
out = minify out, options if options.minify?
grunt.file.write f.dest, out
grunt.log.writeln "File #{f.dest.cyan} created."
)

minify = (src, options) ->
options.minify.pretty ?=
mode: 'minify'
lang: 'markup'
html: 'html-yes'

options.minify.pretty.source = src

try
minified = minifier.api(options.minify.pretty)[0]
catch e
grunt.log.error e
grunt.fail.warn 'Oh noes! Template minification failed :('

minified


compileTemplate = (filepath, options, dest) ->
src = grunt.file.read filepath

try
wrap = options.wrap?.banner? and options.wrap?.footer?
inject = wrap and options.wrap.inject?.length >= 1

processed = grunt.template.process src, options

if wrap
processed = "#{options.wrap.banner}#{processed}#{options.wrap.footer}"

if wrap and inject
for inj, pos in options.wrap.inject
inj.rem ?= ''
inj.repl ?= {}

switch inj.prop
when 'src'
inject = filepath.replace inj.rem, ''
when 'dest'
inject = dest.replace inj.rem, ''
else
inject = '#{' + pos + '}'
grunt.log.warn(
"Invalid inject property supplied, not
replacing positional marker"
)

inject = inject.replace k, v for k, v of inj.repl
processed = processed.replace('#{' + pos + '}', inject)

return processed
catch e
grunt.log.error e
grunt.fail.warn 'Template copilation failed.'

0 comments on commit 4146a89

Please sign in to comment.