module Clayoven::Toplevel
The toplevel module for clayoven, which contains main
Public Class Methods
Return Array
of IndexPage
and ContentPage
entries to render
# File lib/clayoven/toplevel.rb, line 234 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
Simply create IndexPage
and ContentPage
entries out of index_files and content_files.
# File lib/clayoven/toplevel.rb, line 145 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 do |filename| progress.increment IndexPage.new filename, @git end dirty_content_pages = content_files.map do |filename| progress.increment ContentPage.new filename, @git end [dirty_index_pages, dirty_content_pages] end
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 206 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 do |filename| progress.increment ContentPage.new filename, @git end [ find_dirty_index_pages( dirty_content_pages, modified_index_files, progress ), dirty_content_pages ] end
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 180 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 do |dif| progress.increment IndexPage.new dif, @git end dirty_from_content + modified_index_files.map do |filename| progress.increment IndexPage.new filename, @git end end
Find the list of topics to be exposed as Page#topics
.
# File lib/clayoven/toplevel.rb, line 268 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
Produce HTML files, first using Page#render
, and then operating on the files produced in-place with MathJaX
# File lib/clayoven/toplevel.rb, line 311 def self.generate_html(genpages, topics) progress = ProgressBar.create(title: "[#{"CLAY".green}]", total: genpages.length) genpages.each do |page| page.render topics, @config.template progress.increment end Util.render_math genpages.map(&:target).join(" ") end
Generate HTML, minify the design, and generate the sitemap.
# File lib/clayoven/toplevel.rb, line 346 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 if is_aggressive end
Generate sitemap.xml
from all_pages.
# File lib/clayoven/toplevel.rb, line 329 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
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 292 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] warn "[#{"WARN".yellow} ]: #{stray} is a stray file or directory; ignored" end [index_files, content_files, topics] end
The entry point for clayoven
, and clayoven aggressive
.
# File lib/clayoven/toplevel.rb, line 353 def self.main(is_aggressive: false) # Only operate on git repositories toplevel = Git.toplevel if $? != 0 || 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 # If the template changes, we're definitely in aggressive mode is_aggressive ||= @git.template_changed? # Get a list of pages to render, genpages genpages = pages_to_render index_files, content_files, is_aggressive # Generate the genpages generate_site genpages, topics, is_aggressive end end
Find the modified index_files and added_or_modified content_files from the git index
# File lib/clayoven/toplevel.rb, line 167 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
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 252 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 out index_files
and content_files
from all_files
based on filename
# File lib/clayoven/toplevel.rb, line 281 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
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 324 def self.sitewide_lastmod(all_pages) all_pages.select { |p| p.instance_of? IndexPage }.map(&:lastmod).max end