From b152bf1153be577631c89c26659495d4d78f629b Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 15 Feb 2016 11:50:18 -0500 Subject: [PATCH] Added slang adapter + specs, reorganized specs and adapters --- README.md | 15 +++++++++++---- shard.yml | 2 ++ spec/fixtures/test.slang | 1 + .../crustache_spec.cr} | 8 ++++---- spec/kilt/slang_spec.cr | 18 ++++++++++++++++++ spec/kilt_spec.cr | 10 +++++----- spec/spec_helper.cr | 7 +------ spec/support/raw_engine.cr | 5 +++++ src/kilt.cr | 6 +++--- src/kilt/crustache.cr | 4 ++++ src/kilt/ecr.cr | 3 +++ src/kilt/slang.cr | 4 ++++ src/kilt/version.cr | 2 +- src/mustache.cr | 4 ---- 14 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 spec/fixtures/test.slang rename spec/{mustache_spec.cr => kilt/crustache_spec.cr} (78%) create mode 100644 spec/kilt/slang_spec.cr create mode 100644 spec/support/raw_engine.cr create mode 100644 src/kilt/crustache.cr create mode 100644 src/kilt/ecr.cr create mode 100644 src/kilt/slang.cr delete mode 100644 src/mustache.cr diff --git a/README.md b/README.md index d7c130a..cb3a325 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Simplify developers' lives by abstracting template rendering for multiple templa | -------- | --------------- | ------------------ | ---------- | | ECR | .ecr | none (part of the stdlib) | | | Mustache | .mustache | [crustache](https://github.com/MakeNowJust/crustache) | [@MakeNowJust](https://github.com/MakeNowJust) | +| Slang | .slang | [slang](https://github.com/jeromegn/slang) | [@jeromegn](https://github.com/jeromegn) | See also: [Registering your own template engine](#registering-your-own-template-engine). @@ -39,7 +40,10 @@ Both macros take a `filename` and a `io_name` (the latter defaults to `"__kilt_i ```crystal require "kilt" -require "slang" # if you want to use Slang templates, for instance + +# For slang, try: +require "slang" +require "kilt/slang" # With a Class @@ -51,12 +55,14 @@ puts YourView.new.to_s # => # Embedded +str = Kilt.render "path/to/template.slang" + +# or + str = String.build do |__kilt_io__| Kilt.embed "path/to/template.slang" end -# or `Kilt.render "path/to/template.slang"` - puts str # => ``` @@ -73,7 +79,7 @@ module MyEngine end end -Kilt.register_engine("myeng", ::MyEngine.embed) +Kilt.register_engine("myeng", MyEngine.embed) ``` This can be part of your own `my-engine` library: in this case it should depend @@ -94,3 +100,4 @@ Please contribute your own "adapter" if you create a template language for Cryst - [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer - [waterlink](https://github.com/waterlink) Oleksii Fedorov +- [MakeNowJust](https://github.com/MakeNowJust) TSUYUSATO Kitsune \ No newline at end of file diff --git a/shard.yml b/shard.yml index 6e81b03..91e707f 100644 --- a/shard.yml +++ b/shard.yml @@ -7,5 +7,7 @@ authors: license: MIT development_dependencies: + slang: + github: jeromegn/slang crustache: github: MakeNowJust/crustache diff --git a/spec/fixtures/test.slang b/spec/fixtures/test.slang new file mode 100644 index 0000000..e8325c7 --- /dev/null +++ b/spec/fixtures/test.slang @@ -0,0 +1 @@ +span = Process.pid \ No newline at end of file diff --git a/spec/mustache_spec.cr b/spec/kilt/crustache_spec.cr similarity index 78% rename from spec/mustache_spec.cr rename to spec/kilt/crustache_spec.cr index 5ebc3b4..8a1b9e5 100644 --- a/spec/mustache_spec.cr +++ b/spec/kilt/crustache_spec.cr @@ -1,5 +1,5 @@ -require "./spec_helper" -require "../src/mustache" +require "../spec_helper" +require "../../src/kilt/crustache" class MustacheView def has_key?(name) @@ -13,9 +13,9 @@ class MustacheView Kilt.file "spec/fixtures/test.mustache", "__kilt_io__", self end -describe Kilt do +describe "kilt/crustache" do - it "renders ecr" do + it "renders crustache" do Kilt.render("spec/fixtures/test.mustache", { "pid" => Process.pid }).should eq("#{Process.pid}") end diff --git a/spec/kilt/slang_spec.cr b/spec/kilt/slang_spec.cr new file mode 100644 index 0000000..1ece1b5 --- /dev/null +++ b/spec/kilt/slang_spec.cr @@ -0,0 +1,18 @@ +require "../spec_helper" +require "../../src/kilt/slang" + +class SlangView + Kilt.file "spec/fixtures/test.slang" +end + +describe "kilt/slang" do + + it "renders slang" do + Kilt.render("spec/fixtures/test.slang").should eq("#{Process.pid}") + end + + it "works with classes" do + SlangView.new.to_s.should eq("#{Process.pid}") + end + +end diff --git a/spec/kilt_spec.cr b/spec/kilt_spec.cr index 81e02a2..dc3a3e5 100644 --- a/spec/kilt_spec.cr +++ b/spec/kilt_spec.cr @@ -10,11 +10,6 @@ describe Kilt do Kilt.render("spec/fixtures/test.ecr").should eq("#{Process.pid}") end - it "renders registered engine" do - Kilt.register_engine "raw", Raw.embed - Kilt.render("spec/fixtures/test.raw").should eq("Hello World!") - end - it "works with classes" do View.new.to_s.should eq("#{Process.pid}") end @@ -25,4 +20,9 @@ describe Kilt do } end + it "renders registered engine" do + Kilt.register_engine "raw", Raw.embed + Kilt.render("spec/fixtures/test.raw").should eq("Hello World!") + end + end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 1984266..d69d98e 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,8 +1,3 @@ require "spec" require "../src/kilt" - -module Raw - macro embed(filename, io) - {{ io.id }} << {{`cat #{filename}`.stringify}} - end -end \ No newline at end of file +require "./support/raw_engine" \ No newline at end of file diff --git a/spec/support/raw_engine.cr b/spec/support/raw_engine.cr new file mode 100644 index 0000000..2e0f9dc --- /dev/null +++ b/spec/support/raw_engine.cr @@ -0,0 +1,5 @@ +module Raw + macro embed(filename, io) + {{ io.id }} << {{`cat #{filename}`.stringify}} + end +end \ No newline at end of file diff --git a/src/kilt.cr b/src/kilt.cr index 6c05e45..9a9a43c 100644 --- a/src/kilt.cr +++ b/src/kilt.cr @@ -1,5 +1,5 @@ -require "ecr/macros" -require "./kilt/*" +require "./kilt/version" +require "./kilt/exception" module Kilt # macro only constant @@ -32,4 +32,4 @@ module Kilt end end -Kilt.register_engine("ecr", embed_ecr) +require "./kilt/ecr" diff --git a/src/kilt/crustache.cr b/src/kilt/crustache.cr new file mode 100644 index 0000000..a470c1a --- /dev/null +++ b/src/kilt/crustache.cr @@ -0,0 +1,4 @@ +require "../kilt" +require "crustache" + +Kilt.register_engine "mustache", embed_mustache \ No newline at end of file diff --git a/src/kilt/ecr.cr b/src/kilt/ecr.cr new file mode 100644 index 0000000..80a354f --- /dev/null +++ b/src/kilt/ecr.cr @@ -0,0 +1,3 @@ +require "ecr/macros" + +Kilt.register_engine("ecr", embed_ecr) \ No newline at end of file diff --git a/src/kilt/slang.cr b/src/kilt/slang.cr new file mode 100644 index 0000000..17eb67b --- /dev/null +++ b/src/kilt/slang.cr @@ -0,0 +1,4 @@ +require "../kilt" +require "slang" + +Kilt.register_engine "slang", Slang.embed \ No newline at end of file diff --git a/src/kilt/version.cr b/src/kilt/version.cr index 558fa90..6f369f2 100644 --- a/src/kilt/version.cr +++ b/src/kilt/version.cr @@ -1,3 +1,3 @@ module Kilt - VERSION = "0.2.1" + VERSION = "0.3.0" end diff --git a/src/mustache.cr b/src/mustache.cr deleted file mode 100644 index 497777f..0000000 --- a/src/mustache.cr +++ /dev/null @@ -1,4 +0,0 @@ -require "./kilt" -require "crustache" - -Kilt.register_engine "mustache", embed_mustache