Skip to content

Commit

Permalink
Optimize and support Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
shootingfly committed Aug 8, 2020
1 parent f2f331e commit bceb2f0
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 79 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ A library for writing HTML in plain Crystal.
```crystal
require "water"
class Water::Page
class Water
def_custom_tag hello_world
end
page = Water::Page.new do
page = Water.new do
doctype
html {
head {
Expand Down Expand Up @@ -148,6 +148,9 @@ puts page
</html>
```

## Note
Because `p` and `select` are used by Crystal. tag `p` is renamed to `para`, tag `select` is renamed to `select_tag`.

## Contributing

1. Fork it (<https://github.com/shootingfly/water/fork>)
Expand Down
4 changes: 2 additions & 2 deletions spec/water/page_spec.cr → spec/water/water_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "../spec_helper"

describe Water::Page do
describe Water do
it ".new" do
str = Water::Page.new do
str = Water.new do
{{ run("../read.cr", "spec/fixtures/dummy.water") }}
end
str.should eq File.read("spec/fixtures/dummy.html")
Expand Down
40 changes: 19 additions & 21 deletions src/water.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ require "./water/tag"
require "./water/kilts/embed"
require "./water/version"

module Water
class Page
include Water::Tag
class Water
include Water::Tag

property lines : Array(String)
property indents : Array(Int32)
property current_indent : Int32
property lines : Array(String)
property indents : Array(Int32)
property current_indent : Int32

def initialize(@lines = [] of String, @indents = [] of Int32, @current_indent = 0)
end
def initialize(@lines = [] of String, @indents = [] of Int32, @current_indent = 0)
end

def self.new
builder = new
with builder yield
render_string(builder)
end
def self.new
builder = new
with builder yield
render_string(builder)
end

def self.render_string(builder)
String.build do |str|
builder.lines.each_with_index do |line, index|
str << " " * builder.indents[index]
str << line
str << "\n"
end
str.chomp!(10) # Remove last newline "\n"
def self.render_string(builder)
String.build do |str|
builder.lines.each_with_index do |line, index|
str << " " * builder.indents[index]
str << line
str << "\n"
end
str.chomp!(10) # Remove last newline "\n"
end
end
end
13 changes: 9 additions & 4 deletions src/water/kilts/embed.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module Water
macro embed(filename, io_name)
{{ io_name.id }} << {{ run("./embedder.cr", filename) }}
{{ io_name.id }}
class Water
macro embed(filename, io_name, layout_file = nil)
{% if layout_file %}
{{ io_name.id }} << {{ read_file(layout_file).gsub(/yield_content/, read_file(filename)).id }}
{{ io_name.id }}
{% else %}
{{ io_name.id }} << {{ run("./embedder.cr", filename) }}
{{ io_name.id }}
{% end %}
end
end
2 changes: 1 addition & 1 deletion src/water/kilts/embedder.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
puts "Water::Page.new do"
puts "Water.new do"
puts File.read(ARGV[0])
puts "end"
6 changes: 1 addition & 5 deletions src/water/tag.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "./tag/self_close_tag"
require "./tag/custom_tag"
require "html"

module Water
class Water
module Tag
include Water::OpenTag
include Water::SelfCloseTag
Expand All @@ -21,10 +21,6 @@ module Water
end
end

def strip_attributes(attributes : String)
attributes == "" ? "" : " #{attributes}"
end

def script(attributes, content)
@lines << "<script #{attributes}>#{content}</script>"
@indents << @current_indent
Expand Down
20 changes: 15 additions & 5 deletions src/water/tag/custom_tag.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Water
class Water
module CustomTag
macro def_custom_tag(tag)
def {{tag.id}}(attributes = "")
@lines << "<{{tag.id.gsub(/_/, "-")}}#{strip_attributes(attributes)}>"
def {{tag.id}}
@lines << "<{{tag.id.gsub(/_/, "-")}}>"
@indents << @current_indent
@current_indent += 1
yield
Expand All @@ -11,8 +11,18 @@ module Water
@indents << @current_indent
end

def {{tag.id}}(attributes, content)
@lines << "<{{tag.id.gsub(/_/, "-")}}#{strip_attributes(attributes)}>#{strip_content(content)}</{{tag.id.gsub(/_/, "-")}}>"
def {{tag.id}}(attributes : String)
@lines << "<{{tag.id.gsub(/_/, "-")}} #{attributes}>"
@indents << @current_indent
@current_indent += 1
yield
@current_indent -= 1
@lines << "</{{tag.id.gsub(/_/, "-")}}>"
@indents << @current_indent
end

def {{tag.id}}(attributes : String, content)
@lines << "<{{tag.id.gsub(/_/, "-")}} #{attributes}>#{strip_content(content)}</{{tag.id.gsub(/_/, "-")}}>"
@indents << @current_indent
end

Expand Down
52 changes: 21 additions & 31 deletions src/water/tag/open_tag.cr
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
module Water
class Water
module OpenTag
macro def_open_tag(tag)
def {{tag.id}}(attributes = "")
@lines << "<{{tag.id}}#{strip_attributes(attributes)}>"
macro def_open_tag(tag, alias_name = nil)
{% real_tag = alias_name || tag %}

def {{tag.id}}
@lines << "<{{real_tag.id}}>"
@indents << @current_indent
@current_indent += 1
yield
@current_indent -= 1
@lines << "</{{tag.id}}>"
@indents << @current_indent
end

def {{tag.id}}(attributes, content)
@lines << "<{{tag.id}}#{strip_attributes(attributes)}>#{strip_content(content)}</{{tag.id}}>"
@indents << @current_indent
end

def {{tag.id}}(content)
@lines << "<{{tag.id}}>#{strip_content(content)}</{{tag.id}}>"
@lines << "</{{real_tag.id}}>"
@indents << @current_indent
end
end

OPEN_TAGS = %w(a abbr address article aside b bdi body button code details dialog div dd dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header html i iframe label li main mark menuitem meter nav ol option pre progress rp rt ruby s section small span strong summary table tbody td textarea th thead time title tr u ul video wbr)
{% for tag in OPEN_TAGS %}
def_open_tag {{tag}}
{% end %}

macro def_renamed_open_tag(tag, alias_name)
def {{alias_name.id}}(attributes = "")
@lines << "<{{tag.id}}#{strip_attributes(attributes)}>"
def {{tag.id}}(attributes : String)
@lines << "<{{real_tag.id}} #{attributes}>"
@indents << @current_indent
@current_indent += 1
yield
@current_indent -= 1
@lines << "</{{tag.id}}>"
@lines << "</{{real_tag.id}}>"
@indents << @current_indent
end

def {{alias_name.id}}(attributes, content)
@lines << "<{{tag.id}}#{strip_attributes(attributes)}>#{strip_content(content)}</{{tag.id}}>"
def {{tag.id}}(attributes : String, content)
@lines << "<{{real_tag.id}} #{attributes}>#{strip_content(content)}</{{real_tag.id}}>"
@indents << @current_indent
end

def {{alias_name.id}}(content)
@lines << "<{{tag.id}}>#{strip_content(content)}</{{tag.id}}>"
def {{tag.id}}(content)
@lines << "<{{real_tag.id}}>#{strip_content(content)}</{{real_tag.id}}>"
@indents << @current_indent
end
end

def_renamed_open_tag "p", "para"
def_renamed_open_tag "select", "select_tag"
OPEN_TAGS = %w(a abbr address article aside b bdi body button code details dialog div dd dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header html i iframe label li main mark menuitem meter nav ol option pre progress rp rt ruby s section small span strong summary table tbody td textarea th thead time title tr u ul video wbr)
{% for tag in OPEN_TAGS %}
def_open_tag {{tag}}
{% end %}

def_open_tag "para", "p"
def_open_tag "select_tag", "select"
end
end
12 changes: 5 additions & 7 deletions src/water/tag/self_close_tag.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
module Water
class Water
module SelfCloseTag
macro def_self_close_tag(tag)
def {{tag.id}}(attributes = "")
@lines << "<{{tag.id}}#{strip_attributes(attributes)}>"
def {{tag.id}}
@lines << "<{{tag.id}}>"
@indents << @current_indent
end
end

macro def_renamed_self_close_tag(tag, alias_name)
def {{tag.id}}(attributes = "")
@lines << "<{{tag.id}}#{strip_attributes(attributes)}>"
def {{tag.id}}(attributes : String)
@lines << "<{{tag.id}} #{attributes}>"
@indents << @current_indent
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/water/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Water
class Water
VERSION = "0.2.0"
end

0 comments on commit bceb2f0

Please sign in to comment.