From 5022d1a0df1534b1cbec1cbee23568f201ea1cc6 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 9 Jan 2017 16:45:53 -0500 Subject: Implement actual file generation, go through fixing things. --- .gitignore | 2 -- lib/category.rb | 2 +- lib/page_index.rb | 14 ++++++++++++++ lib/page_local.rb | 11 ++++++----- lib/sitegen.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- tmpl/index.atom.erb | 10 +++++----- tmpl/page.atom.erb | 19 ++++++++++--------- tmpl/page.html.erb | 2 +- 8 files changed, 85 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 6883828..a7d4477 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ /out/ -/www/ .tmp* -.var* diff --git a/lib/category.rb b/lib/category.rb index 266da34..b60c82e 100644 --- a/lib/category.rb +++ b/lib/category.rb @@ -15,6 +15,6 @@ class Category return "#{name}" end def atom - return "" + return "" end end diff --git a/lib/page_index.rb b/lib/page_index.rb index 178525c..fed7e5f 100644 --- a/lib/page_index.rb +++ b/lib/page_index.rb @@ -41,6 +41,17 @@ class IndexPage < LocalPage end @pages end + def index_pages_leaves + ret = Set[] + index_pages.each do |page| + if page.is_a?(IndexPage) + ret.merge(page.index_pages) + else + ret.add(page) + end + end + return ret + end def index_link(cururl, depth) ret = '' @@ -61,6 +72,9 @@ class IndexPage < LocalPage _metadata['title'] end + def local_intype + return 'markdown' + end def local_outfile local_infile.sub(/^src/, 'out')+"/index.html" end diff --git a/lib/page_local.rb b/lib/page_local.rb index ca3aa31..da8689d 100644 --- a/lib/page_local.rb +++ b/lib/page_local.rb @@ -21,15 +21,16 @@ class LocalPage < Page def local_infile ; @infile ; end def local_input ; @input ||= File::read(local_infile); end - def _pandoc - if @pandoc.nil? + def local_intype types = { 'md' => 'markdown' } - ext = File::extname(local_infile).gsub(/^[.]/, '') - type = types[ext] || ext - @pandoc = Pandoc::load(type, local_input) + return types[ext] || ext + end + def _pandoc + if @pandoc.nil? + @pandoc = Pandoc::load(local_intype, local_input) if @pandoc['pandoc_format'] @pandoc = Pandoc::load(@pandoc['pandoc_format'], local_input) diff --git a/lib/sitegen.rb b/lib/sitegen.rb index c6e89e9..765cfbd 100644 --- a/lib/sitegen.rb +++ b/lib/sitegen.rb @@ -1,5 +1,6 @@ # coding: utf-8 require 'date' +require 'fileutils' require 'set' module Sitegen @@ -60,6 +61,52 @@ module Sitegen end def self.generate(target) - # TODO + case + when @mk[target].nil? + raise "No rule to make target '#{target}'. Stop." + when target.end_with?(".atom") + write_ifchanged(target) do |file| + file.puts('') + file.print(@mk[target].atom) + end + when target.end_with?(".html") + write_ifchanged(target) do |file| + file.print(@mk[target].html) + end + else + raise "No rule to make target '#{target}'. Stop." + end + end + + def self.write_ifchanged(outfilename) + tmpfilename = "#{File::dirname(outfilename)}/.tmp#{File::basename(outfilename)}" + + # Write our stuff to tmpfile + FileUtils::mkdir_p(File::dirname(tmpfilename)) + tmpfile = File::new(tmpfilename, 'wb') + begin + yield tmpfile + rescue Exception => e + tmpfile.close + File::unlink(tmpfilename) + raise e + end + tmpfile.close + + # Now see if we should replace outfile with tmpfile + same = false + begin + if FileUtils::compare_file(tmpfilename, outfilename) + same = true + end + rescue Errno::ENOENT + end + + # And actually do so + if same + File::unlink(tmpfilename) + else + File::rename(tmpfilename, outfilename) + end end end diff --git a/tmpl/index.atom.erb b/tmpl/index.atom.erb index 54e4b72..8719d05 100644 --- a/tmpl/index.atom.erb +++ b/tmpl/index.atom.erb @@ -1,12 +1,12 @@ - AndrewDM.me <%= @title %> + <%= atom_title %> - <%= @pages.map{|p|p.updated}.sort.last.rfc3339 %> - <%= Person.new("Andrew Murrell").atom %> - <%= $url %> + <%= index_pages_leaves.map{|p|p.atom_updated}.sort.last.to_datetime.rfc3339 %> + <%= Person.new(Config::get.default_author).atom %> + <%= url %> - <% @pages.sort_by{|p| p.updated}.reverse.each do |page| %><%=@page.atom %><% end %> + <% index_pages_leaves.sort_by{|p|p.atom_updated}.reverse.each do |page| %><%= page.atom %><% end %> diff --git a/tmpl/page.atom.erb b/tmpl/page.atom.erb index ea37ea5..d690974 100644 --- a/tmpl/page.atom.erb +++ b/tmpl/page.atom.erb @@ -1,10 +1,11 @@ - - - <%= page.url %> - <%= page.updated.rfc3339 %> - <%= page.published.rfc3339 %> - <%= page.title %> - <%= page.author.atom %> - <% if page.content %><%= html_escape(page.content) %><% end %> - <% if page.rights %><%= html_escape(page.rights) %><% end %> +<% require 'siteutil' %> + + <%= url %> + <%= atom_updated.to_datetime.rfc3339 %> + <%= atom_published.to_datetime.rfc3339 %> + <%= atom_title %> + <%= atom_author.atom %> + <% atom_categories.each do |c| %><%= c.atom %><% end %> + <% if atom_content %><%= SiteUtil::html_escape(atom_content) %><% end %> + <% if atom_rights %><%= SiteUtil::html_escape(atom_rights) %><% end %> diff --git a/tmpl/page.html.erb b/tmpl/page.html.erb index dae08eb..fb5ddaa 100644 --- a/tmpl/page.html.erb +++ b/tmpl/page.html.erb @@ -29,7 +29,7 @@ -- cgit v1.2.3