hir 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hir.rb +75 -0
- metadata +62 -0
data/lib/hir.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
class Hash
|
2
|
+
def to_html_attrs
|
3
|
+
map { |k, v| " #{k}='#{v}'" }.join
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class HIR
|
8
|
+
VERSION = "0.1.0"
|
9
|
+
|
10
|
+
module HTMLTags
|
11
|
+
|
12
|
+
def self.declare_tag(tagname)
|
13
|
+
define_method(tagname, ->(*args, &block) { tag(tagname, *args, &block) })
|
14
|
+
define_method("#{tagname}!", ->(*args, &block) { tag_sc(tagname, *args) })
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.declare_tags(*tagnames)
|
18
|
+
tagnames.each { |tagname| declare_tag(tagname) }
|
19
|
+
end
|
20
|
+
|
21
|
+
declare_tags *%w[
|
22
|
+
a abbr acronym address applet area article aside audio b base basefont
|
23
|
+
bdi bdo big blockquote body br button canvas caption center cite code
|
24
|
+
col colgroup command datalist dd del details dfn dir div dl dt em embed
|
25
|
+
fieldset figcaption figure font footer form frame frameset h1 head
|
26
|
+
header hgroup hr html i iframe img input ins keygen kbd label legend li
|
27
|
+
link map mark menu meta meter nav noframes noscript object ol optgroup
|
28
|
+
option output p param pre progress q rp rt ruby s samp script section
|
29
|
+
select small source span strike strong style sub summary sup table tbody
|
30
|
+
td textarea tfoot th thead time title tr track tt u ul var video wbr ]
|
31
|
+
|
32
|
+
def doctype!
|
33
|
+
handle_output "<!DOCTYPE html>\n"
|
34
|
+
end
|
35
|
+
|
36
|
+
def comment(content)
|
37
|
+
handle_output "<!-- #{content} -->"
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def tag(tagname, content = "", options = {}, &children)
|
43
|
+
handle_output "<#{tagname}#{options.to_html_attrs}>#{content}" \
|
44
|
+
"#{HIR.to_html(&children) if block_given?}</#{tagname}>"
|
45
|
+
end
|
46
|
+
|
47
|
+
# self-closing tag
|
48
|
+
def tag_sc(tagname, options = {})
|
49
|
+
handle_output "<#{tagname}#{options.to_html_attrs}/>"
|
50
|
+
end
|
51
|
+
|
52
|
+
def handle_output(output)
|
53
|
+
self.class.eql?(HIR) ? (@tags << output).join : output
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.to_html(&content)
|
59
|
+
HIR.new.instance_eval(&content).to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
include HTMLTags
|
63
|
+
|
64
|
+
def initialize
|
65
|
+
@tags = []
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_s
|
69
|
+
@tags.join
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def hir(&content)
|
74
|
+
HIR.to_html(&content)
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hir
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jack Willis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.9'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.9'
|
30
|
+
description: ! 'hir: HTML in Ruby'
|
31
|
+
email:
|
32
|
+
- jcwillis.school@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/hir.rb
|
38
|
+
homepage: http://github.com/jacksonwillis/hir
|
39
|
+
licenses: []
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.23
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Write HTML as Ruby blocks
|
62
|
+
test_files: []
|