code_terminator 0.1.3 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb222ada4193c60d08123b1900f179726f553925
4
- data.tar.gz: fc52fc8bf422eecbecbd7280cde9112bb1f74d5a
3
+ metadata.gz: f709b570130343716bc93ce9ad74de6d0a2bfaeb
4
+ data.tar.gz: af32895544dd05949969cd09f79b6c743ef156e5
5
5
  SHA512:
6
- metadata.gz: 6570122173a590de2f18728120685cc9714309e704f125f0b9753cccd0b24aecce99bd7b61f804a3b8eb0bb41a0353827ad243f4fe0ddbccc51f1a60a126319b
7
- data.tar.gz: 9d1b5f40d43e1bac788a4dcdf0ce59c25720a6c61a9808c0782a0a6af8ad79776adb93b159d4125832473e50c847ef3b135b9f43865abfb38cb3395b3b128529
6
+ metadata.gz: a01842f4e2424e9389e71bde81fee7f8adacee5880e4f4737eed63b7c18ed16f35af02636656c1f4db8434164e9970e64030252cf1f875690a5f591334124df2
7
+ data.tar.gz: d40385534ee5461634abfc4bb542cff6f1f4a11d532e77883b4a75395fb7b524950d587d7e673db7c5bdf16076888e85194b2cf94d63febc44ffbb80c134635d
data/Gemfile CHANGED
@@ -10,6 +10,8 @@ gem 'rails', '4.2.6'
10
10
  # gem 'therubyracer', platforms: :ruby
11
11
  gem 'html5_validator'
12
12
  gem 'nokogiri'
13
+ gem 'crass'
14
+ gem 'css_parser'
13
15
 
14
16
  # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
15
17
  gem 'jbuilder', '~> 2.0'
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # CodeTerminator
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/code_terminator`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ CodeTerminator is a gem that helps to <strike>exterminate Sarah Connor</strike> parse, evaluate and compare html code. Also is useful when you need to check html code syntaxis.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ##Features
6
+ <ul>
7
+ <li>HTML parser </li>
8
+ <li>CSS parser (Coming soon)</li>
9
+ </ul>
6
10
 
7
11
  ## Installation
8
12
 
@@ -20,9 +24,88 @@ Or install it yourself as:
20
24
 
21
25
  $ gem install code_terminator
22
26
 
23
- ## Usage
27
+ ## Quick Start
28
+
29
+ To parse HTML and match the file with html code you just need to do:
30
+ ```ruby
31
+ # code = code get from an editor
32
+ # source = Source of the file you want to compare with
33
+
34
+ # First, validate syntasis of the code
35
+ ct = CodeTerminator::Html.new
36
+ errors = ct.validate_syntax(code)
37
+ result << errors[0]
38
+
39
+ # If code do't have errors, match the code with your html file
40
+ if errors.empty?
41
+ result = ct.match(source, code)
42
+ end
43
+ ```
44
+ If the code and the source mismatch, `ct.match()` will return an array with the differences between code and source file.
45
+ You will know that the code and the source file have the same html elements when the `ct.match()` return a nil array.
46
+
47
+ ```ruby
48
+ # hola_mundo.html
49
+ # <h1>Hola Mundo!</h1>
50
+ #
51
+ >> CodeTerminator::Html.match("hola_mundo.html","<h2>Hola Mundo!</h2>")
52
+ # => ["h1 not exist"]
53
+ ```
54
+
55
+ ##Cheat Sheet
56
+
57
+ ###match(source, code)
58
+ Match if the code have the same elements than the exercise. Return an array with the mismatches.
59
+
60
+ ```ruby
61
+ # hola_mundo.html
62
+ # => <h1>Hola Mundo!</h1>
63
+ ```
64
+ ```ruby
65
+ CodeTerminator::Html.match("hola_mundo.html","<h2>Hola Mundo!</h2>")
66
+ # => ["h1 not exist"]
67
+ #
68
+ ```
69
+ ###new_file(source, code)
70
+ Create a Html file with the code of the editor. Return a boolean that indicate if the file was created or not.
71
+ ```ruby
72
+ CodeTerminator::Html.new_file("hola_mundo.html", "<h1>Hola Mundo!</h1>")
73
+ # => true
74
+ ```
75
+ ###read_file(source)
76
+ Read a html file. Return the text of the file.
77
+ ```ruby
78
+ CodeTerminator::Html.read_file("hola_mundo.html")
79
+ # => "<h1>Hola Mundo!</h1>"
80
+ ```
81
+
82
+ ###validate_syntax(code)
83
+ Validate if the syntax is correct. Return an array with Nokogiri errors.
84
+ ```ruby
85
+ CodeTerminator::Html.validate_syntax("<h1>Hola Mundo!</h1")
86
+ # => [#<Nokogiri::XML::SyntaxError: expected '>'>]
87
+ ```
88
+
89
+ ###get_elements(source)
90
+ Get html elements of a html file. Return a list of Nokogiri XML objects.
91
+ ```ruby
92
+ CodeTerminator::Html.get_elements("hola_mundo.html")
93
+ # => [#<Nokogiri::XML::Element:0x3fe3391547d8 name="h1" children=[#<Nokogiri::XML::Text:0x3fe33915474c "Hola Mundo!">]>, #<Nokogiri::XML::Text:0x3fe33915474c "Hola Mundo!">]
94
+ ```
95
+
96
+ ###print_elements(Elements Array)
97
+ Get the elements of the code in html format. Return a string with elements in html.
98
+ <br>
99
+ **Get 'Elements Array' calling 'get_elements()'
100
+ ```ruby
101
+ CodeTerminator::Html.print_elements([#<Nokogiri::XML::Element:0x3fe31dc42bfc name="h1" children=[#<Nokogiri::XML::Text:0x3fe31dc42b70 "hola mundo">]>, #<Nokogiri::XML::Text:0x3fe31dc42b70 "hola mundo">])
102
+ # => "name = h1<br><hr>name = text<br>content = hola mundo<br><hr>"
103
+ #
104
+ ```
105
+
106
+ ##Example
107
+ An example of an editor using CodeTerminator is available at https://github.com/eponce19/editor_terminator.git
24
108
 
25
- TODO: Write usage instructions here
26
109
 
27
110
  ## Development
28
111
 
@@ -32,7 +115,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
115
 
33
116
  ## Contributing
34
117
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/code_terminator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eponce19/code_terminator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
119
 
37
120
 
38
121
  ## License
Binary file
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Evelin Ponce"]
10
10
  spec.email = ["eponce19@gmail.com"]
11
11
 
12
- spec.summary = "Validate syntaxis and instructions of html code"
13
- spec.description = "Helps to evaluate and parse html code."
12
+ spec.summary = "Validate syntaxis and instructions of html and css code"
13
+ spec.description = "Helps to evaluate and parse html and css code."
14
14
  spec.homepage = 'http://rubygems.org/gems/code_terminator'
15
15
  spec.license = "MIT"
16
16
 
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.10"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "minitest"
25
+
25
26
  end
@@ -0,0 +1,2 @@
1
+ body {
2
+ background-color: lightblue; }
@@ -0,0 +1,196 @@
1
+ require 'Nokogiri'
2
+ require 'Crass'
3
+ require 'css_parser'
4
+ require 'active_support/core_ext/string/filters'
5
+
6
+ class CodeTerminator::Css
7
+
8
+ def initialize(args = {})
9
+ @code = args[:code]
10
+ @source = args[:source]
11
+ @tags = Array.new
12
+ end
13
+
14
+ # Create a CSS file with the code of the editor. Return a boolean that indicate if the file was created or not.
15
+ #
16
+ # Example:
17
+ # >> CodeTerminator::Css.new_file("test.css", "<style> body { background-color: lightblue; }</style>")
18
+ # => true
19
+ #
20
+ # Arguments:
21
+ # source: (String)
22
+ # code: (String)
23
+
24
+ def new_file(source,code)
25
+ fileCss = File.new(source, "w+")
26
+ result = true
27
+ begin
28
+ fileCss.puts code
29
+ rescue
30
+ result = false
31
+ ensure
32
+ fileCss.close unless fileCss.nil?
33
+ end
34
+ #return true if file was succesfully created
35
+ result
36
+ end
37
+
38
+
39
+ # Get html elements of a css file. Return a array of selectors with their properties and values.
40
+ #
41
+ # Example:
42
+ # >> CodeTerminator::Css.get_elements("test.css")
43
+ # => [{:selector=>"body"}, {:selector=>"body", :property=>"background-color", :value=>"lightblue"}, {:selector=>"body", :property=>"color", :value=>"green"}]
44
+ #
45
+ # Arguments:
46
+ # source: (String)
47
+ #
48
+ # Fixes:
49
+ # IMPORTANT DELETE <STYLE> tag from the source
50
+
51
+
52
+ def get_elements(source)
53
+ reader = read_file(source)
54
+ parser = Crass.parse(reader)
55
+ errors = parser.pop
56
+ p errors
57
+ elements = Array.new
58
+ selector = ""
59
+
60
+ parser.each do |node|
61
+ if !node[:selector].nil?
62
+ selector = node[:selector][:value]
63
+ elements << {:selector => selector}
64
+ end
65
+ if !node[:children].nil?
66
+ node[:children].each do |children|
67
+ if children.has_value?(:property)
68
+ elements << {:selector => selector, :property => children[:name], :value => children[:value]}
69
+ end
70
+ end
71
+ end
72
+ end
73
+ elements
74
+ end
75
+
76
+ # Validate if the syntax is correct. If is valid return boolean true.
77
+ #
78
+ # Example:
79
+ # >> CodeTerminator::Html.validate_syntax("body { background-color: lightblue; }")
80
+ # => true
81
+ #
82
+ # Arguments:
83
+ # code: (String)
84
+ #
85
+ # Fixes:
86
+ # IMPORTANT Method not validate <STYLE> tag from the code
87
+
88
+ def validate_syntax(code)
89
+ valid = true
90
+ tree = Crass.parse(code)
91
+ last = tree.length
92
+ tree[last-1][:children].each do |children|
93
+ if children[:node].to_s == "error"
94
+ valid = false
95
+ end
96
+ end
97
+ valid
98
+ end
99
+
100
+ # Read a css file. Return a string with the text of the file.
101
+ #
102
+ # Example:
103
+ # >> CodeTerminator::Css.read_file("test.css")
104
+ # => "body { background-color: lightblue; }"
105
+ #
106
+ # Arguments:
107
+ # source: (String)
108
+
109
+ def read_file(source)
110
+ fileCss = File.open(source, "r")
111
+ text = ""
112
+ begin
113
+ fileCss.each_line do |line|
114
+ text << line
115
+ end
116
+ fileCss.close
117
+ rescue
118
+ text = false
119
+ ensure
120
+ #fileHtml.close unless fileHtml.nil?
121
+ end
122
+
123
+ text
124
+ end
125
+
126
+ # Get the elements of the code in css format. Return a string with elements in css.
127
+ #
128
+ # Example:
129
+ # >> CodeTerminator::Css.print_elements([{:selector=>"body"}, {:selector=>"body", :property=>"background-color", :value=>"lightblue"}] )
130
+ # => "selector = body<br><hr>selector = body<br>property = background-color<br>value = lightblue<br><hr>"
131
+ #
132
+ # Arguments:
133
+ # elements: (Array)
134
+
135
+
136
+ def print_elements(elements)
137
+ text = ""
138
+ elements.each do |child|
139
+ text << "selector = " + child[:selector] + "<br>"
140
+ text << "property = " + child[:property] + "<br>" if !child[:property].nil?
141
+ text << "value = " + child[:value] + "<br>" if !child[:value].nil?
142
+ text << "<hr>"
143
+ end
144
+ text
145
+ end
146
+
147
+
148
+ # Match if the code have the same elements than the exercise. Return an array with the mismatches.
149
+
150
+ # Example:
151
+ #
152
+ # test.css
153
+ # => body { background-color: lightblue; }
154
+ #
155
+ # >> CodeTerminator::Css.match("test.css","body {background-color: blue; }")
156
+ # => ["not the same property background-color in selector body "]
157
+ #
158
+ # Arguments:
159
+ # source: (String)
160
+ # code: (String)
161
+ #
162
+ # Fix: Add <style> tag in the compare
163
+
164
+ def match(source,code)
165
+ #source = "exercises/test.css"
166
+ #code = " a.hover { color: yellow; } body { background-color: lightblue; color: blue; } "
167
+ elements = get_elements(source)
168
+ css_errors = Array.new
169
+ parser = CssParser::Parser.new
170
+
171
+ parser.load_string!(code)
172
+ elements.each do |e|
173
+ item = e[:selector]
174
+ if !e[:property].nil?
175
+ property = e[:property] + ": " + e[:value]
176
+ parser_array = parser.find_by_selector(item)
177
+ if parser_array.any?
178
+ parser_property = parser_array[0].split(";")
179
+ parser_property.each {|a| a.strip! if a.respond_to? :strip! }
180
+ if !parser_property.include?(property)
181
+ css_errors << "not the same property " + property + " in selector " + item
182
+ end
183
+ else
184
+ css_errors << "property "+ property + " not found in " + item
185
+ end
186
+ end
187
+ end
188
+ css_errors
189
+ end
190
+
191
+ private
192
+
193
+
194
+ #end
195
+
196
+ end
@@ -119,8 +119,8 @@ class CodeTerminator::Html
119
119
  # Get the elements of the code in html format. Return a string with elements in html.
120
120
  #
121
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>"
122
+ # >> CodeTerminator::Html.print_elements([#<Nokogiri::XML::Element:0x3fe31dc42bfc name="h1" children=[#<Nokogiri::XML::Text:0x3fe31dc42b70 "hola mundo">]>, #<Nokogiri::XML::Text:0x3fe31dc42b70 "hola mundo">])
123
+ # => "name = h1<br><hr>name = text<br>content = hola mundo<br><hr>"
124
124
  #
125
125
  # Arguments:
126
126
  # elements: (Array)
@@ -1,3 +1,3 @@
1
1
  module CodeTerminator
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "code_terminator/version"
2
2
  require "code_terminator/html"
3
+ require "code_terminator/css"
3
4
 
4
5
  module CodeTerminator
5
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_terminator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evelin Ponce
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-17 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Helps to evaluate and parse html code.
55
+ description: Helps to evaluate and parse html and css code.
56
56
  email:
57
57
  - eponce19@gmail.com
58
58
  executables:
@@ -73,12 +73,15 @@ files:
73
73
  - code_terminator-0.1.0.gem
74
74
  - code_terminator-0.1.1.gem
75
75
  - code_terminator-0.1.2.gem
76
+ - code_terminator-0.1.3.gem
76
77
  - code_terminator.gemspec
77
78
  - exe/code_terminator
78
79
  - exercises/new.html
79
80
  - exercises/new2.html
81
+ - exercises/test.css
80
82
  - exercises/test.html
81
83
  - lib/code_terminator.rb
84
+ - lib/code_terminator/css.rb
82
85
  - lib/code_terminator/html.rb
83
86
  - lib/code_terminator/version.rb
84
87
  homepage: http://rubygems.org/gems/code_terminator
@@ -104,5 +107,5 @@ rubyforge_project:
104
107
  rubygems_version: 2.4.8
105
108
  signing_key:
106
109
  specification_version: 4
107
- summary: Validate syntaxis and instructions of html code
110
+ summary: Validate syntaxis and instructions of html and css code
108
111
  test_files: []