Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support slim require #26

Merged
merged 9 commits into from
Aug 15, 2015
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ The options are the same as what's supported by `slimrb`.

Set `bundler: true` to invoke `slimrb` via bundler.

Set `chdir: true` to invoke `slimrb` in the same directory as the file currently being processed (for including files in the same directory).

You can require one of the [plug-ins](https://github.com/slim-template/slim/blob/master/README.md#plugins) available with Slim with the ```require``` key. Value can be ```string``` or ```array```.
```javascript
slim({
require: 'slim/include'
options: 'include_dirs=[".", "common/includes", "./includes"]'
})

slim({
require: ['slim/include', 'slim/logic_less']
})
```
Note that when using slim/include you will likely need to use the 'include_dirs' option (as outlined above). See the tests on how to configure include directories with the [inclulde partials plugin](https://github.com/slim-template/slim/blob/master/doc/include.md).

You can also add [custom options](https://github.com/slim-template/slim/blob/master/README.md#available-options) with ```options``` key. Value can be ```string``` or ```array```.

```javascript
Expand Down
12 changes: 12 additions & 0 deletions coffee/index.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
spawn = require('spawn-cmd').spawn
through = require 'through2'
gutil = require 'gulp-util'
path = require 'path'
PluginError = gutil.PluginError

PLUGIN_NAME = 'gulp-slim'
Expand Down Expand Up @@ -38,6 +39,15 @@ module.exports = (options = {}) ->
args.push '--locals'
args.push JSON.stringify options.data

if options.require
if options.require.constructor is Array
options.require.forEach (lib) ->
args.push '-r'
args.push lib
else if options.require.constructor is String
args.push '-r'
args.push options.require

if options.options
if options.options.constructor is Array
options.options.forEach (opt) ->
Expand All @@ -62,6 +72,8 @@ module.exports = (options = {}) ->
ext = if options.erb then '.erb' else '.html'
file.path = gutil.replaceExtension file.path, ext

spawn_options.cwd = path.dirname(file.path) if options.chdir

program = spawn cmnd, args, spawn_options

# create buffer
Expand Down
1 change: 1 addition & 0 deletions test/expect/test_include.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><head><title>Slim Examples</title><meta content="template language" name="keywords" /><meta content="John Doe" name="author" /></head><body><h1>Markup examples</h1><div><p>This content is from an include file</p></div><div id="content"><p>This example shows you how a basic Slim file looks like.</p><div class="subfile"><p class="classy"> This is from an include file in a subfolder!</p></div></div><div id="footer"><Copyright>© 2014</Copyright></div></body></html>
2 changes: 1 addition & 1 deletion test/fixtures/include.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ html

#content
p This example shows you how a basic Slim file looks like.
include include-file
include ./include-file

div id="footer"
Copyright &copy; 2014
2 changes: 2 additions & 0 deletions test/fixtures/include_me.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div
p This content is from an include file
2 changes: 2 additions & 0 deletions test/fixtures/includes/another_include_file.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div.subfile
p.classy This is from an include file in a subfolder!
17 changes: 17 additions & 0 deletions test/fixtures/test_include.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
doctype html
html
head
title Slim Examples
meta name="keywords" content="template language"
meta name="author" content="John Doe"

body
h1 Markup examples
include ./include_me

#content
p This example shows you how a basic Slim file looks like.
include ./includes/another_include_file

div id="footer"
Copyright © 2014
28 changes: 27 additions & 1 deletion test/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,38 @@ describe 'gulp-slim', () ->
done()
stream.write slimFile

it 'should compile include a slim file when include library is requried', (done) ->
slimFile = createFile 'test_include.slim'
stream = slim require: 'slim/include', options: 'include_dirs=[".", "test/fixtures", "test/fixtures/includes"]'
stream.on 'data', (htmlFile) ->
should.exist htmlFile
should.exist htmlFile.path
should.exist htmlFile.relative
should.exist htmlFile.contents
htmlFile.path.should.equal path.join __dirname, 'fixtures', 'test_include.html'
String(htmlFile.contents).should.equal fs.readFileSync path.join(__dirname, 'expect/test_include.html'), 'utf8'
done()
stream.write slimFile

it 'should compile a slim file when included file is in same directory', (done) ->
slimFile = createFile 'test_include.slim'
stream = slim require: 'slim/include', chdir: true
stream.on 'data', (htmlFile) ->
should.exist htmlFile
should.exist htmlFile.path
should.exist htmlFile.relative
should.exist htmlFile.contents
htmlFile.path.should.equal path.join __dirname, 'fixtures', 'test_include.html'
String(htmlFile.contents).should.equal fs.readFileSync path.join(__dirname, 'expect/test_include.html'), 'utf8'
done()
stream.write slimFile

it 'should include additional file with include plugin', (done) ->
slimFile = createFile 'include.slim'
stream = slim {
pretty:true
include: true
options: ["include_dirs=['test/fixtures']"]
options: "include_dirs=['.', 'test/fixtures']"
}
stream.on 'data', (htmlFile) ->
should.exist htmlFile
Expand Down