Skip to content

Commit

Permalink
Ported code from Crystal's standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Dec 5, 2015
1 parent 5aad6bb commit be483b9
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .gitignore
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

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
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.
45 changes: 44 additions & 1 deletion README.md
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
7 changes: 7 additions & 0 deletions shard.yml
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
59 changes: 59 additions & 0 deletions spec/html_builder_spec.cr
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="&lt;&gt;"></a>)
end

it "escapes text" do
str = HTML::Builder.new.build do
a { text "<>" }
end
str.should eq %(<a>&lt;&gt;</a>)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/html_builder"
144 changes: 144 additions & 0 deletions src/html/builder/builder.cr
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
3 changes: 3 additions & 0 deletions src/html/builder/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct HTML::Builder
VERSION = "0.1.0"
end
1 change: 1 addition & 0 deletions src/html_builder.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "./html/builder"

0 comments on commit be483b9

Please sign in to comment.