Module: Trifle::Docs::Helper::Sitemap

Defined in:
lib/trifle/docs/helper/sitemap.rb

Class Method Summary collapse

Class Method Details

.absolute_url?(loc) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/trifle/docs/helper/sitemap.rb', line 80

def absolute_url?(loc)
  loc.to_s.match?(%r{\Ahttps?://}i)
end

.absolutize_loc(loc, base_url) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/trifle/docs/helper/sitemap.rb', line 71

def absolutize_loc(loc, base_url)
  return loc if base_url.nil? || absolute_url?(loc)

  path = loc.to_s.gsub(%r{^/+}, '')
  return "#{base_url}/" if path.empty?

  "#{base_url}/#{path}"
end

.build_document(urls) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/trifle/docs/helper/sitemap.rb', line 46

def build_document(urls)
  [
    %(<?xml version="1.0" encoding="UTF-8"?>),
    %(<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">),
    urls.join("\n"),
    %(</urlset>)
  ].join("\n")
end

.build_url_entry(url, meta, base_url: nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/trifle/docs/helper/sitemap.rb', line 55

def build_url_entry(url, meta, base_url: nil)
  loc = normalize_loc(url, meta, base_url: base_url)
  lastmod = format_lastmod(meta['updated_at'])
  lastmod_tag = lastmod ? "<lastmod>#{lastmod}</lastmod>" : ''

  "<url><loc>#{loc}</loc>#{lastmod_tag}</url>"
end

.configuration_base_url(config) ⇒ Object



88
89
90
91
92
# File 'lib/trifle/docs/helper/sitemap.rb', line 88

def configuration_base_url(config)
  return nil unless config.respond_to?(:sitemap_base_url)

  config.sitemap_base_url
end

.flatten_sitemap(node, path = []) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/trifle/docs/helper/sitemap.rb', line 22

def flatten_sitemap(node, path = [])
  return [] unless node.is_a?(Hash)

  entries = []
  meta = node['_meta']
  entries << { url: path.join('/'), meta: meta } if meta

  node.keys.reject { |key| key == '_meta' }.sort.each do |key|
    entries.concat(flatten_sitemap(node[key], path + [key]))
  end

  entries
end

.format_lastmod(value) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/trifle/docs/helper/sitemap.rb', line 108

def format_lastmod(value)
  return nil if value.nil?

  return value.utc.iso8601 if value.respond_to?(:utc) && value.respond_to?(:iso8601)
  return value.iso8601 if value.respond_to?(:iso8601)

  nil
end

.normalize_base_url(base_url) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/trifle/docs/helper/sitemap.rb', line 94

def normalize_base_url(base_url)
  value = base_url.to_s.strip
  return nil if value.empty?

  uri = URI.parse(value)
  return nil unless uri.is_a?(URI::HTTP) && uri.host

  uri.query = nil
  uri.fragment = nil
  uri.to_s.gsub(%r{/+$}, '')
rescue URI::InvalidURIError
  nil
end

.normalize_loc(url, meta, base_url: nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/trifle/docs/helper/sitemap.rb', line 63

def normalize_loc(url, meta, base_url: nil)
  loc = meta['url']
  loc = "/#{url}" if loc.nil? || loc.empty?
  loc = '/' if loc == '//'
  loc = absolutize_loc(loc, base_url)
  CGI.escapeHTML(loc)
end

.resolve_base_url(config:, base_url:) ⇒ Object



84
85
86
# File 'lib/trifle/docs/helper/sitemap.rb', line 84

def resolve_base_url(config:, base_url:)
  normalize_base_url(base_url) || normalize_base_url(configuration_base_url(config))
end

.sitemap_urls(sitemap, base_url: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/trifle/docs/helper/sitemap.rb', line 36

def sitemap_urls(sitemap, base_url: nil)
  entries = flatten_sitemap(sitemap)
  entries.filter_map do |entry|
    meta = entry[:meta]
    next if meta.nil? || meta['type'] == 'file'

    build_url_entry(entry[:url], meta, base_url: base_url)
  end
end

.xml(config: nil, base_url: nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/trifle/docs/helper/sitemap.rb', line 13

def xml(config: nil, base_url: nil)
  sitemap = Trifle::Docs.sitemap(config: config)
  resolved_base_url = resolve_base_url(config: config, base_url: base_url)
  urls = sitemap_urls(sitemap, base_url: resolved_base_url)
  return nil if urls.empty?

  build_document(urls)
end