Commit 85460d01 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor HTML escaping methods to helper class.

Created JsDuck::HTML class which implements #strip_tags and also proxies
CGI methods #escapeHTML and #unescapeHTML for more unified interface.
parent 8ab351a5
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -2,10 +2,10 @@
require 'rubygems'
require 'rdiscount'
require 'strscan'
require 'cgi'
require 'jsduck/logger'
require 'jsduck/inline_img'
require 'jsduck/inline_video'
require 'jsduck/html'

module JsDuck

@@ -274,7 +274,7 @@ module JsDuck
        when '%-'
          member ? "-" : ""
        when '%a'
          CGI.escapeHTML(anchor_text||"")
          HTML.escape(anchor_text||"")
        else
          $1
        end
@@ -327,7 +327,7 @@ module JsDuck
    #   Blah blah blah some text.
    #
    def shorten(input)
      sent = first_sentence(strip_tags(input))
      sent = first_sentence(HTML.strip_tags(input).strip)
      # Use u-modifier to correctly count multi-byte characters
      chars = sent.scan(/./mu)
      if chars.length > @max_length
@@ -343,15 +343,12 @@ module JsDuck

    # Returns true when input should get shortened.
    def too_long?(input)
      stripped = strip_tags(input)
      stripped = HTML.strip_tags(input).strip
      # for sentence v/s full - compare byte length
      # for full v/s max - compare char length
      first_sentence(stripped).length < stripped.length || stripped.scan(/./mu).length > @max_length
    end

    def strip_tags(str)
      str.gsub(/<.*?>/, "").strip
    end
  end

end
+2 −4
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ require 'jsduck/io'
require 'jsduck/null_object'
require 'jsduck/logger'
require 'jsduck/grouped_asset'
require 'jsduck/html'
require 'fileutils'

module JsDuck
@@ -111,7 +112,7 @@ module JsDuck
      html.each_line do |line|
        if line =~ /^<h2>(.*)<\/h2>$/
          i += 1
          text = strip_tags($1)
          text = HTML.strip_tags($1)
          toc << "<li><a href='#!/guide/#{guide['name']}-section-#{i}'>#{text}</a></li>\n"
          new_html << "<h2 id='#{guide['name']}-section-#{i}'>#{text}</h2>\n"
        else
@@ -152,9 +153,6 @@ module JsDuck
      "guides/" + guide["name"] + "/icon.png"
    end

    def strip_tags(str)
      str.gsub(/<.*?>/, "")
    end
  end

end

lib/jsduck/html.rb

0 → 100644
+25 −0
Original line number Diff line number Diff line
require 'cgi'

module JsDuck

  # Helpers for dealing with HTML
  class HTML

    # Strips tags from HTML text
    def self.strip_tags(html)
      html.gsub(/<.*?>/, "")
    end

    # Escapes HTML, replacing < with &lt; ...
    def self.escape(html)
      CGI.escapeHTML(html)
    end

    # Unescapes HTML, replacing &lt; with < ...
    def self.unescape(html)
      CGI.unescapeHTML(html)
    end

  end

end
+2 −5
Original line number Diff line number Diff line
require 'jsduck/json_duck'
require 'cgi'
require 'jsduck/html'

module JsDuck

@@ -65,7 +65,7 @@ module JsDuck
            ex = s.scan_until(@end_example_re).sub(@end_example_re, '')

            examples << {
              :code => CGI.unescapeHTML(strip_tags(ex)),
              :code => HTML.unescape(HTML.strip_tags(ex)),
              :options => options,
            }
          else
@@ -89,9 +89,6 @@ module JsDuck
      hash
    end

    def strip_tags(str)
      str.gsub(/<.*?>/, "")
    end
  end

end
+2 −2
Original line number Diff line number Diff line
require 'cgi'
require 'jsduck/html'
require 'jsduck/logger'

module JsDuck
@@ -42,7 +42,7 @@ module JsDuck
        when '%u'
          @base_path ? (@base_path + "/" + url) : url
        when '%a'
          CGI.escapeHTML(alt_text||"")
          HTML.escape(alt_text||"")
        else
          $1
        end
Loading