html_rb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/html_rb.rb +83 -0
  3. data/lib/html_rb/constants.rb +158 -0
  4. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 907ff51c68c17cdcf035eacaa2ef82b3a2a3050b
4
+ data.tar.gz: d70f50da58bc048b61ebf02873028dc3794f0a64
5
+ SHA512:
6
+ metadata.gz: 22c13f0bbdc20394782c704e59a8d92dde4d0a7361f4d8eaa83109feb21d2ce905578b2ac7c9dd0308378881f512a6912f609c24f761fc756f6d691bb116a6bc
7
+ data.tar.gz: a3098a8f2623954a6e81a1aece656ee22dd598afa76e31c6b404ce4028e958a2fa82148bf042525b1d7ec2867b8799d3ea0d2ea45186e5836e6d1d22b654b3a6
data/lib/html_rb.rb ADDED
@@ -0,0 +1,83 @@
1
+ require 'html_rb/constants'
2
+
3
+ module HtmlRB
4
+ VERSION = "1.0.0".freeze
5
+
6
+ Error = Class.new(StandardError)
7
+
8
+ class Tag
9
+ def initialize(name=nil,content=nil,**attrs,&block)
10
+ @strings = []
11
+ build name, content, **attrs, &block
12
+ end
13
+
14
+ def to_s
15
+ @strings.join
16
+ end
17
+
18
+ private
19
+ def build(name=nil,content=nil,**attrs,&block)
20
+ void = HtmlRB::VOID_ELEMENTS.include?(name)
21
+ raise HtmlRB::Error, "May not provide both content and block" if content && block_given?
22
+ raise HtmlRB::Error, "Void elements cannot enclose content" if void && (content || block_given?)
23
+
24
+ @strings << "<#{[name,attribute_string(attrs)].join(" ").strip}>" if name
25
+ unless void
26
+ @strings << content if content
27
+ instance_eval(&block) if block_given?
28
+ @strings << "</#{name}>" if name
29
+ end
30
+ end
31
+
32
+ # Define all the tag methods
33
+ (HtmlRB::STD_ELEMENTS + HtmlRB::VOID_ELEMENTS).each do |el|
34
+ define_method(el) do |content=nil,**attrs,&block|
35
+ build el, content, **attrs, &block
36
+ end
37
+ end
38
+
39
+ # Used for Text Nodes
40
+ def text(content)
41
+ build nil, content
42
+ end
43
+
44
+ # ATTRIBUTE HANDLING
45
+ def attribute_string(hash={})
46
+ hash.delete_if{|k,v| v.nil? || v == "" }
47
+ .map{|k,v| attribute_pair(k,v) }
48
+ .compact
49
+ .join(" ")
50
+ end
51
+
52
+ def attribute_pair(k,v)
53
+ if HtmlRB::BOOL_ATTRS.include?(k)
54
+ attribute_key(k) if v
55
+ else
56
+ %Q|#{attribute_key(k)}="#{v}"|
57
+ end
58
+ end
59
+
60
+ def attribute_key(k)
61
+ str = k.to_s
62
+ if str.start_with? "!"
63
+ str.slice!(0)
64
+ else
65
+ str.gsub!("_","-")
66
+ end
67
+ str
68
+ end
69
+ end
70
+
71
+ def html(content=nil,document:false,doctype:"html",**attrs,&block)
72
+ name = nil
73
+ dt = ""
74
+ if document
75
+ name = :html
76
+ dt = "<!DOCTYPE #{doctype}>" if doctype
77
+ end
78
+ dt + Tag.new(name, content, **attrs, &block).to_s
79
+ end
80
+
81
+ alias_method :markup, :html
82
+ module_function :markup
83
+ end
@@ -0,0 +1,158 @@
1
+ module HtmlRB
2
+ STD_ELEMENTS=[
3
+ :a,
4
+ :abbr,
5
+ :address,
6
+ :article,
7
+ :aside,
8
+ :audio,
9
+ :b,
10
+ :bdi,
11
+ :bdo,
12
+ :blockquote,
13
+ :body,
14
+ :button,
15
+ :canvas,
16
+ :caption,
17
+ :cite,
18
+ :code,
19
+ :colgroup,
20
+ :datalist,
21
+ :dd,
22
+ :del,
23
+ :details,
24
+ :dfn,
25
+ :dialog,
26
+ :div,
27
+ :dl,
28
+ :dt,
29
+ :em,
30
+ :fieldset,
31
+ :figcaption,
32
+ :figure,
33
+ :footer,
34
+ :form,
35
+ :h1,
36
+ :head,
37
+ :header,
38
+ :html,
39
+ :i,
40
+ :iframe,
41
+ :ins,
42
+ :kbd,
43
+ :label,
44
+ :legend,
45
+ :li,
46
+ :main,
47
+ :map,
48
+ :mark,
49
+ :menu,
50
+ :menuitem,
51
+ :meter,
52
+ :nav,
53
+ :noscript,
54
+ :object,
55
+ :ol,
56
+ :optgroup,
57
+ :option,
58
+ :output,
59
+ :p,
60
+ :pre,
61
+ :progress,
62
+ :q,
63
+ :rp,
64
+ :rt,
65
+ :ruby,
66
+ :s,
67
+ :samp,
68
+ :script,
69
+ :section,
70
+ :select,
71
+ :small,
72
+ :span,
73
+ :strong,
74
+ :style,
75
+ :sub,
76
+ :summary,
77
+ :sup,
78
+ :table,
79
+ :tbody,
80
+ :td,
81
+ :textarea,
82
+ :tfoot,
83
+ :th,
84
+ :thead,
85
+ :time,
86
+ :title,
87
+ :tr,
88
+ :u,
89
+ :ul,
90
+ :var,
91
+ :video,
92
+ ].to_set.freeze
93
+
94
+ VOID_ELEMENTS=[
95
+ :area,
96
+ :base,
97
+ :br,
98
+ :col,
99
+ :embed,
100
+ :hr,
101
+ :img,
102
+ :input,
103
+ :keygen,
104
+ :link,
105
+ :meta,
106
+ :param,
107
+ :source,
108
+ :track,
109
+ :wbr,
110
+ ].to_set.freeze
111
+
112
+ BOOL_ATTRS=[
113
+ :allowfullscreen,
114
+ :async,
115
+ :autofocus,
116
+ :autoplay,
117
+ :checked,
118
+ :compact,
119
+ :controls,
120
+ :declare,
121
+ :default,
122
+ :defaultchecked,
123
+ :defaultmuted,
124
+ :defaultselected,
125
+ :defer,
126
+ :disabled,
127
+ :draggable,
128
+ :enabled,
129
+ :formnovalidate,
130
+ :hidden,
131
+ :indeterminate,
132
+ :inert,
133
+ :ismap,
134
+ :itemscope,
135
+ :loop,
136
+ :multiple,
137
+ :muted,
138
+ :nohref,
139
+ :noresize,
140
+ :noshade,
141
+ :novalidate,
142
+ :nowrap,
143
+ :open,
144
+ :pauseonexit,
145
+ :readonly,
146
+ :required,
147
+ :reversed,
148
+ :scoped,
149
+ :seamless,
150
+ :selected,
151
+ :sortable,
152
+ :spellcheck,
153
+ :translate,
154
+ :truespeed,
155
+ :typemustmatch,
156
+ :visible,
157
+ ].to_set.freeze
158
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Burleson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A beautifully simple DSL for writing HTML in ruby. Check out the readme
14
+ at github.com/burlesona/html_rb for detailed examples, or see the tests.
15
+ email: burlesona@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/html_rb.rb
21
+ - lib/html_rb/constants.rb
22
+ homepage: http://github.com/burlesona/html
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '2.0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: A beautifully simple DSL for writing HTML in ruby
46
+ test_files: []
47
+ has_rdoc: