Skip to content

Instantly share code, notes, and snippets.

@kerotaa
Last active December 23, 2015 06:49
Show Gist options
  • Select an option

  • Save kerotaa/6596364 to your computer and use it in GitHub Desktop.

Select an option

Save kerotaa/6596364 to your computer and use it in GitHub Desktop.
path = require 'path'
grunt.registerMultiTask 'template_concat', ->
indent = (text, indentStr, width)->
lines = text.split "\n"
t = ''
for i in [0...width]
t += indentStr
lines.forEach (line, index)->
lines[index] = t + line
lines.join "\n"
compile_template = (filepath)->
if !grunt.file.exists filepath
grunt.util.error 'File not found: ' + filepath
code = grunt.file.read filepath
matches = code.match /(\n?[\s\t]*)<%=\s*.+\s*%>/g
base_dir = path.dirname(filepath)
if matches
matches.forEach (value, index, arr)->
m = value.match(/(\n*)([\s\t]*)(<%=\s*(.+)\s*%>)/)
n = m[4].replace /\s+$/, ''
p = base_dir + '/' + n
compile_template.compiled[p] = compile_template.compiled[p] || compile_template(p)
code = code.replace(m[2] + m[3], indent(compile_template.compiled[p], m[2], 1))
code
compile_template.compiled = {}
compiled_code = compile_template this.data
grunt.file.write this.target, compiled_code
grunt.log.oklns 'succeed: ' + this.target
@kerotaa

kerotaa commented Sep 23, 2013

Copy link
Copy Markdown
Author

・色々とおかしかった部分を修正
・インデントを良い感じにしてくれるようにした

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment