Permalink
Browse files

Added `include` support for non-jade files

note that since `include` is a directive and `script/style/etc` tags
are plain-text only, you cannot currently use `include` within them
  • Loading branch information...
1 parent 727a348 commit 8d7952083127bc877999581abe5f4f68f01fb0d5 @tj tj committed Aug 16, 2011
Showing with 62 additions and 6 deletions.
  1. +2 −1 examples/includes/head.jade
  2. +5 −0 examples/includes/style.css
  3. +13 −1 lib/compiler.js
  4. +1 −0 lib/nodes/index.js
  5. +29 −0 lib/nodes/literal.js
  6. +11 −3 lib/parser.js
  7. +1 −1 test/jade.test.js
@@ -1,4 +1,5 @@
head
title My Site
script(src='/javascripts/jquery.js')
- script(src='/javascripts/app.js')
+ script(src='/javascripts/app.js')
+ include style.css
@@ -0,0 +1,5 @@
+<style>
+ body {
+ padding: 50px;
+ }
+</style>
View
@@ -151,7 +151,19 @@ Compiler.prototype = {
|| node.constructor.toString().match(/function ([^(\s]+)()/)[1];
return this['visit' + name](node);
},
-
+
+ /**
+ * Visit literal `node`.
+ *
+ * @param {Literal} node
+ * @api public
+ */
+
+ visitLiteral: function(node){
+ var str = node.str.replace(/\n/g, '\\\\n');
+ this.buffer(str);
+ },
+
/**
* Visit all nodes in `block`.
*
View
@@ -14,5 +14,6 @@ exports.Block = require('./block');
exports.Mixin = require('./mixin');
exports.Filter = require('./filter');
exports.Comment = require('./comment');
+exports.Literal = require('./literal');
exports.BlockComment = require('./block-comment');
exports.Doctype = require('./doctype');
View
@@ -0,0 +1,29 @@
+
+/*!
+ * Jade - nodes - Literal
+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var Node = require('./node');
+
+/**
+ * Initialize a `Literal` node with the given `str.
+ *
+ * @param {String} str
+ * @api public
+ */
+
+var Literal = module.exports = function Literal(str) {
+ this.str = str;
+};
+
+/**
+ * Inherit from `Node`.
+ */
+
+Literal.prototype.__proto__ = Node.prototype;
View
@@ -315,16 +315,24 @@ Parser.prototype = {
var path = require('path')
, fs = require('fs')
, dirname = path.dirname
+ , basename = path.basename
, join = path.join;
if (!this.filename)
throw new Error('the "filename" option is required to use includes');
var path = name = this.expect('include').val.trim()
- , dir = dirname(this.filename)
- , path = join(dir, path + '.jade');
+ , dir = dirname(this.filename);
- var str = fs.readFileSync(path, 'utf8')
+ // non-jade
+ if (~basename(path).indexOf('.')) {
+ var path = join(dir, path)
+ , str = fs.readFileSync(path, 'utf8');
+ return new nodes.Literal(str);
+ }
+
+ var path = join(dir, path + '.jade')
+ , str = fs.readFileSync(path, 'utf8')
, parser = new Parser(str, path)
, ast = parser.parse();
View
@@ -801,7 +801,7 @@ module.exports = {
assert.equal(html, render(str));
},
-
+
'test renderFile() fs exception': function(assert, beforeExit){
var called;
jade.renderFile('foo', function(err, str){

0 comments on commit 8d79520

Please sign in to comment.