romantic_text 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21d6578acdda679ea72dd1ec7eb1c577c49afee17896496a90a884ae47058119
4
+ data.tar.gz: 457453820720e812ccecd82a6a4454d9419203b0161bdefb38259c622c5c8b25
5
+ SHA512:
6
+ metadata.gz: a71c59dc64fba8d4576d8702eba1e948ed917588771fdf51fddba6e83dbcc71abccd5efc34a18af3b01795adb733f745622a7fad0fb4fd6bfbccfb763c93032a
7
+ data.tar.gz: cba936040efe1ec973edf74575d8447d4990e2c4148410f28100621cac3aff72d6e351c16ca8f2bd381b029fcaa1384b70c20ada8eb269a40c9a8b398cfbd9a6
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ AllCops:
2
+ Exclude:
3
+ - tmp/**/*
4
+ - vendor/**/*
5
+ TargetRubyVersion: 2.5
6
+ DisplayCopNames: true
7
+
8
+ # no romantic :(
9
+ Style/Alias:
10
+ Enabled: false
11
+
12
+ # no document
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ # incompatible DSL
17
+ Layout/EmptyLinesAroundArguments:
18
+ Enabled: false
19
+
20
+ # incompatible DSL
21
+ Metrics/BlockLength:
22
+ Enabled: false
23
+
24
+ # incompatible DSL
25
+ Metrics/ClassLength:
26
+ Exclude:
27
+ - test/**/*
28
+
29
+ # my preference :)
30
+ Metrics/LineLength:
31
+ Max: 120
32
+
33
+ # my preference :(
34
+ Metrics/MethodLength:
35
+ Max: 20
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in romantic_text.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ romantic_text (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ jaro_winkler (1.5.3)
11
+ parallel (1.17.0)
12
+ parser (2.6.3.0)
13
+ ast (~> 2.4.0)
14
+ power_assert (1.1.4)
15
+ rainbow (3.0.0)
16
+ rake (10.5.0)
17
+ rr (1.2.1)
18
+ rubocop (0.72.0)
19
+ jaro_winkler (~> 1.5.1)
20
+ parallel (~> 1.10)
21
+ parser (>= 2.6)
22
+ rainbow (>= 2.2.2, < 4.0)
23
+ ruby-progressbar (~> 1.7)
24
+ unicode-display_width (>= 1.4.0, < 1.7)
25
+ ruby-progressbar (1.10.1)
26
+ test-unit (3.3.3)
27
+ power_assert
28
+ test-unit-rr (1.0.5)
29
+ rr (>= 1.1.1)
30
+ test-unit (>= 2.5.2)
31
+ unicode-display_width (1.6.0)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 1.17)
38
+ rake (~> 10.0)
39
+ romantic_text!
40
+ rubocop (= 0.72.0)
41
+ test-unit (~> 3.3)
42
+ test-unit-rr (~> 1.0)
43
+
44
+ BUNDLED WITH
45
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 ru_shalm
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,157 @@
1
+ # RomanticText
2
+
3
+ > A romantic DSL for writing HTML.
4
+
5
+ <!-- TOC -->
6
+
7
+ - [RomanticText](#romantictext)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Simply](#simply)
11
+ - [Attributes](#attributes)
12
+ - [Shorthand id / class](#shorthand-id--class)
13
+ - [Escape HTML](#escape-html)
14
+ - [Use `h` method](#use-h-method)
15
+ - [Motivation](#motivation)
16
+ - [Contributing](#contributing)
17
+ - [License](#license)
18
+
19
+ <!-- /TOC -->
20
+
21
+ ## Installation
22
+
23
+ ```ruby
24
+ gem 'romantic_text'
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'romantic_text'
31
+
32
+ html = RomanticText.markup do
33
+ `#wrapper`['data-version': '1.0'] do
34
+ `h1` << 'Welcome to RomanticText'
35
+ `main.contents`[] do
36
+ `p`[] do
37
+ _ 'This is '
38
+ `strong`[class: 'red'] << 'a romantic DSL'
39
+ _ ' for writing HTML.'
40
+ end
41
+ end
42
+ dangerous_raw_html '<div class="footer">Thanks.</div>'
43
+ end
44
+ end
45
+
46
+ puts html.to_s
47
+ # <div id="wrapper" data-version='1.0'>
48
+ # <h1>Welcome to RomanticText</h1>
49
+ # <main class="contents">
50
+ # <p>This is <strong class="red">a romantic DSL</strong> for writing HTML.</p>
51
+ # </main>
52
+ # <div class="footer">Thanks.</div>
53
+ # </div>
54
+ ```
55
+
56
+ ### Simply
57
+
58
+ ```ruby
59
+ html = RomanticText.markup do
60
+ `div` << 'Hello'
61
+ end
62
+ html.to_s # => <div>Hello</div>
63
+
64
+ html = RomanticText.markup do
65
+ `div`[] do
66
+ _ 'Hello'
67
+ end
68
+ end
69
+ html.to_s # => <div>Hello</div>
70
+ ```
71
+
72
+ ### Attributes
73
+
74
+ ```ruby
75
+ html = RomanticText.markup do
76
+ `a`[class: 'my-class', href: 'https://example.com'] do
77
+ _ 'link'
78
+ end
79
+ end
80
+ html.to_s # => <a class="my-class" href="https://example.com">link</a>
81
+ ```
82
+
83
+ ### Shorthand id / class
84
+
85
+ ```ruby
86
+ html = RomanticText.markup do
87
+ `#my-id` << 'text'
88
+ `p.my-class1.my-class2` << 'text'
89
+ end
90
+ html.to_s # => <div id="my-id">text</div><p class="my-class1 my-class2">text</p>
91
+ ```
92
+
93
+ ### Escape HTML
94
+
95
+ ```ruby
96
+ # safe :)
97
+ html = RomanticText.markup do
98
+ `div`[] do
99
+ _ '<script>alert(1)</script>'
100
+ end
101
+ end
102
+ html.to_s # => <div>&lt;script&gt;alert(1)&lt;/script&gt;</div>
103
+
104
+ # danger :(
105
+ html = RomanticText.markup do
106
+ `div`[] do
107
+ dangerous_raw_html '<script>alert(1)</script>'
108
+ end
109
+ end
110
+ html.to_s # => <div><script>alert(1)</script></div>
111
+ ```
112
+
113
+ ### Use `h` method
114
+
115
+ ```ruby
116
+ html1 = RomanticText.markup do
117
+ `p`[class: 'red'] do
118
+ `strong` << 'Hello'
119
+ end
120
+ end
121
+
122
+ html2 = RomanticText.markup do
123
+ h('p', class: 'red') do
124
+ h('strong') << 'Hello'
125
+ end
126
+ end
127
+
128
+ html1.to_s == html2.to_s # => true
129
+ ```
130
+
131
+ ## Motivation
132
+
133
+ I think backquote method is very very exciting :laughing:
134
+
135
+ ```ruby
136
+ class Hoge
137
+ def `(arg)
138
+ arg.upcase
139
+ end
140
+
141
+ def custom_eval(&block)
142
+ instance_eval(&block)
143
+ end
144
+ end
145
+
146
+ Hoge.new.custom_eval { `hello` } # => HELLO
147
+ ```
148
+
149
+ It can be used like [Tagged templates in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals).
150
+
151
+ ## Contributing
152
+
153
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rutan/romantic_text.
154
+
155
+ ## License
156
+
157
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'romantic_text'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RomanticText
4
+ class Element
5
+ def initialize(subject = nil, attributes = {})
6
+ @name = pick_element_name(subject)
7
+ @attributes = pick_id_and_classes(subject)
8
+ merge_attributes!(attributes)
9
+ @children = []
10
+ end
11
+
12
+ def `(name)
13
+ Element.new(name).tap { |e| @children.push(e) }
14
+ end
15
+
16
+ def h(name, attributes = {}, &block)
17
+ public_send(:`, name).render(attributes, &block)
18
+ end
19
+
20
+ def <<(str)
21
+ @children.push Utils.escape(str)
22
+ end
23
+ alias_method :text, :<<
24
+ alias_method :_, :<<
25
+
26
+ def dangerous_raw_html(str)
27
+ @children.push str.to_s
28
+ end
29
+
30
+ def render(attributes = {}, &block)
31
+ merge_attributes!(attributes)
32
+ instance_eval(&block) if block_given?
33
+ self
34
+ end
35
+ alias_method :[], :render
36
+
37
+ def to_s
38
+ inner = @children.map(&:to_s).join('')
39
+ return inner if @name.nil?
40
+
41
+ generate_tag(@name, @attributes, inner)
42
+ end
43
+
44
+ private
45
+
46
+ def pick_element_name(subject)
47
+ return nil if subject.nil?
48
+
49
+ name = subject.gsub(/(?:#|\.).*\z/, '')
50
+ name.empty? ? 'div' : name
51
+ end
52
+
53
+ def pick_id_and_classes(subject)
54
+ return {} if subject.nil?
55
+
56
+ group = subject.scan(/(?:#|\.)[^#\.]+/).group_by { |n| n[0] }
57
+ {
58
+ id: group['#'] ? group['#'].first.to_s.delete('#') : nil,
59
+ class: group['.'] ? group['.'].map { |s| s.delete('.') } : nil
60
+ }
61
+ end
62
+
63
+ def generate_tag(element_name, attributes, inner_html)
64
+ name = Utils.escape(element_name)
65
+ attr_text = generate_attributes(attributes)
66
+ "<#{name}#{" #{attr_text}" unless attr_text.empty?}>#{inner_html}</#{name}>"
67
+ end
68
+
69
+ def generate_attributes(attributes)
70
+ attributes.map do |key, value|
71
+ next if value.nil?
72
+ next if value.is_a?(Array) && value.empty?
73
+
74
+ "#{Utils.escape(key)}=\"#{Utils.escape((value.is_a?(Array) ? value.join(' ') : value))}\""
75
+ end.compact.join(' ')
76
+ end
77
+
78
+ def merge_attributes!(attributes)
79
+ attributes.each do |key, value|
80
+ case @attributes[key]
81
+ when Array
82
+ if value.is_a?(Array)
83
+ @attributes[key].concat!(value)
84
+ else
85
+ @attributes[key].push value
86
+ end
87
+ else
88
+ @attributes[key] = value.is_a?(Array) ? value : value.to_s
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi/escape'
4
+
5
+ module RomanticText
6
+ module Utils
7
+ class << self
8
+ def html_safe?(text)
9
+ text.respond_to?(:html_safe?) ? text.html_safe? : false
10
+ end
11
+
12
+ def escape(text)
13
+ html_safe?(text) ? text.to_s : CGI.escapeHTML(text.to_s)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RomanticText
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'romantic_text/version'
4
+ require 'romantic_text/utils'
5
+ require 'romantic_text/element'
6
+
7
+ module RomanticText
8
+ class << self
9
+ def markup(&block)
10
+ Element.new.render(&block)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'romantic_text/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'romantic_text'
9
+ spec.version = RomanticText::VERSION
10
+ spec.authors = ['ru_shalm']
11
+ spec.email = ['ru_shalm@hazimu.com']
12
+
13
+ spec.summary = 'A romantic DSL for writing HTML.'
14
+ spec.homepage = 'https://github.com/rutan/romantic_text'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.17'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rubocop', '0.72.0'
27
+ spec.add_development_dependency 'test-unit', '~> 3.3'
28
+ spec.add_development_dependency 'test-unit-rr', '~> 1.0'
29
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: romantic_text
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ru_shalm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.72.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.72.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit-rr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ description:
84
+ email:
85
+ - ru_shalm@hazimu.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rubocop.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/romantic_text.rb
100
+ - lib/romantic_text/element.rb
101
+ - lib/romantic_text/utils.rb
102
+ - lib/romantic_text/version.rb
103
+ - romantic_text.gemspec
104
+ homepage: https://github.com/rutan/romantic_text
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.0.3
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: A romantic DSL for writing HTML.
127
+ test_files: []