Class: CodeTerminator::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/code_terminator/html.rb

Instance Method Summary collapse

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
# File 'lib/code_terminator/html.rb', line 49

def get_elements(source)
  @elements = Array.new
  if @source_type == "url"
    reader = Nokogiri::HTML(open(source).read)
  else
    reader = Nokogiri::HTML(File.open(source))
  end
  reader = remove_empty_text(reader)
  reader.at('body').attribute_nodes.each do |element_attribute|
    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

  if !reader.at('head').nil?
  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?
     @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

  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?
     @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
  @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)


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/code_terminator/html.rb', line 214

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?
    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)


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
# File 'lib/code_terminator/html.rb', line 249

def match(source, code)
  html_errors = Array.new

  code = Nokogiri::HTML(code)

  elements = get_elements(source)

  elements.each do |e|
    item = e[:tag]

    if item=="text"

     #  Check the text
      if !e[:content].nil?
        if code.css(e[:parent]).count < 2
          if code.css(e[:parent]).text != e[:content]
            html_errors << new_error(element: e, type: 330, description: "The text inside #{e[:parent]} should be #{e[:content]}.")
          end
        else
          exist = false
          code.css(e[:parent]).each do |code_css|
            #if code_css.at_css(e[:tag]).parent.name == e[:parent]
              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

    else
    if code.css(e[:tag]).length > 0

      if !e[:attribute].nil?
       #  Check the tag's attributes
        if code.css(e[: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 code.css(e[:tag]).attribute(e[:attribute]).value != e[:value]
            html_errors << new_error(element: e, type: 333, description: "Make sure that the attribute #{e[:attribute]} in #{e[:tag]} has the value #{e[:value]}.")
          end
        end
      end

     #  Check that tags exist within parent tags
      if code.css(e[:tag]).count < 2
        if code.css(e[: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
        code.css(e[: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

    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

   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

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)


192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/code_terminator/html.rb', line 192

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)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/code_terminator/html.rb', line 161

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)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/code_terminator/html.rb', line 124

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