diff options
author | Andrew Murrell <merl@neo.andrewdm.me> | 2017-12-26 08:43:24 +0000 |
---|---|---|
committer | Andrew Murrell <merl@neo.andrewdm.me> | 2017-12-26 08:43:24 +0000 |
commit | bc78567716a106042356053d556f57c1ddc13cfc (patch) | |
tree | 0c008bddb47699a9dcd62cdeb2e7131a137f64ee /lib | |
parent | db410594baa4bb507aaa23ff98bf8853056b0823 (diff) | |
parent | 50830151e8168b8be0100da349ba7e3576054dc3 (diff) |
Merge branch 'master' of https://git.andrewdm.me/www
Diffstat (limited to 'lib')
-rw-r--r-- | lib/category.rb | 2 | ||||
-rw-r--r-- | lib/config.rb | 12 | ||||
-rw-r--r-- | lib/page.rb | 4 | ||||
-rw-r--r-- | lib/page_index.rb | 13 | ||||
-rw-r--r-- | lib/page_local.rb | 15 | ||||
-rw-r--r-- | lib/page_pdf.rb | 62 | ||||
-rw-r--r-- | lib/sitegen.rb | 2 |
7 files changed, 93 insertions, 17 deletions
diff --git a/lib/category.rb b/lib/category.rb index e0ed8e0..8f14153 100644 --- a/lib/category.rb +++ b/lib/category.rb @@ -9,7 +9,7 @@ class Category @abbr.downcase end def name - Config::get.category_name(@abbr) + Config::get.category_name(abbr) end def html return "<a class=\"tag #{abbr}\" href=\"/tags/#{abbr}.html\">#{name}</a>" diff --git a/lib/config.rb b/lib/config.rb index 4690559..944acc3 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -21,11 +21,7 @@ class Config return @default_license ||= @data['default_license'] end def license_uri(name) - str = @data['license_uris'][name] - if str.nil? - return nil - end - return URI::parse(str) + return URI::parse(@data['license_uris'][name]) end # People def default_author @@ -42,10 +38,8 @@ class Config return @data['person_emails'][name] end # Categories - def categories - return @data['categories'].keys - end def category_name(abbr) - return @data['categories'][abbr] + @categories ||= (@data['categories'] || {}).map{|k,v|[k.downcase,v]}.to_h + return @categories[abbr.downcase] end end diff --git a/lib/page.rb b/lib/page.rb index b349dc8..ebc123b 100644 --- a/lib/page.rb +++ b/lib/page.rb @@ -91,11 +91,11 @@ class Page end def index_link(cururl, depth) # FIXME: This code is super gross. - ret = " * <span><a class=\"#{index_class}\" href=\"#{cururl.route_to(url)}\" title=\"Published on #{atom_published.strftime('%Y-%m-%d')}" + ret = " * <span class=link-main><a class=\"#{index_class}\" href=\"#{cururl.route_to(url)}\" title=\"Published on #{atom_published.strftime('%Y-%m-%d')}" if atom_updated != atom_published ret += " (updated on #{atom_updated.strftime('%Y-%m-%d')})" end - ret += "\">#{atom_title}</a></span><span>" + ret += "\">#{atom_title}</a></span><span class=link-categories>" atom_categories.each do |t| ret += t.html end diff --git a/lib/page_index.rb b/lib/page_index.rb index 43bf367..31d4682 100644 --- a/lib/page_index.rb +++ b/lib/page_index.rb @@ -28,13 +28,15 @@ class IndexPage < LocalPage @ls ||= Dir::entries(local_infile) .select{|fname|not fname.start_with?(".")} .map{|fname|"#{local_infile}/#{fname}"} - .select{|path|Dir::exist?(path) or Config::get.html_suffixes.include?(File::extname(path).gsub(/^[.]/, ''))} end def index_pages if @pages.nil? @pages = Set[] for path in _ls - @pages.add( Dir::exist?(path) ? IndexPage::new(path) : LocalPage::new(path) ) + page = LocalPage::load(path) + unless page.nil? + @pages.add(page) + end end for data in (_metadata['external'] || []) @pages.add(RemotePage::new(data)) @@ -57,9 +59,9 @@ class IndexPage < LocalPage def index_link(cururl, depth) ret = '' unless depth <= 1 - ret += "<h#{depth}>[#{atom_title}](#{cururl.route_to(url)})</h#{depth}>\n\n" + ret += "<section><h#{depth}>[#{atom_title}](#{cururl.route_to(url)})</h#{depth}>\n\n" end - for page in index_pages.select{|page|not page.is_a?(IndexPage)}.sort_by{|page|page.atom_published} + for page in index_pages.select{|page|not page.is_a?(IndexPage)}.sort_by{|page|page.atom_updated}.reverse ret += page.index_link(cururl, depth+1) end ret += "\n" @@ -67,6 +69,9 @@ class IndexPage < LocalPage ret += page.index_link(cururl, depth+1) end ret += "\n" + unless depth <= 1 + ret += "</section>\n\n" + end return ret.gsub(/\n\n+/, "\n\n") end def index_title diff --git a/lib/page_local.rb b/lib/page_local.rb index e13fa33..6c70ac3 100644 --- a/lib/page_local.rb +++ b/lib/page_local.rb @@ -9,6 +9,21 @@ require 'pandoc' require 'person' class LocalPage < Page + def self.load(inpath) + case + when Dir::exist?(inpath) + require 'page_index' + return IndexPage::new(inpath) + when [".md", ".org"].include?(File::extname(inpath)) + return LocalPage::new(inpath) + when ".pdf" == File::extname(inpath) + require 'page_pdf' + return PdfPage::new(inpath) + else + return nil + end + end + def initialize(infile) @infile = infile super() diff --git a/lib/page_pdf.rb b/lib/page_pdf.rb new file mode 100644 index 0000000..e70c887 --- /dev/null +++ b/lib/page_pdf.rb @@ -0,0 +1,62 @@ +# coding: utf-8 +require 'erb' +require 'open3' +require 'yaml' + +require 'page_local' + +class PdfPage < LocalPage + def initialize(filename) + super(filename) + end + + def pdf_metadata + if @metadata.nil? + stdout, stderr, status = Open3::capture3("pdfinfo", "--", local_infile) + unless stderr.empty? + raise stderr + end + unless status.success? + raise status + end + raw_metadata = stdout.split("\n").map{|l|l.split(":", 2).map{|c|c.strip}}.to_h + + # Transform the PDF property names to match our metadata names + key_map = { + "Title" => "title", + "Author" => "author", + "CreationDate" => "published", + "ModDate" => "updated", + # "Keywords" => "categories", + } + @metadata = raw_metadata.map{|k,v|[key_map[k]||k,v]}.to_h + + yamlfile = local_infile.sub(/\.pdf$/, '.yaml') + if File::exist?(yamlfile) + @metadata = @metadata.merge(YAML::load(File::read(yamlfile))) + end + end + @metadata + end + def pdf_js_url + @@pdjfs ||= Config::get.url + 'pdfjs/web/viewer.html' + end + def pdf_viewer_url + @viewer_url ||= pdf_js_url + ('?file=' + URI::encode_www_form_component(pdf_js_url.route_to(local_srcurl))) + end + + def local_intype + return 'markdown' + end + def local_depends + if @depends.nil? + yamlfile = local_infile.sub(/\.pdf$/, '.yaml') + metafile = File::exist?(yamlfile) ? yamlfile : File::dirname(yamlfile) + tmplfile = "tmpl/pdf.md.erb" + @depends = super.map{|k,v|[k,v.merge([metafile, tmplfile])]}.to_h + end + @depends + end +end + +ERB::new(File::read("tmpl/pdf.md.erb")).def_method(PdfPage, 'local_input()', "tmpl/pdf.md.erb") diff --git a/lib/sitegen.rb b/lib/sitegen.rb index 78222a3..4a3dd48 100644 --- a/lib/sitegen.rb +++ b/lib/sitegen.rb @@ -40,7 +40,7 @@ module Sitegen end @deps end - def self.Makefile() + def self.Makefile str = '' dependencies.each do |target, deps| str += "#{target.to_s}: #{deps.sort.join(' ')}\n" |