-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ported code from Crystal's standard library
- Loading branch information
Ary Borenszweig
committed
Dec 5, 2015
1 parent
5aad6bb
commit be483b9
Showing
10 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
language: crystal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Ary Borenszweig | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,45 @@ | ||
# html_builder | ||
DSL for creating HTML | ||
|
||
DSL for creating HTML programatically (extracted from Crystal's standard library). | ||
|
||
## Installation | ||
|
||
Add this to your application's `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
html_builder: | ||
github: crystal-lang/html_builder | ||
``` | ||
## Usage | ||
```crystal | ||
require "html_builder" | ||
|
||
html = HTML::Builder.new.a({href: "google.com"}) do | ||
text "crystal is awesome" | ||
end | ||
|
||
puts html # => "<a href="google.com">crystal is awesome</a> | ||
``` | ||
|
||
Or also you can use `build` method: | ||
|
||
``` | ||
HTML::Builder.new.build do | ||
a({href: "google.com"}) do | ||
text "crystal is awesome" | ||
end | ||
end # => "<a href="google.com">crystal is awesome</a> | ||
## Contributing | ||
1. Fork it ( https://github.com/crystal-lang/html_builder/fork ) | ||
2. Create your feature branch (git checkout -b my-new-feature) | ||
3. Commit your changes (git commit -am 'Add some feature') | ||
4. Push to the branch (git push origin my-new-feature) | ||
5. Create a new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: html_builder | ||
version: 0.1.0 | ||
|
||
authors: | ||
- Ary Borenszweig <[email protected]> | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require "./spec_helper" | ||
require "html/builder" | ||
|
||
describe HTML::Builder do | ||
it "builds html" do | ||
str = HTML::Builder.new.build do | ||
doctype | ||
html do | ||
head do | ||
title { text "Crystal Programming Language" } | ||
end | ||
body do | ||
a({href: "http://crystal-lang.org"}) { text "Crystal rocks!" } | ||
form({method: "POST"}) do | ||
input({name: "name"}) | ||
end | ||
end | ||
end | ||
end | ||
str.should eq %(<!DOCTYPE html><html><head><title>Crystal Programming Language</title></head><body><a href="http://crystal-lang.org">Crystal rocks!</a><form method="POST"><input name="name"></form></body></html>) | ||
end | ||
|
||
it "builds html with some tag attributes" do | ||
str = HTML::Builder.new.build do | ||
a({href: "http://crystal-lang.org", class: "crystal", id: "main"}) do | ||
text "Crystal rocks!" | ||
end | ||
end | ||
str.should eq %(<a href="http://crystal-lang.org" class="crystal" id="main">Crystal rocks!</a>) | ||
end | ||
|
||
it "builds html with an provided html string" do | ||
str = HTML::Builder.new.build do | ||
html "<section>Crystal rocks!</section>" | ||
end | ||
str.should eq %(<section>Crystal rocks!</section>) | ||
end | ||
|
||
it "builds html with a custom tag with attributes" do | ||
str = HTML::Builder.new.build do | ||
tag("section", {class: "crystal"}) { text "Crystal rocks!" } | ||
end | ||
str.should eq %(<section class="crystal">Crystal rocks!</section>) | ||
end | ||
|
||
it "escapes attribute values" do | ||
str = HTML::Builder.new.build do | ||
a({href: "<>"}) { } | ||
end | ||
str.should eq %(<a href="<>"></a>) | ||
end | ||
|
||
it "escapes text" do | ||
str = HTML::Builder.new.build do | ||
a { text "<>" } | ||
end | ||
str.should eq %(<a><></a>) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require "spec" | ||
require "../src/html_builder" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
require "html" | ||
|
||
# HTML::Builder | ||
# | ||
# HTML::Builder is a library for representing HTML in Crystal. | ||
# | ||
# Usage: | ||
# | ||
# ``` | ||
# html = HTML::Builder.new.a({href: "google.com"}) do | ||
# text "crystal is awesome" | ||
# end | ||
# | ||
# puts html # => "<a href="google.com">crystal is awesome</a> | ||
# ``` | ||
# | ||
# Or also you can use `build` method: | ||
# | ||
# ``` | ||
# HTML::Builder.new.build do | ||
# a({href: "google.com"}) do | ||
# text "crystal is awesome" | ||
# end | ||
# end # => "<a href="google.com">crystal is awesome</a> | ||
# ``` | ||
struct HTML::Builder | ||
def initialize | ||
@str = MemoryIO.new | ||
end | ||
|
||
def build | ||
with self yield self | ||
@str.to_s | ||
end | ||
|
||
# Renders `HTML` doctype tag. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build { doctype } # => <doctype/> | ||
# ``` | ||
def doctype | ||
@str << "<!DOCTYPE html>" | ||
end | ||
|
||
# Renders `BR` html tag. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build { br } # => <br/> | ||
# ``` | ||
def br | ||
@str << "<br/>" | ||
end | ||
|
||
# Renders `HR` html tag. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build { hr } # => <hr/> | ||
# ``` | ||
def hr | ||
@str << "<hr/>" | ||
end | ||
|
||
# Renders escaped text in html tag. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build { text "crystal is awesome" } | ||
# # => crystal is awesome | ||
# ``` | ||
def text(text) | ||
@str << HTML.escape(text) | ||
end | ||
|
||
# Renders the provided html string. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build { html "<p>crystal is awesome</p>" } | ||
# # => <>crystal is awesome</p> | ||
# ``` | ||
def html(html) | ||
@str << html | ||
end | ||
|
||
# Renders the provided html tag with any options. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build do | ||
# tag("section", {class: "crystal"}) { text "crystal is awesome" } | ||
# end | ||
# # => <section class="crystal">crystal is awesome</section> | ||
# ``` | ||
def tag(name, attrs = nil : Hash(Symbol, String)?) | ||
@str << "<#{name}" | ||
append_attributes_string(attrs) | ||
@str << ">" | ||
with self yield self | ||
@str << "</#{name}>" | ||
end | ||
|
||
{% for tag in %w(a b body button div em h1 h2 h3 head html i li ol p s script span strong table tbody td textarea thead title tr u ul form) %} | ||
# Renders `{{tag.id.upcase}}` html tag with any options. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build do | ||
# {{tag.id}}({ class: "crystal" }) { text "crystal is awesome" } | ||
# end | ||
# # => <{{tag.id}} class="crystal">crystal is awesome</{{tag.id}}> | ||
# ``` | ||
def {{tag.id}}(attrs = nil : Hash(Symbol, String)?) | ||
@str << "<{{tag.id}}" | ||
append_attributes_string(attrs) | ||
@str << ">" | ||
with self yield self | ||
@str << "</{{tag.id}}>" | ||
end | ||
{% end %} | ||
|
||
{% for tag in %w(link input img) %} | ||
# Renders `{{tag.id.upcase}}` html tag with any options. | ||
# | ||
# ``` | ||
# HTML::Builder.new.build do | ||
# {{tag.id}}({ class: "crystal" }) | ||
# end | ||
# # => <{{tag.id}} class="crystal"> | ||
# ``` | ||
def {{tag.id}}(attrs = nil : Hash(Symbol, String)?) | ||
@str << "<{{tag.id}}" | ||
append_attributes_string(attrs) | ||
@str << ">" | ||
end | ||
{% end %} | ||
|
||
private def append_attributes_string(attrs : Hash(Symbol, String)?) | ||
if attrs | ||
attrs.each do |name, value| | ||
@str << " " | ||
@str << name | ||
@str << %(=") | ||
HTML.escape(value, @str) | ||
@str << %(") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
struct HTML::Builder | ||
VERSION = "0.1.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "./html/builder" |