From f31dad17d7de0132200ca989be6f8de259c0ca63 Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Sun, 14 Feb 2016 13:05:54 -0500 Subject: [PATCH] Initial commit --- .gitignore | 10 +++++++ .travis.yml | 1 + LICENSE | 21 +++++++++++++ README.md | 65 ++++++++++++++++++++++++++++++++++++++++ shard.yml | 11 +++++++ spec/fixtures/test.ecr | 1 + spec/fixtures/test.slang | 1 + spec/kilt_spec.cr | 27 +++++++++++++++++ spec/spec_helper.cr | 9 ++++++ src/kilt.cr | 22 ++++++++++++++ src/kilt/exception.cr | 5 ++++ src/kilt/version.cr | 3 ++ 12 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 shard.yml create mode 100644 spec/fixtures/test.ecr create mode 100644 spec/fixtures/test.slang create mode 100644 spec/kilt_spec.cr create mode 100644 spec/spec_helper.cr create mode 100644 src/kilt.cr create mode 100644 src/kilt/exception.cr create mode 100644 src/kilt/version.cr diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8cf75a --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/doc/ +/libs/ +/.crystal/ +/.shards/ + + +# Libraries don't need dependency lock +# Dependencies will be locked in application that uses them +/shard.lock + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ffc7b6a --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: crystal diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f7175ef --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Jerome Gravel-Niquet + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c628c8d --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +# Kilt + +Generic templating interface for Crystal. + +## Goal + +Simplify developers' lives by abstracting template rendering for multiple template languages. + +## Supported + +| Language | File extensions | Required libraries | +| -------- | --------------- | ------------------ | +| ECR | .ecr | none | +| Slang | .slang | [slang](https://github.com/jeromegn/slang) | + +## Installation + +Add this to your application's `shard.yml`: + +```yaml +dependencies: + kilt: + github: jeromegn/kilt + + # Any other template languages Crystal shard +``` + +## Usage + +Add template language dependencies, as listed in the support table above. + +```crystal +require "kilt" +require "slang" # if you want to use Slang templates, for instance + +# With a Class + +class YourView + Kilt.file("path/to/template.ecr") # Adds a to_s method +end +puts YourView.new.to_s # => + + +# Embedded + +str = String.build do |str| + Kilt.embed "path/to/template.slang", "str" +end + +puts str # => +``` + +## Contributing + +Please contribute your own "adapter" if you create a template language for Crystal that's not yet supported here! + +1. Fork it ( https://github.com/jeromegn/kilt/fork ) +2. Create your feature branch (git checkout -b my-awesome-template-language) +3. Commit your changes (git commit -am 'Add my-awesome-template-language') +4. Push to the branch (git push origin my-awesome-template-language) +5. Create a new Pull Request + +## Contributors + +- [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..cf18406 --- /dev/null +++ b/shard.yml @@ -0,0 +1,11 @@ +name: kilt +version: 0.1.0 + +authors: + - Jerome Gravel-Niquet + +license: MIT + +development_dependencies: + slang: + github: jeromegn/slang \ No newline at end of file diff --git a/spec/fixtures/test.ecr b/spec/fixtures/test.ecr new file mode 100644 index 0000000..7ae3255 --- /dev/null +++ b/spec/fixtures/test.ecr @@ -0,0 +1 @@ +<%= Process.pid %> \ No newline at end of file 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/kilt_spec.cr b/spec/kilt_spec.cr new file mode 100644 index 0000000..98f4d73 --- /dev/null +++ b/spec/kilt_spec.cr @@ -0,0 +1,27 @@ +require "./spec_helper" + +class View + Kilt.file "spec/fixtures/test.ecr" +end + +describe Kilt do + + it "renders ecr" do + render_file("spec/fixtures/test.ecr").should eq("#{Process.pid}") + end + + it "renders slang" do + render_file("spec/fixtures/test.slang").should eq("#{Process.pid}") + end + + it "works with classes" do + View.new.to_s.should eq("#{Process.pid}") + end + + it "raises with unsupported filetype" do + expect_raises(Kilt::Exception, "Unsupported template type \"abc\"") { + render_file("test.abc") + } + end + +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..92958ff --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,9 @@ +require "spec" +require "slang" +require "../src/kilt" + +macro render_file(filename) + String.build do |__io__| + Kilt.embed({{filename}}, "__io__") + end +end \ No newline at end of file diff --git a/src/kilt.cr b/src/kilt.cr new file mode 100644 index 0000000..b9ff7d2 --- /dev/null +++ b/src/kilt.cr @@ -0,0 +1,22 @@ +require "ecr/macros" +require "./kilt/*" + +module Kilt + + macro embed(filename, io_name = "__io__") + {% if filename.ends_with?(".ecr") %} + embed_ecr({{filename}}, {{io_name}}) + {% elsif filename.ends_with?(".slang") %} + embed_slang({{filename}}, {{io_name}}) + {% else %} + raise Kilt::Exception.new("Unsupported template type \"" + {{filename.split(".").last}} + "\"") + {% end %} + end + + macro file(filename) + def to_s(__io__) + Kilt.embed({{filename}}, "__io__") + end + end + +end diff --git a/src/kilt/exception.cr b/src/kilt/exception.cr new file mode 100644 index 0000000..2e43dfd --- /dev/null +++ b/src/kilt/exception.cr @@ -0,0 +1,5 @@ +module Kilt + class Exception < ::Exception + # Nothing special + end +end \ No newline at end of file diff --git a/src/kilt/version.cr b/src/kilt/version.cr new file mode 100644 index 0000000..11e892d --- /dev/null +++ b/src/kilt/version.cr @@ -0,0 +1,3 @@ +module Kilt + VERSION = "0.1.0" +end