code_terminator 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6bdf80e428a50d7ef529a50616d5ec25c8e05c65
4
- data.tar.gz: ccca0876f7656c2d45601fb267504ab7bc6307e8
3
+ metadata.gz: 634176f1ddfaf0c0ba29fd2ddf876c1d5b970d71
4
+ data.tar.gz: 826282d9ee279e46917b6564723cef8a8e260647
5
5
  SHA512:
6
- metadata.gz: 90f8d44077544dc173bf694a012e9c47f038d87a2696807051e500d7e22af53c0827bb14b377adbc2329c92a3827b2a9e82cc271319f5eb122c173c4df42c887
7
- data.tar.gz: e9f02a06ad1f6820661422b304a2f3204af3f573ca72e3f8fe56e0a36a026e6897b47d6c92acdac732c46c077e0e8f1e760ff52e3d4ae353189cce5c9ca710cb
6
+ metadata.gz: 6b3c2a02790c995cc482dcc9c40ad41dcca3d194777b5e9e31a14e9c4793a14395b8e12502b32e75adc23d460f98c8942ecee028bfef73718717d4f2ba1ee809
7
+ data.tar.gz: 80793d8937b41a26e747ca6eaffd5a790b7aade9f479b23064ff4aed4c0a1cc014126691474b6d9c5c104f6ea839f6aada975e3fb51ee69612ade2d4729b4fcd
@@ -1,13 +1,24 @@
1
- #class CodeTerminator::Html
2
- #class CodeTerminator::Html
1
+ require 'Nokogiri'
2
+ require 'active_support/core_ext/string/filters'
3
3
 
4
4
  class CodeTerminator::Html
5
5
 
6
6
  def initialize(args = {})
7
7
  @code = args[:code]
8
8
  @source = args[:source]
9
+ @tags = Array.new
9
10
  end
10
11
 
12
+ # Create a Html file with the code of the editor. Return a boolean that indicate if the file was created or not.
13
+ #
14
+ # Example:
15
+ # >> CodeTerminator::Html.new_file("hola_mundo.html", "<h1>Hola Mundo!</h1>")
16
+ # => true
17
+ #
18
+ # Arguments:
19
+ # source: (String)
20
+ # code: (String)
21
+
11
22
  def new_file(source,code)
12
23
  fileHtml = File.new(source, "w+")
13
24
  result = true
@@ -22,8 +33,182 @@ class CodeTerminator::Html
22
33
  result
23
34
  end
24
35
 
25
- def print(text)
26
- p text
36
+
37
+ # Get html elements of a html file. Return a list of Nokogiri XML objects.
38
+ #
39
+ # Example:
40
+ # >> CodeTerminator::Html.get_elements("hola_mundo.html")
41
+ # => [#<Nokogiri::XML::Element:0x3fe3391547d8 name="h1" children=[#<Nokogiri::XML::Text:0x3fe33915474c "Hola Mundo!">]>, #<Nokogiri::XML::Text:0x3fe33915474c "Hola Mundo!">]
42
+ #
43
+ # Arguments:
44
+ # source: (String)
45
+
46
+ def get_elements(source)
47
+ reader = Nokogiri::HTML(File.open(source))
48
+ reader = remove_empty_text(reader)
49
+ reader.at('body').children.each do |child|
50
+ @tags.push(child)
51
+ add_children(child) if child.children.any?
52
+ end
53
+ @tags
54
+ end
55
+
56
+ # Validate if the syntax is correct. Return an array with Nokogiri errors.
57
+ #
58
+ # Example:
59
+ # >> CodeTerminator::Html.validate_syntax("<h1>Hola Mundo!</h1")
60
+ # => [#<Nokogiri::XML::SyntaxError: expected '>'>]
61
+ #
62
+ # Arguments:
63
+ # code: (String)
64
+
65
+ def validate_syntax(code)
66
+ errors = Array.new
67
+
68
+ begin
69
+ xml = Nokogiri::XML(code) { |config| config.strict }
70
+
71
+ #validate if html follow w3, uncomment when check all the page
72
+ #"<!DOCTYPE html>
73
+ # <html>
74
+ # <head>
75
+ # <h1>asdasd</h1>
76
+ # <title>asdasd</title>
77
+ # </head>
78
+ # <body>
79
+ # <h1>hola</h1>
80
+ # </body>
81
+ # </html>"
82
+ # @validator = Html5Validator::Validator.new
83
+ # @validator.validate_text(@html)
84
+
85
+ rescue Nokogiri::XML::SyntaxError => e
86
+ #errors[0] = "Check if you close your tags"
87
+ errors[0] = e
88
+ end
89
+
90
+ errors
91
+ end
92
+
93
+ # Read a html file. Return the text of the file.
94
+ #
95
+ # Example:
96
+ # >> CodeTerminator::Html.read_file("hola_mundo.html")
97
+ # => "<h1>Hola Mundo!</h1>\n"
98
+ #
99
+ # Arguments:
100
+ # source: (String)
101
+
102
+ def read_file(source)
103
+ fileHtml = File.open(source, "r")
104
+ text = ""
105
+ begin
106
+ fileHtml.each_line do |line|
107
+ text << line
108
+ end
109
+ fileHtml.close
110
+ rescue
111
+ text = "error"
112
+ ensure
113
+ #fileHtml.close unless fileHtml.nil?
114
+ end
115
+
116
+ text
117
+ end
118
+
119
+ # Get the elements of the code in html format. Return a string with elements in html.
120
+ #
121
+ # Example:
122
+ # >> CodeTerminator::Html.print_elements([#<Nokogiri::XML::Element:0x3fe31dc42bfc name="h1" children=[#<Nokogiri::XML::Text:0x3fe31dc42b70 "hola evelin">]>, #<Nokogiri::XML::Text:0x3fe31dc42b70 "hola evelin">])
123
+ # => "name = h1<br><hr>name = text<br>content = hola evelin<br><hr>"
124
+ #
125
+ # Arguments:
126
+ # elements: (Array)
127
+
128
+ def print_elements(elements)
129
+ text = ""
130
+ elements.each do |child|
131
+ text << "name = " + child.name + "<br>"
132
+ text << "content = " + child.text + "<br>" if child.text?
133
+ child.attribute_nodes.each do |child_attribute|
134
+ text << child.name + " attribute = " + child_attribute.name + " - " + child_attribute.value + "<br>"
135
+ end
136
+ text << "<hr>"
137
+ end
138
+ text
139
+ end
140
+
141
+
142
+ # Match if the code have the same elements than the exercise. Return an array with the mismatches.
143
+ #
144
+ # Example:
145
+ #
146
+ # hola_mundo.html
147
+ # => <h1>Hola Mundo!</h1>
148
+ #
149
+ # >> CodeTerminator::Html.match("hola_mundo.html","<h2>Hola Mundo!</h2>")
150
+ # => ["h1 not exist"]
151
+ #
152
+ # Arguments:
153
+ # source: (String)
154
+ # code: (String)
155
+
156
+ def match(source, code)
157
+ html_errors = Array.new
158
+
159
+ code = Nokogiri::HTML(code)
160
+
161
+ elements = get_elements(source)
162
+ #@elements = Html::PrintElements.call(elements)
163
+
164
+ elements.each do |element|
165
+ if element.name == "text"
166
+ #code if element is text
167
+ else
168
+ if code.css(element.name).length == 0
169
+ html_errors << element.name + " not exist"
170
+ else
171
+ element.attribute_nodes.each do |element_attribute|
172
+ if !code.css(element.name).attribute(element_attribute.name).nil?
173
+ if code.css(element.name).attribute(element_attribute.name).value != element_attribute.value
174
+ html_errors << element_attribute.name + " not is the same value " + element_attribute.value
175
+ end
176
+ else
177
+ html_errors << element_attribute.name + " not exist"
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ html_errors
184
+ end
185
+
186
+ private
187
+
188
+ def add_children(parent)
189
+ parent.children.each do |child|
190
+ @tags.push(child)
191
+ add_children(child) if child.children.any?
192
+ end
193
+ end
194
+
195
+ def remove_empty_text (reader)
196
+ reader.at("body").children.each do |child|
197
+ if child.text?
198
+ child.remove if child.content.to_s.squish.empty?
199
+ end
200
+ check_children(child) if child.children.any?
201
+ end
202
+ reader
203
+ end
204
+
205
+ def check_children(parent)
206
+ parent.children.each do |child|
207
+ if child.text?
208
+ child.remove if child.content.to_s.squish.empty?
209
+ end
210
+ check_children(child) if child.children.any?
211
+ end
27
212
  end
28
213
 
29
214
  #end
@@ -1,3 +1,3 @@
1
1
  module CodeTerminator
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -2,13 +2,14 @@ require "code_terminator/version"
2
2
  require "code_terminator/html"
3
3
 
4
4
  module CodeTerminator
5
- # Your code goes here...
6
- def self.process(source="",code="")
7
5
 
8
- file = CodeTerminator::Html.new()
9
- file.new_file(source,code)
10
- file.print("hola")
11
- #code + " HI"
12
- end
6
+ # def self.process(source="",code="")
7
+ #
8
+ # file = CodeTerminator::Html.new()
9
+ # file.get_elements(source)
10
+ # #file.new_file(source,code)
11
+ # #file.print("hola")
12
+ # #code + " HI"
13
+ # end
13
14
 
14
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_terminator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evelin Ponce