hypertext 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ed751f7bacf414010a50e266e61ad2124e9694b2f58e063c86c07aafdf6a7d0
4
+ data.tar.gz: f49d0b5f3ebed4ca55ca4ccb1d92eb01491f52069698ab8636da2cb6acae61a0
5
+ SHA512:
6
+ metadata.gz: 8f2fe8239fe6cf432af39a8b2382abe3097b1e9f0b092374cfadc588f5f595a1f690d42ae152c1a389e813d27d2481daa9c2f205ed4f3c10a0624a015d9d969e
7
+ data.tar.gz: f6da75a9e0c066a835e35abd2583b063b85c0d39d04aa0fe35036a7648625b93e22b0bb296beecd58fef3264f593975da6666ab1d9dfa7352bbb3cd800fad308
data/CHANGELOG ADDED
File without changes
data/CONTRIBUTING ADDED
@@ -0,0 +1,19 @@
1
+ This code tries to solve a particular problem with a very simple
2
+ implementation. We try to keep the code to a minimum while making
3
+ it as clear as possible. The design is very likely finished, and
4
+ if some feature is missing it is possible that it was left out on
5
+ purpose. That said, new usage patterns may arise, and when that
6
+ happens we are ready to adapt if necessary.
7
+
8
+ A good first step for contributing is to meet us on IRC and discuss
9
+ ideas. We spend a lot of time on #lesscode at freenode, always ready
10
+ to talk about code and simplicity. If connecting to IRC is not an
11
+ option, you can create an issue explaining the proposed change and
12
+ a use case. We pay a lot of attention to use cases, because our
13
+ goal is to keep the code base simple. Usually the result of a
14
+ conversation is the creation of a different tool.
15
+
16
+ Please don't start the conversation with a pull request. The code
17
+ should come at last, and even though it may help to convey an idea,
18
+ more often than not it draws the attention to a particular
19
+ implementation.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2021 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Hypertext
2
+ =========
3
+
4
+ Hypertext authoring
5
+
6
+ Description
7
+ -----------
8
+
9
+ Hypertext allows you to write HTML from Ruby.
10
+
11
+ Usage
12
+ -----
13
+
14
+ A basic example would look like this:
15
+
16
+ ```ruby
17
+ html = Hypertext.new do |h|
18
+ h.tag :div, class: "greeting" do |h|
19
+ h.tag :h1 do |h|
20
+ h.text "hello world"
21
+ end
22
+
23
+ h.tag :hr
24
+
25
+ h.tag :p do |h|
26
+ h.text "nice to meet you"
27
+ end
28
+ end
29
+ end
30
+
31
+ puts html.to_s
32
+
33
+ # <div class="greeting">
34
+ # <h1>
35
+ # hello world
36
+ # </h1>
37
+ # <hr />
38
+ # <p>
39
+ # nice to meet you
40
+ # </p>
41
+ # </div>
42
+ ```
43
+
44
+ Installation
45
+ ------------
46
+
47
+ ```
48
+ $ gem install hypertext
49
+ ```
data/hypertext.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "hypertext"
3
+ s.version = "0.0.1"
4
+ s.summary = "Hypertext authoring"
5
+ s.description = "Hypertext authoring with Ruby"
6
+ s.authors = ["Michel Martens"]
7
+ s.email = ["michel@soveran.com"]
8
+ s.homepage = "https://github.com/soveran/hypertext"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_development_dependency "cutest"
14
+ end
data/lib/hypertext.rb ADDED
@@ -0,0 +1,95 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (c) 2021 Michel Martens
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.
22
+
23
+ class Hypertext
24
+ ENTITIES = {
25
+ "'" => '&#39;',
26
+ '&' => '&amp;',
27
+ '"' => '&quot;',
28
+ '<' => '&lt;',
29
+ '>' => '&gt;',
30
+ }
31
+
32
+ def self.render(array, indent = " ", level = 0)
33
+ array.map do |element|
34
+ if Array === element
35
+ render(element, indent, level + 1)
36
+ else
37
+ sprintf "%s%s\n", indent * level, element
38
+ end
39
+ end.join
40
+ end
41
+
42
+ def self.escape(str)
43
+ str.gsub(/['&\"<>]/, ENTITIES)
44
+ end
45
+
46
+ def initialize
47
+ @dom = []
48
+
49
+ if block_given?
50
+ yield self
51
+ end
52
+ end
53
+
54
+ def tag(name, attributes = {}, &block)
55
+ atts = compile(attributes)
56
+
57
+ if block_given?
58
+ @dom.push "<#{name}#{atts}>"
59
+ @dom.push yield(self.class.new).to_a
60
+ @dom.push "</#{name}>"
61
+ else
62
+ @dom.push "<#{name}#{atts} />"
63
+ end
64
+ end
65
+
66
+ def text(value)
67
+ @dom.push(escape(value))
68
+ end
69
+
70
+ def to_a
71
+ @dom
72
+ end
73
+
74
+ def to_s(indent = " ")
75
+ self.class.render(@dom, indent)
76
+ end
77
+
78
+ private
79
+
80
+ def escape(str)
81
+ self.class.escape(str)
82
+ end
83
+
84
+ def compile(attributes)
85
+ attributes.map do |key, val|
86
+ case val
87
+ when false
88
+ when true
89
+ %[ #{key}]
90
+ else
91
+ %[ #{key}="#{escape(val.to_s)}"]
92
+ end
93
+ end.join
94
+ end
95
+ end
data/makefile ADDED
@@ -0,0 +1,4 @@
1
+ .PHONY: test
2
+
3
+ test:
4
+ cutest -r ./test/helper.rb ./test/*.rb
data/test/all.rb ADDED
@@ -0,0 +1,75 @@
1
+ scope "Hypertext" do
2
+ test "text" do
3
+ expected = "hello world\n"
4
+
5
+ observed = Hypertext.new
6
+ observed.text "hello world"
7
+
8
+ assert_equal expected, observed.to_s
9
+ end
10
+
11
+ test "self-closing tags" do
12
+ expected = "<br />\n"
13
+
14
+ observed = Hypertext.new
15
+ observed.tag :br
16
+
17
+ assert_equal expected, observed.to_s
18
+ end
19
+
20
+ test "opening and closing tags" do
21
+ expected = "<span>\n hello world\n</span>\n"
22
+
23
+ observed = Hypertext.new
24
+ observed.tag :span do |ht|
25
+ ht.text "hello world"
26
+ end
27
+
28
+ assert_equal expected, observed.to_s
29
+ end
30
+
31
+ test "nested tags" do
32
+ expected = "<div>\n <p>\n hello world\n </p>\n</div>\n"
33
+
34
+ observed = Hypertext.new
35
+ observed.tag :div do |ht|
36
+ ht.tag :p do |ht|
37
+ ht.text "hello world"
38
+ end
39
+ end
40
+
41
+ assert_equal expected, observed.to_s
42
+ end
43
+
44
+ test "passing a block to Hypertext.new" do
45
+ expected = "<br />\n"
46
+
47
+ observed = Hypertext.new do |ht|
48
+ ht.tag :br
49
+ end
50
+
51
+ assert_equal expected, observed.to_s
52
+ end
53
+
54
+ test "tags with attributes" do
55
+ expected = "<input type=\"text\" name=\"person[name]\" value=\"Foo\" />\n"
56
+
57
+ observed = Hypertext.new do |ht|
58
+ ht.tag :input, :type => "text", :name => "person[name]", :value => "Foo"
59
+ end
60
+
61
+ assert_equal expected, observed.to_s
62
+ end
63
+
64
+ test "custom indentation" do
65
+ expected = "<p>\n....hello world\n</p>\n"
66
+
67
+ observed = Hypertext.new do |ht|
68
+ ht.tag :p do |ht|
69
+ ht.text "hello world"
70
+ end
71
+ end
72
+
73
+ assert_equal expected, observed.to_s("....")
74
+ end
75
+ end
data/test/helper.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "../lib/hypertext"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hypertext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Hypertext authoring with Ruby
28
+ email:
29
+ - michel@soveran.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG
35
+ - CONTRIBUTING
36
+ - LICENSE
37
+ - README.md
38
+ - hypertext.gemspec
39
+ - lib/hypertext.rb
40
+ - makefile
41
+ - test/all.rb
42
+ - test/helper.rb
43
+ homepage: https://github.com/soveran/hypertext
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.0.3
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Hypertext authoring
66
+ test_files: []