File: https_filter.rb

package info (click to toggle)
ruby-html-pipeline 2.14.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 2,265; sh: 13; makefile: 6
file content (29 lines) | stat: -rw-r--r-- 762 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

module HTML
  class Pipeline
    # HTML Filter for replacing http references to :http_url with https versions.
    # Subdomain references are not rewritten.
    #
    # Context options:
    #   :http_url - The HTTP url to force HTTPS. Falls back to :base_url
    class HttpsFilter < Filter
      def call
        doc.css(%(a[href^="#{http_url}"])).each do |element|
          element['href'] = element['href'].sub(/^http:/, 'https:')
        end
        doc
      end

      # HTTP url to replace. Falls back to :base_url
      def http_url
        context[:http_url] || context[:base_url]
      end

      # Raise error if :http_url undefined
      def validate
        needs :http_url unless http_url
      end
    end
  end
end