Module: Parklife::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/parklife/utils.rb

Instance Method Summary collapse

Instance Method Details

#build_path_for(path, index: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/parklife/utils.rb', line 10

def build_path_for(path, index: true)
  path = path.gsub(/^\/|\/$/, '')

  if File.extname(path).empty?
    if path.empty?
      'index.html'
    elsif index
      File.join(path, 'index.html')
    else
      "#{path}.html"
    end
  else
    path
  end
end

#host_with_port(uri) ⇒ Object



26
27
28
29
# File 'lib/parklife/utils.rb', line 26

def host_with_port(uri)
  default_port = uri.scheme == 'https' ? 443 : 80
  uri.port == default_port ? uri.host : "#{uri.host}:#{uri.port}"
end

#save_page(path, content, config) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/parklife/utils.rb', line 31

def save_page(path, content, config)
  build_path = File.join(
    config.build_dir,
    build_path_for(path, index: config.nested_index)
  )
  FileUtils.mkdir_p(File.dirname(build_path))
  File.write(build_path, content, mode: 'wb')
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/parklife/utils.rb', line 40

def scan_for_links(html)
  doc = Nokogiri::HTML.parse(html)
  doc.css('a[href]').each do |a|
    uri = URI.parse(a[:href])

    # Don't visit a URL that belongs to a different domain - for now this is
    # a guess that it's not an internal link but it also covers mailto/ftp
    # links.
    next if uri.host

    # Don't visit a path-less URL - this will be the case for a #fragment
    # for example.
    next if uri.path.nil? || uri.path.empty?

    yield uri.path
  end
end