Sha256: 5113f33789433eaebfa8b7b88376c81bf9521263f5490a56fceda74be136e8c5

Contents?: true

Size: 1.16 KB

Versions: 24

Compression:

Stored size: 1.16 KB

Contents

require 'cgi'

module JsDuck

  # Transforms in-page links so they won't break docs app #!-navigation.
  #
  # For example a link to "#automation" in testing guide will be
  # replaced with "#!/guide/testing-section-automation" and the link
  # target ID will be transformed into "testing-section-automation".
  class GuideAnchors

    def self.transform(html, guide_name)
      html.gsub(/(<a\s+(?:[^<>]*\s+)?href=['"]#)([^!\/].*?)(['"])/i) do |m|
        $1 + "!/guide/" + transform_id($2, guide_name) + $3

      end.gsub(/(<a\s+(?:[^<>]*\s+)?name=['"])(.*?)(['"])/i) do |m|
        $1 + transform_id($2, guide_name) + $3

      end.gsub(/(<\w+\s+(?:[^<>]*\s+)?id=['"])(.*?)(['"])/i) do |m|
        $1 + transform_id($2, guide_name) + $3
      end
    end

    def self.transform_id(id, guide_name)
      if id =~ /^#{guide_name}-section-/
        id
      else
        # Escape the ID if it's not already escaped.  This check is
        # needed to avoid re-escaping anchor-links created with
        # Markdown - these get auto-escaped by RDiscount.
        id = (id =~ /%[0-9A-F]{2}/) ? id : CGI::escape(id)

        "#{guide_name}-section-#{id}"
      end
    end

  end

end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/guide_anchors.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/guide_anchors.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/guide_anchors.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/guide_anchors.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/guide_anchors.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/guide_anchors.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.10 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.9 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.8 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.7 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.5 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.4 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.3 lib/jsduck/guide_anchors.rb
jsduck-troopjs-0.0.1 lib/jsduck/guide_anchors.rb
jsduck-6.0.0beta lib/jsduck/guide_anchors.rb
jsduck-5.3.4 lib/jsduck/guide_anchors.rb
jsduck-5.3.3 lib/jsduck/guide_anchors.rb
jsduck-5.3.2 lib/jsduck/guide_anchors.rb
jsduck-5.3.1 lib/jsduck/guide_anchors.rb