html_gen 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +54 -0
- data/.rubocop_todo.yml +74 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +25 -0
- data/Rakefile +18 -15
- data/VERSION +1 -1
- data/html_gen.gemspec +11 -3
- data/lib/html_gen/element.rb +49 -48
- data/lib/html_gen/parser.rb +21 -23
- data/lib/html_gen/text_ele.rb +23 -22
- data/lib/html_gen.rb +2 -2
- data/shippable.yml +2 -1
- data/spec/html_gen_parser_spec.rb +1 -1
- data/spec/html_gen_spec.rb +3 -3
- data/spec/spec_helper.rb +4 -4
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ca7ca82d9a45a1dc1208d8635acfa91f4a3d950
|
4
|
+
data.tar.gz: 16ffb22120f4dc9eae6e81f265bd27ecc00694c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67e9f9a315d26e6c85c938e7ce6eba41855592c5c4b48d8d17b163bb6cbcf1eb6010974e32e1d1a35e458733e7d59e8418dd4b0c0e8bae88d3ef01411a618940
|
7
|
+
data.tar.gz: 540ea4bb36340410039276fef5251c0edd83ba7ea545a8294655a0ae32cf344f6a9cbcccefc33575faea5c89c20bbb032e4b5b0a04971eff49a98839206fb134
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
DisplayCopNames: true
|
5
|
+
DisplayStyleGuide: true
|
6
|
+
Exclude:
|
7
|
+
- db/schema.rb
|
8
|
+
- spec/dummy/db/schema.rb
|
9
|
+
- html_gen.gemspec
|
10
|
+
|
11
|
+
# https://github.com/AtomLinter/linter-rubocop/issues/2
|
12
|
+
Style/FileName:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/ClassLength:
|
16
|
+
Max: 250
|
17
|
+
|
18
|
+
Style/AccessModifierIndentation:
|
19
|
+
EnforcedStyle: outdent
|
20
|
+
|
21
|
+
Style/ClassAndModuleChildren:
|
22
|
+
EnforcedStyle: compact
|
23
|
+
|
24
|
+
Style/Documentation:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/EmptyLines:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/StringLiterals:
|
31
|
+
EnforcedStyle: double_quotes
|
32
|
+
|
33
|
+
Style/StringLiteralsInInterpolation:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Style/NilComparison:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/SignalException:
|
40
|
+
EnforcedStyle: only_raise
|
41
|
+
|
42
|
+
Style/MultilineOperationIndentation:
|
43
|
+
EnforcedStyle: indented
|
44
|
+
|
45
|
+
Style/SpaceInsideHashLiteralBraces:
|
46
|
+
EnforcedStyle: no_space
|
47
|
+
|
48
|
+
Style/TrivialAccessors:
|
49
|
+
ExactNameMatch: true
|
50
|
+
Enabled: true
|
51
|
+
|
52
|
+
# Disabled on purpose: https://github.com/bbatsov/rubocop/issues/1758
|
53
|
+
Style/ClosingParenthesisIndentation:
|
54
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2016-01-20 15:14:18 +0100 using RuboCop version 0.36.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 6
|
10
|
+
# Configuration parameters: AllowSafeAssignment.
|
11
|
+
Lint/AssignmentInCondition:
|
12
|
+
Exclude:
|
13
|
+
- 'lib/html_gen/parser.rb'
|
14
|
+
|
15
|
+
# Offense count: 3
|
16
|
+
# Cop supports --auto-correct.
|
17
|
+
# Configuration parameters: AlignWith, SupportedStyles, AutoCorrect.
|
18
|
+
# SupportedStyles: keyword, variable, start_of_line
|
19
|
+
Lint/EndAlignment:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
# Offense count: 1
|
23
|
+
Lint/UselessAssignment:
|
24
|
+
Exclude:
|
25
|
+
- 'lib/html_gen/element.rb'
|
26
|
+
|
27
|
+
# Offense count: 3
|
28
|
+
Metrics/AbcSize:
|
29
|
+
Max: 82
|
30
|
+
|
31
|
+
# Offense count: 3
|
32
|
+
Metrics/CyclomaticComplexity:
|
33
|
+
Max: 35
|
34
|
+
|
35
|
+
# Offense count: 3
|
36
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
37
|
+
# URISchemes: http, https
|
38
|
+
Metrics/LineLength:
|
39
|
+
Max: 189
|
40
|
+
|
41
|
+
# Offense count: 1
|
42
|
+
# Configuration parameters: CountComments.
|
43
|
+
Metrics/MethodLength:
|
44
|
+
Max: 72
|
45
|
+
|
46
|
+
# Offense count: 5
|
47
|
+
Metrics/PerceivedComplexity:
|
48
|
+
Max: 41
|
49
|
+
|
50
|
+
# Offense count: 6
|
51
|
+
# Configuration parameters: MinBodyLength.
|
52
|
+
Style/GuardClause:
|
53
|
+
Exclude:
|
54
|
+
- 'lib/html_gen/element.rb'
|
55
|
+
- 'lib/html_gen/parser.rb'
|
56
|
+
|
57
|
+
# Offense count: 5
|
58
|
+
Style/IdenticalConditionalBranches:
|
59
|
+
Exclude:
|
60
|
+
- 'lib/html_gen/element.rb'
|
61
|
+
- 'lib/html_gen/parser.rb'
|
62
|
+
|
63
|
+
# Offense count: 1
|
64
|
+
Style/IfInsideElse:
|
65
|
+
Exclude:
|
66
|
+
- 'lib/html_gen/parser.rb'
|
67
|
+
|
68
|
+
# Offense count: 3
|
69
|
+
# Cop supports --auto-correct.
|
70
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
|
71
|
+
# SupportedStyles: slashes, percent_r, mixed
|
72
|
+
Style/RegexpLiteral:
|
73
|
+
Exclude:
|
74
|
+
- 'lib/html_gen/parser.rb'
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,17 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/kaspernj/best_practice_project.git
|
3
|
+
revision: 85edcdab3e63de1374de10f01fcf4a621099aae2
|
4
|
+
specs:
|
5
|
+
best_practice_project (0.0.8)
|
6
|
+
auto_autoloader
|
7
|
+
|
1
8
|
GEM
|
2
9
|
remote: http://rubygems.org/
|
3
10
|
specs:
|
4
11
|
addressable (2.3.7)
|
12
|
+
ast (2.2.0)
|
13
|
+
auto_autoloader (0.0.4)
|
14
|
+
string-cases
|
5
15
|
builder (3.2.2)
|
6
16
|
codeclimate-test-reporter (0.4.6)
|
7
17
|
simplecov (>= 0.7.1, < 1.0.0)
|
@@ -40,7 +50,11 @@ GEM
|
|
40
50
|
multi_json (~> 1.3)
|
41
51
|
multi_xml (~> 0.5)
|
42
52
|
rack (~> 1.2)
|
53
|
+
parser (2.3.0.1)
|
54
|
+
ast (~> 2.2)
|
55
|
+
powerpack (0.1.1)
|
43
56
|
rack (1.6.0)
|
57
|
+
rainbow (2.0.0)
|
44
58
|
rake (10.4.2)
|
45
59
|
rdoc (3.12.2)
|
46
60
|
json (~> 1.4)
|
@@ -52,6 +66,12 @@ GEM
|
|
52
66
|
rspec-expectations (2.8.0)
|
53
67
|
diff-lcs (~> 1.1.2)
|
54
68
|
rspec-mocks (2.8.0)
|
69
|
+
rubocop (0.36.0)
|
70
|
+
parser (>= 2.3.0.0, < 3.0)
|
71
|
+
powerpack (~> 0.1)
|
72
|
+
rainbow (>= 1.99.1, < 3.0)
|
73
|
+
ruby-progressbar (~> 1.7)
|
74
|
+
ruby-progressbar (1.7.5)
|
55
75
|
simplecov (0.9.1)
|
56
76
|
docile (~> 1.1.0)
|
57
77
|
multi_json (~> 1.0)
|
@@ -63,9 +83,14 @@ PLATFORMS
|
|
63
83
|
ruby
|
64
84
|
|
65
85
|
DEPENDENCIES
|
86
|
+
best_practice_project!
|
66
87
|
bundler (>= 1.0.0)
|
67
88
|
codeclimate-test-reporter
|
68
89
|
jeweler (~> 1.8.4)
|
69
90
|
rdoc (~> 3.12)
|
70
91
|
rspec (~> 2.8.0)
|
92
|
+
rubocop
|
71
93
|
string-cases
|
94
|
+
|
95
|
+
BUNDLED WITH
|
96
|
+
1.11.2
|
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "rubygems"
|
4
|
+
require "bundler"
|
5
5
|
begin
|
6
6
|
Bundler.setup(:default, :development)
|
7
7
|
rescue Bundler::BundlerError => e
|
@@ -9,41 +9,44 @@ rescue Bundler::BundlerError => e
|
|
9
9
|
$stderr.puts "Run `bundle install` to install missing gems"
|
10
10
|
exit e.status_code
|
11
11
|
end
|
12
|
-
require
|
12
|
+
require "rake"
|
13
13
|
|
14
|
-
require
|
14
|
+
require "jeweler"
|
15
15
|
Jeweler::Tasks.new do |gem|
|
16
16
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
17
|
gem.name = "html_gen"
|
18
18
|
gem.homepage = "http://github.com/kaspernj/html_gen"
|
19
19
|
gem.license = "MIT"
|
20
|
-
gem.summary = %
|
21
|
-
gem.description = %
|
20
|
+
gem.summary = %(A small framework for generating HTML.)
|
21
|
+
gem.description = %(A small framework for generating HTML.)
|
22
22
|
gem.email = "k@spernj.org"
|
23
23
|
gem.authors = ["Kasper Johansen"]
|
24
24
|
# dependencies defined in Gemfile
|
25
25
|
end
|
26
26
|
Jeweler::RubygemsDotOrgTasks.new
|
27
27
|
|
28
|
-
require
|
29
|
-
require
|
28
|
+
require "rspec/core"
|
29
|
+
require "rspec/core/rake_task"
|
30
30
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
-
spec.pattern = FileList[
|
31
|
+
spec.pattern = FileList["spec/**/*_spec.rb"]
|
32
32
|
end
|
33
33
|
|
34
34
|
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
-
spec.pattern =
|
35
|
+
spec.pattern = "spec/**/*_spec.rb"
|
36
36
|
spec.rcov = true
|
37
37
|
end
|
38
38
|
|
39
39
|
task default: :spec
|
40
40
|
|
41
|
-
require
|
41
|
+
require "rdoc/task"
|
42
42
|
Rake::RDocTask.new do |rdoc|
|
43
|
-
version = File.exist?(
|
43
|
+
version = File.exist?("VERSION") ? File.read("VERSION") : ""
|
44
44
|
|
45
|
-
rdoc.rdoc_dir =
|
45
|
+
rdoc.rdoc_dir = "rdoc"
|
46
46
|
rdoc.title = "html_gen #{version}"
|
47
|
-
rdoc.rdoc_files.include(
|
48
|
-
rdoc.rdoc_files.include(
|
47
|
+
rdoc.rdoc_files.include("README*")
|
48
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
49
49
|
end
|
50
|
+
|
51
|
+
require "best_practice_project"
|
52
|
+
BestPracticeProject.load_tasks
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.8
|
data/html_gen.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: html_gen 0.0.
|
5
|
+
# stub: html_gen 0.0.8 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "html_gen"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.8"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Kasper Johansen"]
|
14
|
-
s.date = "2016-01-
|
14
|
+
s.date = "2016-01-20"
|
15
15
|
s.description = "A small framework for generating HTML."
|
16
16
|
s.email = "k@spernj.org"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -21,6 +21,8 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.files = [
|
22
22
|
".document",
|
23
23
|
".rspec",
|
24
|
+
".rubocop.yml",
|
25
|
+
".rubocop_todo.yml",
|
24
26
|
"Gemfile",
|
25
27
|
"Gemfile.lock",
|
26
28
|
"LICENSE.txt",
|
@@ -51,12 +53,16 @@ Gem::Specification.new do |s|
|
|
51
53
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
52
54
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
53
55
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
56
|
+
s.add_development_dependency(%q<best_practice_project>, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<rubocop>, [">= 0"])
|
54
58
|
else
|
55
59
|
s.add_dependency(%q<string-cases>, [">= 0"])
|
56
60
|
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
57
61
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
58
62
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
59
63
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
64
|
+
s.add_dependency(%q<best_practice_project>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rubocop>, [">= 0"])
|
60
66
|
end
|
61
67
|
else
|
62
68
|
s.add_dependency(%q<string-cases>, [">= 0"])
|
@@ -64,6 +70,8 @@ Gem::Specification.new do |s|
|
|
64
70
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
65
71
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
66
72
|
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
73
|
+
s.add_dependency(%q<best_practice_project>, [">= 0"])
|
74
|
+
s.add_dependency(%q<rubocop>, [">= 0"])
|
67
75
|
end
|
68
76
|
end
|
69
77
|
|
data/lib/html_gen/element.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#This class can be used to generate HTML.
|
1
|
+
# This class can be used to generate HTML.
|
2
2
|
#===Examples
|
3
3
|
# ele = HtmlGen::Element.new(:a) #=> #<HtmlGen::Element:0x00000000e5f650 @attr={}, @name=:a, @classes=[], @str_html="", @str="", @css={}, @eles=[], @nl="\n", @inden="\t">
|
4
4
|
# ele.classes << "custom_link"
|
@@ -10,48 +10,48 @@
|
|
10
10
|
#
|
11
11
|
# ele.html #=> "<a href=\"http://www.youtube.com\" style=\"font-weight: bold;\" class=\"custom_link\">\n\t<b>\n\t\tTitle of link\n\t</b>\n</a>\n"
|
12
12
|
class HtmlGen::Element
|
13
|
-
FORBIDDEN_SHORT = ["script"]
|
13
|
+
FORBIDDEN_SHORT = ["script"].freeze
|
14
14
|
|
15
|
-
#Attributes hash which will be used to generate attributes-elements.
|
15
|
+
# Attributes hash which will be used to generate attributes-elements.
|
16
16
|
#===Example
|
17
17
|
# element.attr[:href] = "http://www.youtube.com"
|
18
18
|
attr_reader :attr
|
19
19
|
|
20
|
-
#CSS-hash which will be used to generate the 'style'-attribute.
|
20
|
+
# CSS-hash which will be used to generate the 'style'-attribute.
|
21
21
|
#===Example
|
22
22
|
# element.css["font-weight"] = "bold"
|
23
23
|
attr_reader :css
|
24
24
|
|
25
|
-
#Data hash which will nest keys.
|
25
|
+
# Data hash which will nest keys.
|
26
26
|
attr_reader :data
|
27
27
|
|
28
|
-
#Classes-array which will be used to generate the 'class'-attribute.
|
28
|
+
# Classes-array which will be used to generate the 'class'-attribute.
|
29
29
|
#===Example
|
30
30
|
# element.classes += ["class1", "class2"]
|
31
31
|
# element.html #=> ... class="class1 class2"...
|
32
32
|
attr_reader :classes
|
33
33
|
|
34
|
-
#This string is used to generate the value of an element. It will be HTML-escaped.
|
34
|
+
# This string is used to generate the value of an element. It will be HTML-escaped.
|
35
35
|
#===Example
|
36
36
|
# element = HtmlGen::Element.new("b")
|
37
37
|
# element.str = "Te<i>s</i>t"
|
38
38
|
# element.html(pretty: false) #=> "<b>Te<i>s</i>t</b>"
|
39
39
|
attr_accessor :str
|
40
40
|
|
41
|
-
#This string is used to generate the value of an element. It will not be HTML-escaped.
|
41
|
+
# This string is used to generate the value of an element. It will not be HTML-escaped.
|
42
42
|
#===Example
|
43
43
|
# element = HtmlGen::Element.new("b")
|
44
44
|
# element.str_html = "Te<i>s</i>t"
|
45
45
|
# element.html #=> "<b>Te<i>s</i>t</b>"
|
46
46
|
attr_accessor :str_html
|
47
47
|
|
48
|
-
#An array holding all the sub-elements of this element.
|
48
|
+
# An array holding all the sub-elements of this element.
|
49
49
|
attr_accessor :eles
|
50
50
|
|
51
|
-
#The name of the element. "a" for <a> and such.
|
51
|
+
# The name of the element. "a" for <a> and such.
|
52
52
|
attr_accessor :name
|
53
53
|
|
54
|
-
#You can give various arguments as shortcuts to calling the methods. You can also decide what should be used for newline and indentation.
|
54
|
+
# You can give various arguments as shortcuts to calling the methods. You can also decide what should be used for newline and indentation.
|
55
55
|
# HtmlGen::Element.new(:b, {
|
56
56
|
# css: {"font-weight" => "bold"},
|
57
57
|
# attr: {"href" => "http://www.youtube.com"},
|
@@ -63,7 +63,7 @@ class HtmlGen::Element
|
|
63
63
|
# eles: [HtmlGen::Element.new("i", str: "Hello world!")
|
64
64
|
# })
|
65
65
|
def initialize(name, args = {})
|
66
|
-
raise "'name' should be a string or a symbol but was a '#{name.class.name}'."if !name.is_a?(String) && !name.is_a?(Symbol)
|
66
|
+
raise "'name' should be a string or a symbol but was a '#{name.class.name}'." if !name.is_a?(String) && !name.is_a?(Symbol)
|
67
67
|
@name = name
|
68
68
|
|
69
69
|
{attr: {}, data: {}, classes: [], str_html: "", str: "", css: {}, eles: [], nl: "\n", inden: "\t"}.each do |arg, default_val|
|
@@ -76,10 +76,10 @@ class HtmlGen::Element
|
|
76
76
|
args.delete(arg)
|
77
77
|
end
|
78
78
|
|
79
|
-
raise "Unknown arguments: '#{args.keys.join(",")}'."
|
79
|
+
raise "Unknown arguments: '#{args.keys.join(",")}'." unless args.empty?
|
80
80
|
end
|
81
81
|
|
82
|
-
#Adds a sub-element to the element.
|
82
|
+
# Adds a sub-element to the element.
|
83
83
|
#===Examples
|
84
84
|
# element = HtmlGen::Element.new("a")
|
85
85
|
# another_ele = element.add_ele("b")
|
@@ -88,58 +88,58 @@ class HtmlGen::Element
|
|
88
88
|
def add_ele(name, args = {})
|
89
89
|
ele = HtmlGen::Element.new(name, args.merge(nl: @nl, inden: @inden))
|
90
90
|
@eles << ele
|
91
|
-
|
91
|
+
ele
|
92
92
|
end
|
93
93
|
|
94
94
|
alias add add_ele
|
95
95
|
|
96
|
-
#Add a text-element to the element.
|
96
|
+
# Add a text-element to the element.
|
97
97
|
def add_str(str)
|
98
98
|
ele = HtmlGen::TextEle.new(str: str, inden: @inden, nl: @nl)
|
99
99
|
@eles << ele
|
100
|
-
|
100
|
+
ele
|
101
101
|
end
|
102
102
|
|
103
|
-
#Add a text-element to the element.
|
103
|
+
# Add a text-element to the element.
|
104
104
|
def add_html(html)
|
105
105
|
ele = HtmlGen::TextEle.new(html: html, inden: @inden, nl: @nl)
|
106
106
|
@eles << ele
|
107
|
-
|
107
|
+
ele
|
108
108
|
end
|
109
109
|
|
110
110
|
# Returns the HTML for the element.
|
111
111
|
# To avoid indentation and newlines you can use the 'pretty'-argument:
|
112
112
|
# element.html(pretty: false)
|
113
113
|
def html(args = {})
|
114
|
-
if args[:level]
|
115
|
-
|
116
|
-
|
117
|
-
|
114
|
+
level = if args[:level]
|
115
|
+
args[:level]
|
116
|
+
else
|
117
|
+
0
|
118
118
|
end
|
119
119
|
|
120
|
-
if args.key?(:pretty)
|
121
|
-
|
122
|
-
|
123
|
-
|
120
|
+
pretty = if args.key?(:pretty)
|
121
|
+
args[:pretty]
|
122
|
+
else
|
123
|
+
true
|
124
124
|
end
|
125
125
|
|
126
|
-
#Used for keeping 'pretty'-value and correct indentation according to parent elements.
|
126
|
+
# Used for keeping 'pretty'-value and correct indentation according to parent elements.
|
127
127
|
pass_args = {level: (level + 1), pretty: pretty, inden: @inden}
|
128
128
|
|
129
|
-
#Clone the attributes-hash since we are going to add stuff to it, and it shouldnt be reflected (if 'html' is called multiple times, it will bug unless we clone).
|
129
|
+
# Clone the attributes-hash since we are going to add stuff to it, and it shouldnt be reflected (if 'html' is called multiple times, it will bug unless we clone).
|
130
130
|
attr = @attr.clone
|
131
131
|
|
132
|
-
#Start generating the string with HTML (possible go give a custom 'str'-variable where the content should be pushed to).
|
133
|
-
if args[:str]
|
134
|
-
|
135
|
-
|
136
|
-
|
132
|
+
# Start generating the string with HTML (possible go give a custom 'str'-variable where the content should be pushed to).
|
133
|
+
str = if args[:str]
|
134
|
+
args[:str]
|
135
|
+
else
|
136
|
+
""
|
137
137
|
end
|
138
138
|
|
139
139
|
str << @inden * level if pretty && level > 0
|
140
140
|
str << "<#{@name}"
|
141
141
|
|
142
|
-
#Add content from the 'css'-hash to the 'style'-attribute in the right format.
|
142
|
+
# Add content from the 'css'-hash to the 'style'-attribute in the right format.
|
143
143
|
unless @css.empty?
|
144
144
|
style = ""
|
145
145
|
@css.each do |key, val|
|
@@ -154,7 +154,7 @@ class HtmlGen::Element
|
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
|
-
#Add content from the 'classes'-array to the 'class'-attribute in the right format.
|
157
|
+
# Add content from the 'classes'-array to the 'class'-attribute in the right format.
|
158
158
|
unless @classes.empty?
|
159
159
|
class_str = @classes.join(" ")
|
160
160
|
|
@@ -165,7 +165,7 @@ class HtmlGen::Element
|
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
168
|
-
#Write out the attributes to the string.
|
168
|
+
# Write out the attributes to the string.
|
169
169
|
attr.each do |key, val|
|
170
170
|
str << " #{key}=\"#{HtmlGen.escape_html(val)}\""
|
171
171
|
end
|
@@ -176,11 +176,11 @@ class HtmlGen::Element
|
|
176
176
|
skip_pretty = false
|
177
177
|
|
178
178
|
if @eles.empty? && @str.empty? && @str_html.empty? && !forbidden_short
|
179
|
-
#If no sub-string, sub-HTML or sub-elements are given, we should end the HTML with " />".
|
179
|
+
# If no sub-string, sub-HTML or sub-elements are given, we should end the HTML with " />".
|
180
180
|
str << " />"
|
181
181
|
str << @nl if pretty
|
182
182
|
else
|
183
|
-
#Write end-of-element and then all sub-elements.
|
183
|
+
# Write end-of-element and then all sub-elements.
|
184
184
|
str << ">"
|
185
185
|
|
186
186
|
if @eles.empty? && @str.empty? && @str_html.empty? && forbidden_short
|
@@ -210,21 +210,20 @@ class HtmlGen::Element
|
|
210
210
|
str << @nl if pretty
|
211
211
|
end
|
212
212
|
|
213
|
-
|
214
|
-
return str
|
213
|
+
str
|
215
214
|
end
|
216
215
|
|
217
|
-
#Returns the names of all sub-elements in an array.
|
216
|
+
# Returns the names of all sub-elements in an array.
|
218
217
|
def eles_names
|
219
218
|
names = []
|
220
219
|
@eles.each do |ele|
|
221
220
|
names << ele.name
|
222
221
|
end
|
223
222
|
|
224
|
-
|
223
|
+
names
|
225
224
|
end
|
226
225
|
|
227
|
-
#Converts the content of the 'style'-attribute to css-hash-content.
|
226
|
+
# Converts the content of the 'style'-attribute to css-hash-content.
|
228
227
|
def convert_style_to_css
|
229
228
|
if !@attr[:style].to_s.strip.empty?
|
230
229
|
style = @attr[:style]
|
@@ -235,13 +234,13 @@ class HtmlGen::Element
|
|
235
234
|
end
|
236
235
|
|
237
236
|
loop do
|
238
|
-
if match = style.match(/\A\s*(\S+?):\s*(.+?)\s*(;|\Z)/)
|
237
|
+
if (match = style.match(/\A\s*(\S+?):\s*(.+?)\s*(;|\Z)/))
|
239
238
|
style.gsub!(match[0], "")
|
240
239
|
key = match[1]
|
241
240
|
val = match[2]
|
242
241
|
raise "Such a key already exists in CSS-hash: '#{key}'." if @css.key?(key)
|
243
242
|
@css[key] = val
|
244
|
-
elsif match = style.slice!(/\A\s*\Z/)
|
243
|
+
elsif (match = style.slice!(/\A\s*\Z/))
|
245
244
|
break
|
246
245
|
else
|
247
246
|
raise "Dont know what to do with style-variable: '#{style}'."
|
@@ -276,15 +275,17 @@ private
|
|
276
275
|
def data_attributes(data_hash, prev_key)
|
277
276
|
html = ""
|
278
277
|
data_hash.each do |key, value|
|
278
|
+
key = key.to_s.tr("_", "-") if key.is_a?(Symbol)
|
279
|
+
|
279
280
|
if value.is_a?(Hash)
|
280
281
|
html << " " unless html.empty?
|
281
|
-
html <<
|
282
|
+
html << data_attributes(value, "#{prev_key}-#{key}").to_s
|
282
283
|
else
|
283
284
|
html << " " unless html.empty?
|
284
285
|
html << "#{prev_key}-#{key}=\"#{HtmlGen.escape_html(value)}\""
|
285
286
|
end
|
286
287
|
end
|
287
288
|
|
288
|
-
|
289
|
+
html
|
289
290
|
end
|
290
291
|
end
|
data/lib/html_gen/parser.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
#A simple, lightweight and pure-Ruby class for parsing HTML-strings into elements.
|
1
|
+
# A simple, lightweight and pure-Ruby class for parsing HTML-strings into elements.
|
2
2
|
#===Examples
|
3
3
|
# doc = HtmlGen::Parser.new(str: a_html_variable)
|
4
4
|
# html_ele = doc.eles.first
|
5
5
|
# html_ele.name #=> "html"
|
6
6
|
class HtmlGen::Parser
|
7
|
-
#An array that holds all the parsed root-elements.
|
7
|
+
# An array that holds all the parsed root-elements.
|
8
8
|
attr_reader :eles
|
9
9
|
|
10
|
-
#The constructor. See class documentation for usage of this.
|
10
|
+
# The constructor. See class documentation for usage of this.
|
11
11
|
def initialize(args)
|
12
12
|
if args[:io]
|
13
13
|
@io = args[:io]
|
@@ -24,17 +24,15 @@ class HtmlGen::Parser
|
|
24
24
|
@eles_t = []
|
25
25
|
@debug = args[:debug]
|
26
26
|
|
27
|
-
while !@eof || !@buffer.empty?
|
28
|
-
parse_tag
|
29
|
-
end
|
27
|
+
parse_tag while !@eof || !@buffer.empty?
|
30
28
|
end
|
31
29
|
|
32
30
|
private
|
33
31
|
|
34
|
-
#Ensures at least 16kb of data is loaded into the buffer.
|
32
|
+
# Ensures at least 16kb of data is loaded into the buffer.
|
35
33
|
def ensure_buffer
|
36
|
-
while @buffer.length <
|
37
|
-
str = @io.gets(
|
34
|
+
while @buffer.length < 16_384 && !@eof
|
35
|
+
str = @io.gets(16_384)
|
38
36
|
if !str
|
39
37
|
@eof = true
|
40
38
|
else
|
@@ -43,7 +41,7 @@ private
|
|
43
41
|
end
|
44
42
|
end
|
45
43
|
|
46
|
-
#Searches for a given regex. If found the contents is removed from the buffer.
|
44
|
+
# Searches for a given regex. If found the contents is removed from the buffer.
|
47
45
|
def search(regex)
|
48
46
|
ensure_buffer
|
49
47
|
|
@@ -53,10 +51,10 @@ private
|
|
53
51
|
return match
|
54
52
|
end
|
55
53
|
|
56
|
-
|
54
|
+
false
|
57
55
|
end
|
58
56
|
|
59
|
-
#Asumes a tag is the next to be parsed and adds it to document-data.
|
57
|
+
# Asumes a tag is the next to be parsed and adds it to document-data.
|
60
58
|
def parse_tag(args = {})
|
61
59
|
if match = search(/\A\s*<\s*(\/|)\s*(\S+?)(\s+|\/\s*>|>)/)
|
62
60
|
tag_name = match[2].to_s.strip.downcase
|
@@ -78,7 +76,7 @@ private
|
|
78
76
|
@eles_t << ele
|
79
77
|
puts "New element-match: #{match.to_a}" if @debug
|
80
78
|
|
81
|
-
if end_sign
|
79
|
+
if end_sign =~ /^\/\s*>$/
|
82
80
|
puts "End of element '#{tag_name}' for '#{@eles_t.last.name}'." if @debug
|
83
81
|
ele = @eles_t.pop
|
84
82
|
raise "Expected ele-name to be: '#{tag_name}' but it wasnt: '#{ele.name}'." if ele.name.to_s != tag_name
|
@@ -101,7 +99,7 @@ private
|
|
101
99
|
end
|
102
100
|
end
|
103
101
|
|
104
|
-
#Parses all attributes of the current tag.
|
102
|
+
# Parses all attributes of the current tag.
|
105
103
|
def parse_attr_of_tag(ele, tag_name)
|
106
104
|
loop do
|
107
105
|
if match = search(/\A\s*(\S+)=(\"|'|)/)
|
@@ -129,21 +127,21 @@ private
|
|
129
127
|
end
|
130
128
|
end
|
131
129
|
|
132
|
-
#Parses an attribute-value until a given quote-char is reached.
|
130
|
+
# Parses an attribute-value until a given quote-char is reached.
|
133
131
|
def parse_attr_until_quote(quote_char, quote_val)
|
134
132
|
val = ""
|
135
133
|
|
136
134
|
loop do
|
137
135
|
ensure_buffer
|
138
136
|
char = @buffer.slice!(0)
|
139
|
-
break
|
137
|
+
break unless char
|
140
138
|
|
141
139
|
if char == "\\"
|
142
140
|
val << char
|
143
141
|
val << @buffer.slice!(0)
|
144
142
|
elsif char =~ quote_char
|
145
143
|
break
|
146
|
-
elsif char == ">"
|
144
|
+
elsif char == ">" && quote_val == :whitespace
|
147
145
|
@buffer = char + @buffer
|
148
146
|
break
|
149
147
|
else
|
@@ -151,13 +149,13 @@ private
|
|
151
149
|
end
|
152
150
|
end
|
153
151
|
|
154
|
-
|
152
|
+
val
|
155
153
|
end
|
156
154
|
|
157
|
-
#Assumes some content of a tag is next to be parsed and parses it.
|
155
|
+
# Assumes some content of a tag is next to be parsed and parses it.
|
158
156
|
def parse_content_of_tag(ele, tag_name)
|
159
157
|
raise "Empty tag-name given: '#{tag_name}'." if tag_name.to_s.strip.empty?
|
160
|
-
raise "No 'ele' was given."
|
158
|
+
raise "No 'ele' was given." unless ele
|
161
159
|
|
162
160
|
loop do
|
163
161
|
if search(/\A\s*\Z/)
|
@@ -170,15 +168,15 @@ private
|
|
170
168
|
break
|
171
169
|
elsif new_ele = parse_tag(false: true)
|
172
170
|
puts "Found new element '#{new_ele.name}' and adding it to '#{ele.name}'." if @debug
|
173
|
-
#ele.eles << new_ele
|
171
|
+
# ele.eles << new_ele
|
174
172
|
elsif match = search(/\A(.+?)(<|\Z)/)
|
175
173
|
puts "Text-content-match: '#{match.to_a}'." if @debug
|
176
174
|
|
177
|
-
#Put end back into buffer.
|
175
|
+
# Put end back into buffer.
|
178
176
|
@buffer = match[2] + @buffer
|
179
177
|
puts "Buffer after text-match: #{@buffer}" if @debug
|
180
178
|
|
181
|
-
#Add text element to list as finished.
|
179
|
+
# Add text element to list as finished.
|
182
180
|
ele.eles << HtmlGen::TextEle.new(str: match[1])
|
183
181
|
else
|
184
182
|
raise "Dont know what to do with buffer: '#{@buffer}'."
|
data/lib/html_gen/text_ele.rb
CHANGED
@@ -8,36 +8,37 @@ class HtmlGen::TextEle
|
|
8
8
|
@nl = args[:nl]
|
9
9
|
end
|
10
10
|
|
11
|
-
#Returns the text that this element holds.
|
12
|
-
|
13
|
-
return @str
|
14
|
-
end
|
11
|
+
# Returns the text that this element holds.
|
12
|
+
attr_reader :str
|
15
13
|
|
16
|
-
#Returns the text HTML-escaped.
|
14
|
+
# Returns the text HTML-escaped.
|
17
15
|
def html(args)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
str = ""
|
17
|
+
str << @inden * level(args) if pretty?(args)
|
18
|
+
str << html_content
|
19
|
+
str << @nl if pretty?(args)
|
20
|
+
str
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
23
|
+
private
|
24
|
+
|
25
|
+
def pretty?(args)
|
26
|
+
!args.key?(:pretty) || args[:pretty]
|
27
|
+
end
|
28
|
+
|
29
|
+
def level
|
30
|
+
if args[:level]
|
31
|
+
args[:level]
|
26
32
|
else
|
27
|
-
|
33
|
+
0
|
28
34
|
end
|
35
|
+
end
|
29
36
|
|
30
|
-
|
31
|
-
str << @inden * level if pretty
|
32
|
-
|
37
|
+
def html_content
|
33
38
|
if @str
|
34
|
-
|
39
|
+
HtmlGen.escape_html(@str)
|
35
40
|
else
|
36
|
-
|
41
|
+
@html
|
37
42
|
end
|
38
|
-
|
39
|
-
str << @nl if pretty
|
40
|
-
|
41
|
-
return str
|
42
43
|
end
|
43
44
|
end
|
data/lib/html_gen.rb
CHANGED
@@ -6,7 +6,7 @@ class HtmlGen
|
|
6
6
|
def self.const_missing(name)
|
7
7
|
file_path = "#{File.dirname(__FILE__)}/html_gen/#{::StringCases.camel_to_snake(name)}.rb"
|
8
8
|
|
9
|
-
if File.
|
9
|
+
if File.exist?(file_path)
|
10
10
|
require file_path
|
11
11
|
return HtmlGen.const_get(name) if HtmlGen.const_defined?(name)
|
12
12
|
end
|
@@ -16,6 +16,6 @@ class HtmlGen
|
|
16
16
|
|
17
17
|
# Escapes HTML from the given string. This is to avoid any dependencies and should not be used by other libs.
|
18
18
|
def self.escape_html(string)
|
19
|
-
|
19
|
+
string.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
|
20
20
|
end
|
21
21
|
end
|
data/shippable.yml
CHANGED
@@ -2,8 +2,9 @@ language: ruby
|
|
2
2
|
cache: bundler
|
3
3
|
archive: true
|
4
4
|
rvm:
|
5
|
-
- 2.1.
|
5
|
+
- 2.1.7
|
6
6
|
script:
|
7
7
|
- CODECLIMATE_REPO_TOKEN=81bf37d5bc3daf65852d6763dda5dbe2f39d5273dd23ac85a91d3efd41cd74d3 bundle exec rspec
|
8
|
+
- bundle exec rake best_practice_project:run
|
8
9
|
notifications:
|
9
10
|
email: false
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) +
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
2
2
|
|
3
3
|
describe "Parser" do
|
4
4
|
let(:parser) { HtmlGen::Parser.new(str: "<html><head><title>Test</title></head><body>This is the body</body></html>") }
|
data/spec/html_gen_spec.rb
CHANGED
@@ -55,11 +55,11 @@ describe "HtmlGen" do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it "supports data attributes" do
|
58
|
-
|
58
|
+
HtmlGen::Element.new(:div, str: "Test", data: {test: "value"})
|
59
59
|
end
|
60
60
|
|
61
61
|
it "supports nested data attributes" do
|
62
|
-
div_ele = HtmlGen::Element.new(:div, str: "Test", data: {deep: {nested: {test: "value"}}})
|
63
|
-
div_ele.html(pretty: false).should eq "<div data-deep-nested-test=\"value\">Test</div>"
|
62
|
+
div_ele = HtmlGen::Element.new(:div, str: "Test", data: {deep: {nested: {test: "value", test_underscoe: "test"}}})
|
63
|
+
div_ele.html(pretty: false).should eq "<div data-deep-nested-test=\"value\" data-deep-nested-test-underscoe=\"test\">Test</div>"
|
64
64
|
end
|
65
65
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require "codeclimate-test-reporter"
|
2
2
|
CodeClimate::TestReporter.start
|
3
3
|
|
4
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
5
5
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
-
require
|
7
|
-
require
|
6
|
+
require "rspec"
|
7
|
+
require "html_gen"
|
8
8
|
|
9
9
|
# Requires supporting files with custom matchers and macros, etc,
|
10
10
|
# in ./support/ and its subdirectories.
|
11
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
11
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
12
12
|
|
13
13
|
RSpec.configure do |config|
|
14
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_gen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kasper Johansen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: string-cases
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.8.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: best_practice_project
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: A small framework for generating HTML.
|
84
112
|
email: k@spernj.org
|
85
113
|
executables: []
|
@@ -90,6 +118,8 @@ extra_rdoc_files:
|
|
90
118
|
files:
|
91
119
|
- ".document"
|
92
120
|
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- ".rubocop_todo.yml"
|
93
123
|
- Gemfile
|
94
124
|
- Gemfile.lock
|
95
125
|
- LICENSE.txt
|