From bee317768441bbf5c778a2d68c491b4894c4c8c2 Mon Sep 17 00:00:00 2001 From: Scott Brady Date: Fri, 25 Apr 2014 13:18:35 -0700 Subject: [PATCH] Made the +include directive work with standalone placement. --- package.json | 2 +- spec/compiler_spec.coffee | 22 ++++++++++++++-------- spec/suites/haml_coffee_spec.json | 14 ++++++++++++-- src/haml-coffee.coffee | 2 ++ src/hamlc.coffee | 6 ++++-- src/nodes/directive.coffee | 9 +++++++-- src/nodes/node.coffee | 30 +++++++++++++++--------------- 7 files changed, 55 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 67c7012..4dedd97 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,6 @@ "url": "https://github.com/netzpirat/haml-coffee/issues" }, "scripts": { - "test": "jasmine-node --coffee spec" + "test": "grunt test" } } diff --git a/spec/compiler_spec.coffee b/spec/compiler_spec.coffee index 9b2d49c..6cb9df5 100644 --- a/spec/compiler_spec.coffee +++ b/spec/compiler_spec.coffee @@ -1,4 +1,5 @@ -fs = require 'fs' +fs = require 'fs' +path = require 'path' CoffeeScript = require 'coffee-script' HamlCoffee = require '../src/haml-coffee' @@ -33,18 +34,21 @@ for suite in suites extendScope : if spec.config?.extend_scope is 'true' then true else false placement : spec.config?.placement || 'global' format : spec.config?.format || 'xhtml' + pwd : spec.config?.pwd || '' } + if spec.haml_template + hamlTemplatePath = "spec/suites/templates/#{ spec.haml_template }.haml" + spec.haml = fs.readFileSync(hamlTemplatePath).toString() + if config.pwd is '' + config.pwd = path.resolve(path.dirname(hamlTemplatePath)) + compiler = new HamlCoffee(config) report = "Generated output doesn't match the expected result.\n\n" report += "-------------------- Compiler settings ------------------------\n" report += JSON.stringify(config) report += "\n-------------------- Haml template ----------------------------\n" - - if spec.haml_template - spec.haml = fs.readFileSync("spec/suites/templates/#{ spec.haml_template }.haml").toString() - report += spec.haml if spec.partials && config.placement is 'global' @@ -75,9 +79,11 @@ for suite in suites try template = CoffeeScript.compile cst - eval template - - html = window.HAML.test(spec.locals) + if config.placement is 'standalone' + html = eval(template)(spec.locals) + else # global + eval template + html = window.HAML.test(spec.locals) catch error report += "\n-------------- Error compiling JST -------------------------\n" diff --git a/spec/suites/haml_coffee_spec.json b/spec/suites/haml_coffee_spec.json index 94a0d11..f4531c5 100644 --- a/spec/suites/haml_coffee_spec.json +++ b/spec/suites/haml_coffee_spec.json @@ -190,7 +190,7 @@ "format" : "html5" } }, - + "class concatenation" : { "haml_template" : "coffee/class", "html_template" : "coffee/class" @@ -542,7 +542,7 @@ }, "directives" : { - "include" : { + "include global" : { "haml_template" : "directives/include", "html_template" : "directives/include", "config": { @@ -554,6 +554,16 @@ "locals" : { "title": "Title For the partial." } + }, + "include standalone" : { + "haml_template" : "directives/include", + "html_template" : "directives/include", + "config": { + "placement": "standalone" + }, + "locals" : { + "title": "Title For the partial." + } } } diff --git a/src/haml-coffee.coffee b/src/haml-coffee.coffee index b854cc1..011c372 100644 --- a/src/haml-coffee.coffee +++ b/src/haml-coffee.coffee @@ -52,6 +52,7 @@ module.exports = class HamlCoffee @options.hyphenateDataAttrs ?= true @options.preserveTags ?= 'pre,textarea' @options.selfCloseTags ?= 'meta,img,link,br,hr,input,area,param,col,base' + @options.extension ?= 'haml' if @options.placement is 'global' @options.name ?= 'test' @@ -168,6 +169,7 @@ module.exports = class HamlCoffee placement : override.placement || @options.placement namespace : override.namespace || @options.namespace name : override.name || @options.name + compilerOptions : @options } # Get the matching node type for the given expression. This diff --git a/src/hamlc.coffee b/src/hamlc.coffee index d637718..ebdfb3e 100644 --- a/src/hamlc.coffee +++ b/src/hamlc.coffee @@ -1,4 +1,5 @@ -fs = require 'fs' +fs = require 'fs' +path = require 'path' Compiler = require './haml-coffee' @@ -105,7 +106,8 @@ module.exports = else options.filename = filename - source = fs.readFileSync(filename, 'utf8') + options.pwd = path.dirname(filename) + source = fs.readFileSync(filename, 'utf8') if options.cache __expressCache[filename] = module.exports.compile(source, options) diff --git a/src/nodes/directive.coffee b/src/nodes/directive.coffee index fc2bffd..84cb03d 100644 --- a/src/nodes/directive.coffee +++ b/src/nodes/directive.coffee @@ -44,16 +44,21 @@ module.exports = class Directive extends Node # we need to compile the referenced template and attach it as a # precompiled (standalone) template here. + templatePath = path.join( + @options.compilerOptions.pwd, + "#{ name }.#{ @options.compilerOptions.extension }" + ) + # Read the source. try - source = fs.readFileSync(name).toString() + source = fs.readFileSync(templatePath).toString() catch error console.error " Error opening file: %s", error console.error error # Compile and build the source function. Compiler = require '../haml-coffee' - compiler = new Compiler(@options) + compiler = new Compiler(@options.compilerOptions) compiler.parse source code = CoffeeScript.compile(compiler.precompile(), bare: true) statement = "`(function(){#{code}}).apply(#{context})`" diff --git a/src/nodes/node.coffee b/src/nodes/node.coffee index c58ad7c..eb4c319 100644 --- a/src/nodes/node.coffee +++ b/src/nodes/node.coffee @@ -32,8 +32,8 @@ module.exports = class Node # @option options [Boolean] escapeHtml whether to escape the rendered HTML or not # @option options [String] format the template format, either `xhtml`, `html4` or `html5` # - constructor: (@expression = '', options = {}) -> - @parentNode = options.parentNode + constructor: (@expression = '', @options = {}) -> + @parentNode = @options.parentNode @children = [] @opener = @closer = null @@ -42,7 +42,7 @@ module.exports = class Node @silent = false # Preserve whitespace on all children - @preserveTags = options.preserveTags.split(',') + @preserveTags = @options.preserveTags.split(',') @preserve = false @wsRemoval = { @@ -50,20 +50,20 @@ module.exports = class Node inside: false } - @escapeHtml = options.escapeHtml - @escapeAttributes = options.escapeAttributes - @cleanValue = options.cleanValue - @format = options.format - @hyphenateDataAttrs = options.hyphenateDataAttrs - @selfCloseTags = options.selfCloseTags.split(',') - @uglify = options.uglify + @escapeHtml = @options.escapeHtml + @escapeAttributes = @options.escapeAttributes + @cleanValue = @options.cleanValue + @format = @options.format + @hyphenateDataAttrs = @options.hyphenateDataAttrs + @selfCloseTags = @options.selfCloseTags.split(',') + @uglify = @options.uglify - @codeBlockLevel = options.codeBlockLevel - @blockLevel = options.blockLevel + @codeBlockLevel = @options.codeBlockLevel + @blockLevel = @options.blockLevel - @placement = options.placement - @namespace = options.namespace - @name = options.name + @placement = @options.placement + @namespace = @options.namespace + @name = @options.name # Add a child node. #