Module: Bridgetown::Site::Writable

Included in:
Bridgetown::Site
Defined in:
bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb

Instance Method Summary collapse

Instance Method Details

#cleanupvoid

This method returns an undefined value.

Remove orphaned files and empty directories in destination.



8
9
10
# File 'bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb', line 8

def cleanup
  @cleaner.cleanup!
end

#each_site_file {|item| ... } ⇒ void

This method returns an undefined value.

Yields all content objects while looping through Bridgetown::Site#generated_pages, Content#static_files_to_write, Content#resources_to_write.



28
29
30
31
32
33
34
# File 'bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb', line 28

def each_site_file
  %w(generated_pages static_files_to_write resources_to_write).each do |type|
    send(type).each do |item| # rubocop:disable Style/ExplicitBlockArgument
      yield item
    end
  end
end

#resources_cache_manifestObject



36
37
38
39
40
41
42
43
44
# File 'bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb', line 36

def resources_cache_manifest
  resources.each_with_object({}) do |resource, hsh|
    next if resource.relative_url == ""

    hsh[resource.relative_url] = {
      id: resource.model.id,
    }
  end
end

#writevoid

This method returns an undefined value.

Write static files, pages, and documents to the destination folder.



15
16
17
18
19
20
# File 'bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb', line 15

def write
  each_site_file { |item| item.write(dest) }
  write_redirecting_index if config.prefix_default_locale

  Bridgetown::Hooks.trigger :site, :post_write, self
end

#write_redirecting_indexObject



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
# File 'bridgetown-core/lib/bridgetown-core/concerns/site/writable.rb', line 46

def write_redirecting_index
  resource = resources.find do |item|
    item.data.slug == "index" && item.data.locale == config.default_locale
  end

  unless resource
    Bridgetown.logger.warn(
      "Index file not found in the source folder, cannot generate top-level redirect file"
    )
    return
  end

  index_html = <<~HTML # rubocop:disable Bridgetown/InsecureHeredoc
    <!DOCTYPE html>
    <html>
      <head>
        <title>Redirecting…</title>
        <meta http-equiv="refresh" content="0; url=#{resource.relative_url}" />
      </head>
      <body></body>
    </html>
  HTML

  File.write(in_dest_dir("index.html"), index_html, mode: "wb")
end