module Clayoven::Toplevel

The toplevel module for clayoven, which contains main

Public Class Methods

dirty_pages(index_files, content_files, is_aggressive) click to toggle source

Return Array of IndexPage and ContentPage entries to render

# File lib/clayoven/toplevel.rb, line 179
def self.dirty_pages(index_files, content_files, is_aggressive)
  # Adding a new index file is equivalent to re-rendering the entire site
  if @git.any_added?(index_files) || @git.template_changed? || is_aggressive
    dirty_pages_from_add(index_files, content_files)
  else
    dirty_pages_from_mod(index_files, content_files)
  end
end
dirty_pages_from_add(index_files, content_files) click to toggle source

Simply create IndexPage and ContentPage entries out of index_files and content_files.

# File lib/clayoven/toplevel.rb, line 134
def self.dirty_pages_from_add(index_files, content_files)
  progress = ProgressBar.create(title: "[#{'GIT'.green} ]", total: index_files.count + content_files.count)

  # Strightforward
  dirty_index_pages = index_files.map { |filename| progress.increment; IndexPage.new filename, @git }
  dirty_content_pages = content_files.map { |filename| progress.increment; ContentPage.new filename, @git }
  [dirty_index_pages, dirty_content_pages]
end
dirty_pages_from_mod(index_files, content_files) click to toggle source

Find the dirty IndexPage and ContentPage entries from some modification that occured in the git index, based on index_files, and content_files.

Returns an Array of IndexPage

# File lib/clayoven/toplevel.rb, line 166
def self.dirty_pages_from_mod(index_files, content_files)
  # Create a progressbar based on information from the git index
  modified_index_files, modified_content_files = modified_files_from_gitidx(index_files, content_files)
  progress = ProgressBar.create(title: "[#{'GIT'.green} ]",
                                total: modified_index_files.count + modified_content_files.count * 2)

  # Find out the dirty content_pages
  dirty_content_pages = modified_content_files.map { |filename| progress.increment; ContentPage.new filename, @git }

  [find_dirty_index_pages(dirty_content_pages, modified_index_files, progress), dirty_content_pages]
end
find_dirty_index_pages(dirty_content_pages, modified_index_files, progress) click to toggle source

Find the dirty IndexPage entries from dirty ContentPage entries and modified_index_files. First, see which modified_index_files are forced dirty by corresponding dirty_content_pages; then, add to the list the ones that are dirty by themselves.

Returns an Array of IndexPage

# File lib/clayoven/toplevel.rb, line 154
def self.find_dirty_index_pages(dirty_content_pages, modified_index_files, progress)
  # Avoid adding the
  # index page twice when there are two dirty content_pages under the same index
  dirty_from_content = dirty_content_pages.map { |dcp| "#{dcp.topic}.index.clay" }.uniq
                                          .map { |dif| progress.increment; IndexPage.new dif, @git }
  dirty_from_content + modified_index_files.map { |filename| progress.increment; IndexPage.new filename, @git }
end
find_topics(index_files) click to toggle source

Find the list of topics to be exposed as Page#topics.

# File lib/clayoven/toplevel.rb, line 210
def self.find_topics(index_files)
  all_topics = Util.lex_sort(index_files).map { |file| file.split('.index.clay').first }
  topics = all_topics.reject do |entry|
    @config.hidden.any? { |hidden_entry| hidden_entry == entry }
  end
  [all_topics, topics]
end
generate_html(genpages, topics) click to toggle source

Produce HTML files, first using Page#render, and then operating on the files produced in-place with MathJaX

# File lib/clayoven/toplevel.rb, line 248
def self.generate_html(genpages, topics)
  progress = ProgressBar.create(title: "[#{'CLAY'.green}]", total: genpages.length)
  genpages.each { |page| page.render topics, @config.template; progress.increment }
  Util.render_math genpages.map(&:target).join(' ')
end
generate_site(genpages, topics, is_aggressive) click to toggle source

Generate HTML, minify the design, and generate the sitemap.

# File lib/clayoven/toplevel.rb, line 278
def self.generate_site(genpages, topics, is_aggressive)
  generate_html genpages, topics if genpages.any?
  Util.minify_design if @git.design_changed? || is_aggressive
  generate_sitemap genpages unless genpages.any?
end
generate_sitemap(all_pages) click to toggle source

Generate sitemap.xml from all_pages.

# File lib/clayoven/toplevel.rb, line 264
def self.generate_sitemap(all_pages)
  puts "[#{'XML'.green} ]: Generating sitemap"
  SitemapGenerator.verbose = false
  SitemapGenerator::Sitemap.include_root = false
  SitemapGenerator::Sitemap.compress = false
  SitemapGenerator::Sitemap.default_host = "https://#{@config.sitename}"
  SitemapGenerator::Sitemap.public_path = '.'
  SitemapGenerator::Sitemap.create do
    add '/', lastmod: Clayoven::Toplevel.sitewide_lastmod(all_pages), priority: 1.0, changefreq: 'always'
    all_pages.each { |p| add p.permalink, lastmod: p.lastmod }
  end
end
index_content_files(all_files) click to toggle source

From all_files, find out the list of index_files, content_files, and topics, and return them.

Returns an Array of three different Array of String.

# File lib/clayoven/toplevel.rb, line 229
def self.index_content_files(all_files)
  index_files, content_files = separate_index_content_files all_files
  all_topics, topics = find_topics index_files

  # Look for stray files.  All content_files are nested within directories
  # We look in `all_topics`, because we still want hidden content_files to be
  # generated, just not shown.
  content_files
    .reject { |file| all_topics.include? file.split('/').first }
    .each do |stray|
    content_files -= [stray]
    puts "[#{'WARN'.yellow} ]: #{stray} is a stray file or directory; ignored"
  end

  [index_files, content_files, topics]
end
main(is_aggressive: false) click to toggle source

The entry point for clayoven, and clayoven aggressive.

# File lib/clayoven/toplevel.rb, line 285
def self.main(is_aggressive: false)
  # Only operate on git repositories
  toplevel = `git rev-parse --show-toplevel`.strip
  if toplevel.empty? || (!File.directory? "#{toplevel}/.clayoven")
    abort "[#{'ERR'.red} ]: Not a clayoven project (have you run `clayoven init`?)"
  end
  Dir.chdir(toplevel) do
    # Write out template files, if necessary
    @config = Clayoven::Config.new

    # Initialize git
    @git = Clayoven::Git.new @config.tzmap

    # Collect the list of files from a directory listing
    all_files = Util.ls_files

    # From all_files, get the list of index_files, content_files, and topics
    index_files, content_files, topics = index_content_files all_files

    # Get a list of pages to render, genpages
    genpages = pages_to_render index_files, content_files, is_aggressive

    # If the template changes, we're definitely in aggressive mode
    is_aggressive ||= @git.template_changed?

    # Generate the genpages
    generate_site genpages, topics, is_aggressive
  end
end
modified_files_from_gitidx(index_files, content_files) click to toggle source

Find the modified index_files and added_or_modified content_files from the git index

# File lib/clayoven/toplevel.rb, line 144
def self.modified_files_from_gitidx(index_files, content_files)
  # The case when index_files are added is handled in dirty_pages
  [index_files.select { |f| @git.modified? f }, content_files.select { |f| @git.added_or_modified? f }]
end
pages_to_render(index_files, content_files, is_aggressive) click to toggle source

Return IndexPage and ContentPage entries to render; we work with index_files and content_files, because converting them to Page objects prematurely will result in unnecessary log --follow invocations

# File lib/clayoven/toplevel.rb, line 197
def self.pages_to_render(index_files, content_files, is_aggressive)
  dirty_index_pages, dirty_content_pages = dirty_pages index_files, content_files, is_aggressive

  # Reject hidden content_files
  dirty_index_pages.each do |dip|
    content_pages = unhidden_content_files(content_files).select { |cf| cf.split('/', 2).first == dip.permalink }
                                                         .map { |cf| ContentPage.new cf, @git }
    dip.fillindex content_pages, @config.stmap
  end
  dirty_index_pages + dirty_content_pages
end
separate_index_content_files(all_files) click to toggle source

Separate out index_files and content_files from all_files based on filename

# File lib/clayoven/toplevel.rb, line 219
def self.separate_index_content_files(all_files)
  # index_files are files ending in '.index.clay' and 'index.clay'
  # content_files are all other files; topics is the list of topics: we need it for the sidebar
  index_files = ['index.clay'] + all_files.select { |file| /\.index\.clay$/ =~ file }
  [index_files, all_files - index_files]
end
sitewide_lastmod(all_pages) click to toggle source

For the sitemap root entry, find the maximal Page#lastmod of IndexPage entries in all_pages.

Returns a Time object.

# File lib/clayoven/toplevel.rb, line 257
def self.sitewide_lastmod(all_pages)
  all_pages.select do |p|
    p.instance_of? IndexPage
  end.map(&:lastmod).max
end
unhidden_content_files(content_files) click to toggle source

Reject the content_files that match Clayoven::Config#hidden

# File lib/clayoven/toplevel.rb, line 189
def self.unhidden_content_files(content_files)
  content_files.reject do |cf|
    @config.hidden.any? { |hidden_entry| "#{hidden_entry}.clay" == cf }
  end
end