File: pages.rb

package info (click to toggle)
ruby-jekyll-github-metadata 2.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ruby: 2,355; javascript: 107; sh: 41; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 2,977 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
# frozen_string_literal: true

module Jekyll
  module GitHubMetadata
    class Pages
      class << self
        DEFAULTS = {
          "PAGES_ENV"              => "development",
          "PAGES_API_URL"          => "https://api.github.com",
          "PAGES_HELP_URL"         => "https://help.github.com",
          "PAGES_GITHUB_HOSTNAME"  => "github.com",
          "PAGES_PAGES_HOSTNAME"   => "github.io",
          "SSL"                    => "false",
          "SUBDOMAIN_ISOLATION"    => "false",
          "PAGES_PREVIEW_HTML_URL" => nil,
          "PAGE_BUILD_ID"          => nil,
        }.freeze

        # Whether the GitHub instance supports HTTPS
        # Note: this will be the same as how sites are served in Enterprise,
        # but may be different from how sites are served on GitHub.com.
        # See Repository#url_scheme
        def ssl?
          env_var("SSL") == "true" || test?
        end

        def scheme
          ssl? ? "https" : "http"
        end

        def subdomain_isolation?
          env_var("SUBDOMAIN_ISOLATION").eql? "true"
        end

        def test?
          env == "test"
        end

        def dotcom?
          env == "dotcom"
        end

        def enterprise?
          env == "enterprise"
        end

        def development?
          env == "development"
        end

        def custom_domains_enabled?
          dotcom? || test?
        end

        def env
          env_var "PAGES_ENV", ENV["JEKYLL_ENV"]
        end

        def repo_pages_html_url_preview?
          env_var "PAGES_PREVIEW_HTML_URL"
        end

        def github_url
          if dotcom? || github_hostname == "github.com"
            "https://github.com"
          else
            "#{scheme}://#{github_hostname}"
          end
        end

        def api_url
          trim_last_slash env_var("PAGES_API_URL", ENV["API_URL"])
        end

        def help_url
          trim_last_slash env_var("PAGES_HELP_URL", ENV["HELP_URL"])
        end

        def github_hostname
          trim_last_slash env_var("PAGES_GITHUB_HOSTNAME", ENV["GITHUB_HOSTNAME"])
        end

        def pages_hostname
          intermediate_default = ENV["PAGES_HOSTNAME"]
          intermediate_default ||= "localhost:4000" if development?
          trim_last_slash env_var("PAGES_PAGES_HOSTNAME", intermediate_default)
        end

        def page_build?
          !env_var("PAGE_BUILD_ID").to_s.empty?
        end

        def configuration
          (methods - Object.methods - [:configuration]).sort.each_with_object({}) do |meth, memo|
            memo[meth.to_s] = public_send(meth)
          end
        end

        private

        def env_var(key, intermediate_default = nil)
          !ENV[key].to_s.empty? ? ENV[key] : (intermediate_default || DEFAULTS[key])
        end

        def trim_last_slash(url)
          if url[-1] == "/"
            url[0..-2]
          else
            url
          end
        end
      end
    end
  end
end