Class: CodeTerminator::Html
- Inherits:
-
Object
- Object
- CodeTerminator::Html
- Defined in:
- lib/code_terminator/html.rb
Instance Method Summary collapse
-
#get_elements(source) ⇒ Object
Get html elements of a html file.
-
#get_instructions(source) ⇒ Object
Get the instructions to recreate the html code.
-
#initialize(args = {}) ⇒ Html
constructor
A new instance of Html.
-
#match(source, code) ⇒ Object
Match if the code have the same elements than the exercise.
-
#new_file(source, code) ⇒ Object
Create a Html file with the code of the editor.
-
#print_elements(elements) ⇒ Object
Get the elements of the code in html format.
-
#read_file(source) ⇒ Object
Read a html file.
-
#validate_syntax(code) ⇒ Object
Validate if the syntax is correct.
Constructor Details
#initialize(args = {}) ⇒ Html
Returns a new instance of Html.
5 6 7 8 9 10 11 12 13 |
# File 'lib/code_terminator/html.rb', line 5 def initialize(args = {}) @code = args[:code] @source = args[:source] @tags = Array.new @elements = Array.new args[:source_type] ||= "file" @source_type = args[:source_type] end |
Instance Method Details
#get_elements(source) ⇒ Object
Get html elements of a html file. Return a list of Nokogiri XML objects.
Example:
>> CodeTerminator::Html.get_elements("hola_mundo.html")
=> [#<Nokogiri::XML::Element:0x3fe3391547d8 name="h1" children=[#<Nokogiri::XML::Text:0x3fe33915474c "Hola Mundo!">]>, #<Nokogiri::XML::Text:0x3fe33915474c "Hola Mundo!">]
Arguments:
source: (String)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/code_terminator/html.rb', line 49 def get_elements(source) @elements = Array.new #How to read if is an url if @source_type == "url" reader = Nokogiri::HTML(open(source).read) else reader = Nokogiri::HTML(File.open(source)) end #remove empty spaces from reader reader = remove_empty_text(reader) node = Hash.new node[:parent] = "" node[:tag] = "html" @elements << node #search elements from body section if !reader.at('body').nil? node = Hash.new node[:parent] = "html" node[:tag] = "body" @elements << node reader.at('body').attribute_nodes.each do |element_attribute| node = Hash.new node[:parent] = "html" node[:tag] = "body" node[:attribute] = element_attribute.name if !element_attribute.name.nil? node[:value] = element_attribute.value if !element_attribute.value.nil? @elements << node end end #end search #search elements from head section if !reader.at('head').nil? node = Hash.new node[:parent] = "html" node[:tag] = "head" @elements << node reader.at('head').children.each do |child| if child.attribute_nodes.empty? node = Hash.new node[:parent] = "head" node[:tag] = child.name node[:content] = child.text if !child.text.nil? or child.comment? @elements << node else child.attribute_nodes.each do |element_attribute| node = Hash.new node[:parent] = "head" if child.name == "#cdata-section" node[:tag] = "text" else node[:tag] = child.name end # node[:tag] = ( ? "text", child.name) node[:content] = child.text if !child.text.nil? node[:attribute] = element_attribute.name if !element_attribute.name.nil? node[:value] = element_attribute.value if !element_attribute.value.nil? @elements << node end end add_children(child) if child.children.any? end end #end search elements #search elements inside body (children) if !reader.at('body').nil? reader.at('body').children.each do |child| if child.attribute_nodes.empty? node = Hash.new node[:parent] = "body" node[:tag] = child.name node[:content] = child.text if child.text? or child.comment? @elements << node else child.attribute_nodes.each do |element_attribute| node = Hash.new node[:parent] = "body" node[:tag] = child.name node[:attribute] = element_attribute.name if !element_attribute.name.nil? node[:value] = element_attribute.value if !element_attribute.value.nil? @elements << node end end add_children(child) if child.children.any? end end #end search elements @elements end |
#get_instructions(source) ⇒ Object
Get the instructions to recreate the html code. Return an array with strings .
Example:
>> CodeTerminator::Html.get_instructions(file.get_elements("exercises/test.html"))
=> ["Add the tag h2 in body", "Add the tag text in h2 with content 'hola test' ", "Add the tag p in body"]
Arguments:
instructions: (Array)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/code_terminator/html.rb', line 242 def get_instructions(source) elements = get_elements(source) text = "" instructions = Array.new elements.each do |child| if child[:tag]!="text" text << "Add the tag " + child[:tag] text << " in " + child[:parent] if !child[:parent].nil? text << " with an attribute '" + child[:attribute] + "' " if !child[:attribute].nil? text << " with value '" + child[:value] + "' " if !child[:value].nil? elsif child[:tag] == "comment" text << " In " + child[:tag]+ " add the text '" + child[:content] + "' " if !child[:content].nil? else text << " In " + child[:parent]+ " add the text '" + child[:content] + "' " if !child[:content].nil? end instructions.push(text) text = "" end instructions end |
#match(source, code) ⇒ Object
Match if the code have the same elements than the exercise. Return an array with the mismatches.
Example:
hola_mundo.html
> <h1>Hola Mundo!</h1>
>> CodeTerminator::Html.match("hola_mundo.html","<h2>Hola Mundo!</h2>")
=> ["h1 not exist"]
Arguments:
source: (String)
code: (String)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/code_terminator/html.rb', line 279 def match(source, code) html_errors = Array.new code = Nokogiri::HTML(code) elements = get_elements(source) exist_in_body = Array.new error333 = nil elements.each do |e| item = e[:tag] if item == "text" or item == "comment" # Check the text if !e[:content].nil? if code.css(e[:parent]).count < 2 if code.css(e[:parent]).class == Nokogiri::XML::NodeSet text_found = false error330 = nil if code.css(e[:parent]).children.any? code.css(e[:parent]).children.each do |node_child| if node_child.class != Nokogiri::XML::Element #embebed code #if code.css(e[:parent]).text != e[:content] if node_child.text.strip != e[:content].strip if item == "comment" # if comment isn't present in the code, mark add tag if e[:content].strip != "" error330 = new_error(element: e, type: 330, description: "The text inside the comment should be #{e[:content]}") else # html_errors << new_error(element: e, type: 404, description: "Remember to add the `<#{e[:tag]}>` tag") end else error330 = new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}") end else text_found = true end #end embebed code end end #end each else if code.css(e[:parent]).text.strip != e[:content].strip # p "text of node: " + code.css(e[:parent]).text if item == "comment" error330 = new_error(element: e, type: 330, description: "The text inside the comment should be #{e[:content]}") else error330 = new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}") end else text_found = true end end #end if parent has children if !text_found && !error330.nil? html_errors << error330 error330 = nil end end #end if parent is nodeset else exist = false code.css(e[:parent]).each do |code_css| #if code_css.at_css(e[:tag]).parent.name == e[:parent] p "text content: " + code_css.text if code_css.text == e[:content] exist = true end #end end if !exist html_errors << new_error(element: e, type: 330, description: "The text inside `<#{e[:parent]}>` should be #{e[:content]}") end end #end if parent < 2 end #end if content is null else #item class is different to text or comment if code.css(e[:tag]).length > 0 code.css(e[:tag]).each do |tag| if !e[:attribute].nil? # Check the tag's attributes if tag.attribute(e[:attribute]).nil? html_errors << new_error(element: e, type: 334, description: "`<#{e[:tag]}>` should have an attribute named #{e[:attribute]}") else if tag.attribute(e[:attribute]).value != e[:value] exist_in_body << false # p "type " + e[:tag] + " with attribute " + e[:attribute] + " value " + e[:value] # Check if the img have attribute src and value is null, the user can write whatever image he wants if !(e[:tag] == "img" && e[:attribute] == "src" && e[:value] == "") error333 = new_error(element: e, type: 333, description: "Make sure that the attribute #{e[:attribute]} in `<#{e[:tag]}>` has the value #{e[:value]}") end else exist_in_body << true end end end # Check that tags exist within parent tags if tag.first.respond_to? :parent if tag.count < 2 && !tag.first.nil? if tag.first.parent.name != e[:parent] html_errors << new_error(element: e, type: 440, description: "Remember to add the `<#{e[:tag]}>` tag inside `<#{e[:parent]}>`") end else exist_in_parent = false tag.each do |code_css| if code_css.parent.name == e[:parent] exist_in_parent = true end end if !exist_in_parent html_errors << new_error(element: e, type: 440, description: "Remember to add the `<#{e[:tag]}>` tag inside `<#{e[:parent]}>`") end end end end else # Check that the tag is present if code.at_css(e[:tag]).nil? html_errors << new_error(element: e, type: 404, description: "Remember to add the `<#{e[:tag]}>` tag") end end if !exist_in_body.empty? && !exist_in_body.include?(true) && !error333.nil? html_errors << error333 end exist_in_body = [] end end html_errors end |
#new_file(source, code) ⇒ Object
Create a Html file with the code of the editor. Return a boolean that indicate if the file was created or not.
Example:
>> CodeTerminator::Html.new_file("hola_mundo.html", "<h1>Hola Mundo!</h1>")
=> true
Arguments:
source: (String)
code: (String)
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/code_terminator/html.rb', line 25 def new_file(source,code) fileHtml = File.new(source, "w+") result = true begin fileHtml.puts code rescue result = false ensure fileHtml.close unless fileHtml.nil? end #return true if file was succesfully created result end |
#print_elements(elements) ⇒ Object
Get the elements of the code in html format. Return a string with elements in html.
Example:
>> CodeTerminator::Html.print_elements("exercises/hola_mundo.html" )
=> "name = h1<br><hr>name = text<br>content = hola mundo<br><hr>"
Arguments:
elements: (Array)
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/code_terminator/html.rb', line 220 def print_elements(elements) text = "" elements.each do |child| text << "parent = " + child[:parent] + "<br>" if !child[:parent].nil? text << "tag = " + child[:tag] + "<br>" if !child[:tag].nil? text << "attribute = " + child[:attribute] + "<br>" if !child[:attribute].nil? text << "value = " + child[:value] + "<br>" if !child[:value].nil? text << "content = " + child[:content] + "<br>" if !child[:content].nil? text << "<hr>" end text end |
#read_file(source) ⇒ Object
Read a html file. Return the text of the file.
Example:
>> CodeTerminator::Html.read_file("hola_mundo.html")
=> "<h1>Hola Mundo!</h1>\n"
Arguments:
source: (String)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/code_terminator/html.rb', line 189 def read_file(source) if @source_type == "url" fileHtml = open(source).read else fileHtml = File.open(source, "r") end text = "" begin fileHtml.each_line do |line| text << line end fileHtml.close rescue text = false ensure #fileHtml.close unless fileHtml.nil? end text end |
#validate_syntax(code) ⇒ Object
Validate if the syntax is correct. Return an array with Nokogiri errors.
Example:
>> CodeTerminator::Html.validate_syntax("<h1>Hola Mundo!</h1")
=> [#<Nokogiri::XML::SyntaxError: expected '>'>]
Arguments:
code: (String)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/code_terminator/html.rb', line 152 def validate_syntax(code) errors = Array.new begin Nokogiri::XML(code) { |config| config.strict } #validate if html follow w3, uncomment when check all the page #"<!DOCTYPE html> # <html> # <head> # <h1>asdasd</h1> # <title>asdasd</title> # </head> # <body> # <h1>hola</h1> # </body> # </html>" # @validator = Html5Validator::Validator.new # @validator.validate_text(@html) rescue Nokogiri::XML::SyntaxError => e #errors[0] = "Check if you close your tags" errors[0] = e end errors end |