html_writer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/LICENSE.md +21 -0
  2. data/README.md +31 -0
  3. data/lib/html_writer.rb +71 -0
  4. metadata +47 -0
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Marcus Ortiz, http://marcusortiz.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # html_writer
2
+
3
+ a small DSL for writing HTML
4
+
5
+ ## usage
6
+
7
+ ``` ruby
8
+ HtmlWriter.new.write do |html|
9
+ html.doctype 5
10
+ html.head do |head|
11
+ head.title 'foobar'
12
+ end
13
+ html.body do |body|
14
+ body.p 'hello, world'
15
+ end
16
+ end
17
+
18
+ ``` html
19
+ <!DOCTYPE html>
20
+ <html>
21
+ <head>
22
+ <title>foobar</title>
23
+ </head>
24
+ <body>
25
+ <p>hello, world</p>
26
+ </body>
27
+ </html>
28
+
29
+ ## todo
30
+
31
+ * support other doctypes besides html5
@@ -0,0 +1,71 @@
1
+ class HtmlWriter
2
+ VERSION = '0.1.0'
3
+
4
+ VALID_TAGS = {
5
+ 5 => [
6
+ :a, :abbr, :address, :area, :article, :aside, :audio,
7
+ :b, :base, :bdi, :bdo, :big, :blockquote, :body, :br, :button,
8
+ :canvas, :caption, :cite, :code, :col, :colgroup, :command,
9
+ :datalist, :dd, :del, :details, :dfn, :div, :dl, :dt,
10
+ :em, :embed,
11
+ :fieldset, :figcaption, :figure, :footer, :form,
12
+ :h1, :h2, :h3, :h4, :h5, :h6, :head, :header, :hr,
13
+ :i, :iframe, :img, :input, :ins,
14
+ :keygen, :kbd,
15
+ :label, :legend, :li, :link,
16
+ :map, :mark, :menu, :meta, :meter,
17
+ :nav, :noscript,
18
+ :object, :ol, :optgroup, :option, :output,
19
+ :p, :param, :pre, :progress,
20
+ :q,
21
+ :rp, :rt, :ruby,
22
+ :s, :samp, :script, :section, :select, :small, :source, :span, :strong,
23
+ :style, :sub, :summary, :sup,
24
+ :table, :tbody, :td, :textarea, :tfoot, :th, :thead, :time, :title, :tr,
25
+ :track,
26
+ :u, :ul,
27
+ :var, :video,
28
+ :wbr
29
+ ]
30
+ }
31
+
32
+ class InvalidTagError < StandardError; end
33
+
34
+ def initialize
35
+ @html = ''
36
+ end
37
+
38
+ def write
39
+ @html << '<html>'
40
+ yield self
41
+ @html << '</html>'
42
+ end
43
+
44
+ def doctype(doctype)
45
+ @doctype = doctype
46
+ case @doctype
47
+ when 5
48
+ @html = '<!DOCTYPE html>' + @html
49
+ end
50
+ end
51
+
52
+ def method_missing(name, *args, &block)
53
+ unless valid_tag?(name)
54
+ raise InvalidTagError, "`<#{name}>` is not a valid HTML #{@doctype} tag."
55
+ end
56
+
57
+ @html << "<#{name}>"
58
+ if block_given?
59
+ yield self
60
+ else
61
+ @html << args.join('')
62
+ end
63
+ @html << "</#{name}>"
64
+ end
65
+
66
+ private
67
+
68
+ def valid_tag?(name)
69
+ VALID_TAGS[@doctype].include? name.to_sym
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_writer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcus Ortiz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: a small DSL for writing HTML
15
+ email: mportiz08@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - LICENSE.md
22
+ - lib/html_writer.rb
23
+ homepage: https://github.com/mportiz08/html_writer
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.11
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: a small DSL for writing HTML
47
+ test_files: []