yart 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +6 -2
- data/bin/console +1 -1
- data/lib/yart/parser.rb +131 -121
- data/lib/yart/version.rb +1 -1
- data/load.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03fbc3910c0db0766cd4ccdd88102c0e777c34d0b7485e9a93c6b67a1c5d9d4c
|
4
|
+
data.tar.gz: fcfa919687199006176319f750ba06b6953565a7500455b17eb16afaeae5387b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d75dd2df60e6c8754c09b302ebfb385cfbc1802f3c106d9bc8766639fd8dbbc9334e01d854eadca289be0821b70f36683bb992649efdec66797bf6e337b2d359
|
7
|
+
data.tar.gz: 55e9cdba62d6fb48bcd60b78577b6e598fb1f91e2670e68464c420aec99286698b0a5cbf2883fbd32c94d345980df4a62568fbb19d7f125069a918eb9d70c7df
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
*Yet Another Ruby Templater* turns plain Ruby into HTML making it fun to write webpages.
|
4
4
|
|
5
5
|
- YART provides an intuitive DSL that feels natural to use and removes the boiler plate from writing HTML
|
6
|
-
- YART has zero runtime dependencies and around
|
6
|
+
- YART has zero runtime dependencies and around 120 lines of code
|
7
7
|
- YART is fully unit tested
|
8
8
|
|
9
9
|
## Example Usage
|
@@ -112,7 +112,11 @@ Main points to note:
|
|
112
112
|
- Several attibute *values* can be rendered by passing an `Array` e.g. `p class: [:para, :italic]`. The values will be rendered space separated.
|
113
113
|
- Attribute *values* containing illegal characters (like quotes etc.) will be escaped in the rendered HTML.
|
114
114
|
- The attribute `close: true` is special and tells the parser to auto-close the element (because it's empty).
|
115
|
-
- Use the convenience methods `doctype`, `
|
115
|
+
- Use the convenience methods `doctype`, `javascript`, `stylesheet` and `br` as needed.
|
116
|
+
|
117
|
+
## Markdown
|
118
|
+
|
119
|
+
If you're creating a site with a lot of static textual content, check out the [static_site_builder](https://github.com/michaeltelford/static_site_builder) gem. It can render HTML sites built using Markdown and it fully supports YART; meaning you can write your text in Markdown and your forms etc in YART, never needing to touch HTML.
|
116
120
|
|
117
121
|
## Development
|
118
122
|
|
data/bin/console
CHANGED
@@ -40,7 +40,7 @@ def sample
|
|
40
40
|
div data_test_id: "String attribute values will be parsed as is" do
|
41
41
|
h2(data_x: :sub_heading) { "Symbol attribute keys/values will be kebab-cased" }
|
42
42
|
text { "Set the div's innerText, before and/or after its child elements" }
|
43
|
-
p(class: [
|
43
|
+
p(class: %i[content italic_text], id: :paragraph) do
|
44
44
|
"You can pass an array of attribute values and they will be space separated"
|
45
45
|
end
|
46
46
|
end
|
data/lib/yart/parser.rb
CHANGED
@@ -1,125 +1,135 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module YART
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
3
|
+
module YART
|
4
|
+
module Parser
|
5
|
+
CUSTOM_ATTRIBUTES = [:close].freeze
|
6
|
+
|
7
|
+
# Parses a block of Ruby, rendering and returning a HTML String.
|
8
|
+
def parse(&block)
|
9
|
+
raise "Must pass a block to parse" unless block_given?
|
10
|
+
|
11
|
+
@@yart_buffer = []
|
12
|
+
instance_eval(&block)
|
13
|
+
@@yart_buffer.join
|
14
|
+
end
|
15
|
+
|
16
|
+
# Allows elements to be called and rendered via a DSL.
|
17
|
+
def method_missing(m, *args, &block)
|
18
|
+
attributes = args.fetch(0, {})
|
19
|
+
|
20
|
+
element(m, **attributes, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Renders an element with the raw name (case insensitive).
|
24
|
+
def element(name, **attributes, &block)
|
25
|
+
render(name, attributes, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sets the `innerText` of the element being rendered.
|
29
|
+
def text(str = nil, &block)
|
30
|
+
raise "Must pass a String param or a block returning a String" unless str || block_given?
|
31
|
+
|
32
|
+
str ||= block.call
|
33
|
+
buffer(str)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Renders a <!DOCTYPE html> element, for convenience.
|
37
|
+
def doctype
|
38
|
+
element("!DOCTYPE", html: true, close: true)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Renders a JS <script src="..."> element, for convenience.
|
42
|
+
def javascript(src = nil, &block)
|
43
|
+
raise "Must pass a String param or a block returning a String" unless src || block_given?
|
44
|
+
|
45
|
+
src ||= block.call
|
46
|
+
element("script", src: src, close: true)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Renders a CSS <link href="..."> element, for convenience.
|
50
|
+
def stylesheet(href = nil, &block)
|
51
|
+
raise "Must pass a String param or a block returning a String" unless href || block_given?
|
52
|
+
|
53
|
+
href ||= block.call
|
54
|
+
element("link", href: href, rel: :stylesheet, type: "text/css", close: true)
|
55
|
+
end
|
56
|
+
|
57
|
+
def br(**attributes, &block)
|
58
|
+
attributes[:close] = true unless attributes[:close] == false
|
59
|
+
element("br", **attributes, &block)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Overrides Ruby's `p` method to render the element instead of printing.
|
63
|
+
def p(**attributes, &block)
|
64
|
+
element("p", **attributes, &block)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def buffer(str)
|
70
|
+
@@yart_buffer << str if str.is_a?(String)
|
71
|
+
end
|
72
|
+
|
73
|
+
def render(element, attributes, &block)
|
74
|
+
raise "Must pass attributes as a Hash" unless attributes.is_a?(Hash)
|
75
|
+
if block_given? && attributes.fetch(:close, false)
|
76
|
+
raise "Block and 'close: true' are mutually exclusive"
|
77
|
+
end
|
78
|
+
|
79
|
+
buffer(build_opening_tag(element, attributes))
|
80
|
+
buffer(instance_eval(&block)) if block_given?
|
81
|
+
buffer(build_closing_tag(element, attributes))
|
82
|
+
end
|
83
|
+
|
84
|
+
def build_opening_tag(element, attributes)
|
85
|
+
attributes_str = sanitise_attributes(attributes)
|
86
|
+
.reject { |k, _v| CUSTOM_ATTRIBUTES.include?(k) }
|
87
|
+
.map { |k, v| v == true ? k.to_s : "#{k}='#{v}'" }
|
88
|
+
.join(" ")
|
89
|
+
separator = attributes_str.empty? ? "" : " "
|
90
|
+
|
91
|
+
"<#{element}#{separator}#{attributes_str}>"
|
92
|
+
end
|
93
|
+
|
94
|
+
def build_closing_tag(element, attributes)
|
95
|
+
attributes[:close] ? "" : "</#{element}>"
|
96
|
+
end
|
97
|
+
|
98
|
+
def sanitise_attributes(attributes)
|
99
|
+
attributes.map do |k, v|
|
100
|
+
k = kebab_case(k)
|
101
|
+
v = if v.respond_to?(:map)
|
102
|
+
v.map { |v2| convert_attribute_value(v2) }.join(" ")
|
103
|
+
else
|
104
|
+
convert_attribute_value(v)
|
105
|
+
end
|
106
|
+
|
107
|
+
[k, v]
|
108
|
+
end.to_h
|
109
|
+
end
|
110
|
+
|
111
|
+
def convert_attribute_value(value)
|
112
|
+
value = kebab_case(value)
|
113
|
+
replace_illegal_chars(value)
|
114
|
+
end
|
115
|
+
|
116
|
+
def kebab_case(s)
|
117
|
+
return s unless s.is_a?(Symbol)
|
118
|
+
|
119
|
+
s
|
120
|
+
.to_s
|
121
|
+
.gsub("_", "-")
|
122
|
+
.to_sym
|
123
|
+
end
|
124
|
+
|
125
|
+
def replace_illegal_chars(s)
|
126
|
+
return s unless s.is_a?(String)
|
127
|
+
|
128
|
+
s
|
129
|
+
.gsub('"', """)
|
130
|
+
.gsub("'", "'")
|
131
|
+
.gsub("<", "<")
|
132
|
+
.gsub(">", ">")
|
133
|
+
end
|
124
134
|
end
|
125
135
|
end
|
data/lib/yart/version.rb
CHANGED
data/load.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Telford
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
Yet Another Ruby Templater (YART) turns plain Ruby into HTML making it fun to write webpages.
|