mirror of
https://gitea.invidious.io/iv-org/shard-kilt.git
synced 2024-08-15 00:43:15 +00:00
Initial commit
This commit is contained in:
commit
f31dad17d7
12 changed files with 176 additions and 0 deletions
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
|
@ -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
|
||||
|
1
.travis.yml
Normal file
1
.travis.yml
Normal file
|
@ -0,0 +1 @@
|
|||
language: crystal
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -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.
|
65
README.md
Normal file
65
README.md
Normal file
|
@ -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 # => <compiled template>
|
||||
|
||||
|
||||
# Embedded
|
||||
|
||||
str = String.build do |str|
|
||||
Kilt.embed "path/to/template.slang", "str"
|
||||
end
|
||||
|
||||
puts str # => <compiled template>
|
||||
```
|
||||
|
||||
## 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
|
11
shard.yml
Normal file
11
shard.yml
Normal file
|
@ -0,0 +1,11 @@
|
|||
name: kilt
|
||||
version: 0.1.0
|
||||
|
||||
authors:
|
||||
- Jerome Gravel-Niquet <jeromegn@gmail.com>
|
||||
|
||||
license: MIT
|
||||
|
||||
development_dependencies:
|
||||
slang:
|
||||
github: jeromegn/slang
|
1
spec/fixtures/test.ecr
vendored
Normal file
1
spec/fixtures/test.ecr
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
<span><%= Process.pid %></span>
|
1
spec/fixtures/test.slang
vendored
Normal file
1
spec/fixtures/test.slang
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
span = Process.pid
|
27
spec/kilt_spec.cr
Normal file
27
spec/kilt_spec.cr
Normal file
|
@ -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("<span>#{Process.pid}</span>")
|
||||
end
|
||||
|
||||
it "renders slang" do
|
||||
render_file("spec/fixtures/test.slang").should eq("<span>#{Process.pid}</span>")
|
||||
end
|
||||
|
||||
it "works with classes" do
|
||||
View.new.to_s.should eq("<span>#{Process.pid}</span>")
|
||||
end
|
||||
|
||||
it "raises with unsupported filetype" do
|
||||
expect_raises(Kilt::Exception, "Unsupported template type \"abc\"") {
|
||||
render_file("test.abc")
|
||||
}
|
||||
end
|
||||
|
||||
end
|
9
spec/spec_helper.cr
Normal file
9
spec/spec_helper.cr
Normal file
|
@ -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
|
22
src/kilt.cr
Normal file
22
src/kilt.cr
Normal file
|
@ -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
|
5
src/kilt/exception.cr
Normal file
5
src/kilt/exception.cr
Normal file
|
@ -0,0 +1,5 @@
|
|||
module Kilt
|
||||
class Exception < ::Exception
|
||||
# Nothing special
|
||||
end
|
||||
end
|
3
src/kilt/version.cr
Normal file
3
src/kilt/version.cr
Normal file
|
@ -0,0 +1,3 @@
|
|||
module Kilt
|
||||
VERSION = "0.1.0"
|
||||
end
|
Loading…
Reference in a new issue