diff options
-rw-r--r-- | Makefile | 3 | ||||
-rwxr-xr-x | check.rb | 4 | ||||
-rw-r--r-- | config.yaml | 4 | ||||
-rw-r--r-- | lib/category.rb (renamed from lib/tag.rb) | 7 | ||||
-rw-r--r-- | lib/config.rb | 9 | ||||
-rw-r--r-- | lib/page.rb | 56 | ||||
-rw-r--r-- | lib/page_index.rb | 45 | ||||
-rw-r--r-- | lib/page_local.rb | 68 | ||||
-rw-r--r-- | lib/page_remote.rb | 19 | ||||
-rw-r--r-- | notes.org | 3 | ||||
-rw-r--r-- | tmpl/index.atom.erb | 17 | ||||
-rw-r--r-- | tmpl/index.md.erb | 11 | ||||
-rw-r--r-- | tmpl/index.md.erb.bak | 12 | ||||
-rw-r--r-- | tmpl/page.atom.erb | 10 |
14 files changed, 160 insertions, 108 deletions
@@ -75,6 +75,9 @@ irb: irb .PHONY: irb +check: + ./check.rb + .PHONY: FORCE .DELETE_ON_ERROR: .SECONDARY: diff --git a/check.rb b/check.rb new file mode 100755 index 0000000..5e2a4ba --- /dev/null +++ b/check.rb @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require 'page_index' +top = IndexPage::new('src') +print top.local_input diff --git a/config.yaml b/config.yaml index b2eb53e..b2f7cd9 100644 --- a/config.yaml +++ b/config.yaml @@ -16,8 +16,8 @@ person_emails: "Luke Shumaker": "lukeshu@parabola.nu" "Andrew Murrell": "ImFromNASA@gmail.com" -# Tags -tag_names: +# Categories +categories: "DM": "DMing Resource" "ES": "Essay" "FF": "Flash Fiction" diff --git a/lib/tag.rb b/lib/category.rb index 4009f67..266da34 100644 --- a/lib/tag.rb +++ b/lib/category.rb @@ -1,7 +1,7 @@ # coding: utf-8 require 'config' -class Tag +class Category def initialize(abbr) @abbr = abbr end @@ -9,9 +9,12 @@ class Tag @abbr end def name - Config::get.tag_name(@abbr) + Config::get.category_name(@abbr) end def html return "<a class=\"tag #{abbr}\" href=\"/tags/#{abbr}.html\">#{name}</a>" end + def atom + return "<category term=\"#{term}\" label=\"#{name}\" />" + end end diff --git a/lib/config.rb b/lib/config.rb index b22eedf..4690559 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -41,8 +41,11 @@ class Config def person_email(name) return @data['person_emails'][name] end - # Tags - def tag_name(abbr) - return @data['tag_names'][abbr] + # Categories + def categories + return @data['categories'].keys + end + def category_name(abbr) + return @data['categories'][abbr] end end diff --git a/lib/page.rb b/lib/page.rb index d0b18ef..64bbaea 100644 --- a/lib/page.rb +++ b/lib/page.rb @@ -1,7 +1,7 @@ # coding: utf-8 require 'set' -require 'tag' +require 'category' class Page # Page is an abstract class. @@ -9,54 +9,54 @@ class Page # Implementors must implement several methods: # # def url => URI - # def title => String - # def author => Person - # def content => html | nil - # def rights => html | nil + # def atom_title => String + # def atom_author => Person + # def atom_content => html | nil + # def atom_rights => html | nil # - # def _tags => String | Enumerable<String> + # def page_categories => String | Enumerable<String> # - # def _published => DateTime | nil - # def _updated => DateTime | nil - # def _years => Enumerable<Fixnum> + # def page_published => DateTime | nil + # def page_updated => DateTime | nil + # def page_years => Enumerable<Fixnum> - def tags # => Enumerable<Tag> - if @tags.nil? - raw = _tags + def atom_categories # => Enumerable<Category> + if @categories.nil? + raw = page_categories if raw.is_a?(String) raw = raw.split end - @tags = raw.map{|tag|Tag.new(tag)} + @categories = raw.map{|abbr|Category.new(abbr)} end - @tags + @categories end - def published # => DateTime | nil + def atom_published # => DateTime | nil if @published.nil? - unless _published.nil? - @published = _published + unless page_published.nil? + @published = page_published else - unless _updated.nil? - @published = _updated + unless page_updated.nil? + @published = page_updated end end # sanity check - unless _published.nil? or _updated.nil? - if _updated < _published - @published = _updated + unless page_published.nil? or page_updated.nil? + if page_updated < page_published + @published = page_updated end end end @published end - def updated # => DateTime | nil + def atom_updated # => DateTime | nil if @updated.nil? - unless _updated.nil? - @updated = _updated + unless page_updated.nil? + @updated = page_updated else - unless _published.nil? - @updated = _published + unless page_published.nil? + @updated = page_published end end end @@ -71,7 +71,7 @@ class Page first = published.year last = updated.year - years = _years + years = page_years years.add(first) years.add(last) diff --git a/lib/page_index.rb b/lib/page_index.rb index 073537e..3e25813 100644 --- a/lib/page_index.rb +++ b/lib/page_index.rb @@ -14,7 +14,7 @@ class IndexPage < LocalPage def _metadata if @metadata.nil? - yamlfile = _infile+"/index.yaml" + yamlfile = local_infile+"/index.yaml" if File::exist?(yamlfile) @metadata = YAML::load(File::read(yamlfile)) else @@ -24,19 +24,19 @@ class IndexPage < LocalPage @metadata end def _ls - @ls ||= Dir::entries(_infile) + @ls ||= Dir::entries(local_infile) .select{|fname|not fname.start_with?(".")} - .map{|fname|"#{_infile}/#{fname}"} + .map{|fname|"#{local_infile}/#{fname}"} .select{|path|Dir::exist?(path) or Config::get.html_suffixes.include?(File::extname(path).gsub(/^[.]/, ''))} end - def pages + def index_pages if @pages.nil? @pages = [] for path in _ls if Dir::exist?(path) page = IndexPage::new(path) @pages.unshift(page) - @pages += page.pages + @pages += page.index_pages else @pages.unshift(LocalPage::new(path)) end @@ -48,16 +48,43 @@ class IndexPage < LocalPage @pages end - def _published + def atom_title + _metadata['title'] + end + + def local_outfile + local_infile.sub(/^src/, 'out')+"/index.html" + end + def local_depends + if @depends.nil? + basename = local_infile.sub(/^src/, 'out') + deps = Set[local_infile] + yamlfile = local_infile+"/index.yaml" + if File::exist?(yamlfile) + deps.add(yamlfile) + end + index_pages.each{|p|deps.merge(p.local_outfile[''])} + @depends = { + "#{basename}/index.html" => deps.clone.merge(["tmpl/index.md.erb", "tmpl/page.html.erb"]), + "#{basename}/index.atom" => deps.clone.merge(["tmpl/index.atom.erb", "tmpl/page.atom.erb"]), + } + end + @depends + end + def local_srcurl + return nil + end + + def page_published return nil end - def _updated + def page_updated return nil end - def _years + def page_years return Set[] end end ERB::new(File::read("tmpl/index.atom.erb")).def_method(IndexPage, 'atom()', "tmpl/index.atom.erb") -ERB::new(File::read("tmpl/index.md.erb")).def_method(IndexPage, '_input()', "tmpl/index.md.erb") +ERB::new(File::read("tmpl/index.md.erb")).def_method(IndexPage, 'local_input()', "tmpl/index.md.erb") diff --git a/lib/page_local.rb b/lib/page_local.rb index 1ca14f0..5b2af3b 100644 --- a/lib/page_local.rb +++ b/lib/page_local.rb @@ -17,34 +17,34 @@ class LocalPage < Page # Some of this code looks a little weird because it is # super-aggressively lazy-evaluated and cached. - def _infile ; @infile ; end - def _input ; @input ||= File::read(_infile) ; end + def local_infile ; @infile ; end + def local_input ; @input ||= File::read(local_infile); end def _pandoc if @pandoc.nil? types = { 'md' => 'markdown' } - ext = File::extname(_infile).gsub(/^[.]/, '') + ext = File::extname(local_infile).gsub(/^[.]/, '') type = types[ext] || ext - @pandoc = Pandoc::load(type, _input) + @pandoc = Pandoc::load(type, local_input) if @pandoc['pandoc_format'] - @pandoc = Pandoc::load(@pandoc['pandoc_format'], _input) + @pandoc = Pandoc::load(@pandoc['pandoc_format'], local_input) end end @pandoc end # Query simple document metadata - def title ; @title ||= _pandoc['title'] || _input.split("\n",2).first ; end - def author ; @author ||= Person::new( _pandoc['author'] || Config::get.default_author) ; end - def license ; @license ||= License::new(_pandoc['license'] || Config::get.default_license); end - def head ; @head ||= _pandoc['html_head_extra'] ; end - def class ; @class ||= _pandoc['class'] ; end - def _tags ; @_tags ||= _pandoc['tags'] || [] ; end - - def content + def atom_author ; @author ||= Person::new( _pandoc['author'] || Config::get.default_author) ; end + def atom_title ; @title ||= _pandoc['title'] || local_input.split("\n",2).first ; end + def html_class ; @class ||= _pandoc['class'] ; end + def html_head_extra ; @head ||= _pandoc['html_head_extra'] ; end + def local_license ; @license ||= License::new(_pandoc['license'] || Config::get.default_license); end + def page_categories ; @cats ||= _pandoc['categories'] || [] ; end + + def atom_content if @content.nil? @content = '' # Only insert the title if it came from Pandoc metadata; @@ -60,17 +60,17 @@ class LocalPage < Page @content end - def rights + def atom_rights # TODO: simplify year spans - @rights ||= "<p>The content of this page is Copyright © #{years.sort.join(', ')} #{author.html}.</p>\n" + - "<p>This page is licensed under the #{license.html} license.</p>" + @rights ||= "<p>The content of this page is Copyright © #{years.sort.join(', ')} #{atom_author.html}.</p>\n" + + "<p>This page is licensed under the #{local_license.html} license.</p>" end def _gitdates - @gitdates ||= `git log --format='%cI' -- #{_infile}`.split('\n').select{|s|!s.empty?}.map{|s|DateTime::iso8601(s)} + @gitdates ||= `git log --format='%cI' -- #{local_infile}`.split("\n").select{|s|!s.empty?}.map{|s|DateTime::iso8601(s)} end - def _published + def page_published if @_published.nil? raw = _pandoc['published'] @_published = Datetime::parse(raw) unless raw.nil? @@ -81,7 +81,7 @@ class LocalPage < Page @_published end - def _updated + def page_updated if @_updated.nil? raw = _pandoc['updated'] @_updated = DateTime::parse(raw) unless raw.nil? @@ -91,24 +91,32 @@ class LocalPage < Page end @_updated end - - def _years - @years ||= Set[*_gitdates.map{|dt|dt.year}] + + def page_years + @years ||= Set[*_gitdates.map{|dt|dt.year}] end - def abssrcpath - @srcpath ||= _infile.sub(/^(src|out)\//, '/') + def local_outfile + local_infile.sub(/^src/, 'out').sub(/\.[^\/.]*$/, '.html') end - def absoutpath - @outpath ||= abssrcpath.sub(/\.[^\/.]*$/, '.html').sub(/\/index[.]html$/, '') + def local_depends + if @depends.nil? + basename = local_infile.sub(/^src/, 'out').sub(/\.[^\/.]*$/, '') + @depends = { + "#{basename}.html" => Set[local_infile, "tmpl/page.html.erb"], + #"#{basename}.atom" => Set[local_infile, "tmpl/page.atom.erb"] + } + end + @depends end - def url - @url ||= Config::get.url + absoutpath + def local_srcurl + @srcurl ||= Config::get.url + local_infile.sub(/^src/, '') end - def srcurl - @srcurl ||= Config::get.url + abssrcpath + def url + @outurl ||= Config::get.url + local_outfile.sub(/^out/, '') end end +ERB::new(File::read("tmpl/page.atom.erb")).def_method(LocalPage, 'atom()', "tmpl/page.atom.erb") ERB::new(File::read("tmpl/page.html.erb")).def_method(LocalPage, 'html()', "tmpl/page.html.erb") diff --git a/lib/page_remote.rb b/lib/page_remote.rb index a754af6..e886283 100644 --- a/lib/page_remote.rb +++ b/lib/page_remote.rb @@ -3,7 +3,6 @@ require 'date' require 'config' require 'page' -require 'tag' class RemotePage < Page def initialize(metadata) @@ -14,27 +13,27 @@ class RemotePage < Page return Config::get.url + @metadata['url'] end - def title + def atom_title @metadata['title'] end - def author + def atom_author Person::new(@metadata['author'] || Config::get.default_author) end - def content + def atom_content return nil end - def rights + def atom_rights return nil end - def _tags - @metadata['tags'] || [] + def page_cagetories + @metadata['categories'] || [] end - def _published + def page_published str = @metadata['published'] if str.nil? return nil @@ -42,7 +41,7 @@ class RemotePage < Page return Date::parse(str) end - def _updated + def page_updated str = @metadata['updated'] if str.nil? return nil @@ -50,7 +49,7 @@ class RemotePage < Page return Date::parse(str) end - def _years + def page_years return [] end end diff --git a/notes.org b/notes.org new file mode 100644 index 0000000..5b00a25 --- /dev/null +++ b/notes.org @@ -0,0 +1,3 @@ +I need to insert this on all XML output: + +<?xml version="1.0" encoding="utf-8"?> diff --git a/tmpl/index.atom.erb b/tmpl/index.atom.erb index cd70f7e..54e4b72 100644 --- a/tmpl/index.atom.erb +++ b/tmpl/index.atom.erb @@ -1,4 +1,3 @@ -<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>AndrewDM.me <%= @title %></title> @@ -9,19 +8,5 @@ <author><%= Person.new("Andrew Murrell").atom %></author> <id><%= $url %></id> - <% @pages.sort_by{|p| p.updated}.reverse.each do |page| %> - <entry xmlns="http://www.w3.org/2005/Atom"> - <link rel="alternate" type="text/html" href="<%= page.url %>"/> - <id><%= page.url %></id> - <updated><%= page.updated.rfc3339 %></updated> - <published><%= page.published.rfc3339 %></published> - <title><%= page.title %></title> - <author><%= page.author.atom %></author> -<% if page.content %> - <content type="html"><%= html_escape(page.content) %></content> -<% end %><% if page.rights %> - <rights type="html"><%= html_escape(page.rights) %></rights> -<% end %> - </entry> - <% end %> + <% @pages.sort_by{|p| p.updated}.reverse.each do |page| %><%=@page.atom %><% end %> </feed> diff --git a/tmpl/index.md.erb b/tmpl/index.md.erb index a3ec547..680647d 100644 --- a/tmpl/index.md.erb +++ b/tmpl/index.md.erb @@ -1,12 +1,7 @@ --- -title: "<%= @title %>" +title: "<%= atom_title %>" class: "index" --- -<% @sections.keys.sort.each do |path| %> -<% unless path.empty? %>## [<%= @sections[path]['head'] %>](<%= path %>)<% end %> - -<% @sections[path]['body'].sort_by{|a|a.published}.reverse.each do |a| %> - * <span><a <% if a.is_a?(ExternPage) %>class="external" <% end %>href="<%= @url.route_to(a.url) %>" title="Published on <%= a.published.strftime('%Y-%m-%d') %><% if a.updated != a.published %> (updated on<%= a.updated.strftime('%Y-%m-%d') %>)<% end %>"><%= a.title %></a></span><span><% a.tags.each do |t| %><%= t.html %><% end %></span><% end %> - -<% end %> +<% index_pages.sort_by{|a|a.atom_published}.reverse.each do |a| %> + * <span><a <% if a.is_a?(RemotePage) %>class="external" <% end %>href="<%= Config::get.url.route_to(a.url) %>" title="Published on <%= a.atom_published.strftime('%Y-%m-%d') %><% if a.atom_updated != a.atom_published %> (updated on<%= a.atom_updated.strftime('%Y-%m-%d') %>)<% end %>"><%= a.title %></a></span><span><% a.atom_categories.each do |t| %><%= t.html %><% end %></span><% end %> diff --git a/tmpl/index.md.erb.bak b/tmpl/index.md.erb.bak new file mode 100644 index 0000000..a3ec547 --- /dev/null +++ b/tmpl/index.md.erb.bak @@ -0,0 +1,12 @@ +--- +title: "<%= @title %>" +class: "index" +--- + +<% @sections.keys.sort.each do |path| %> +<% unless path.empty? %>## [<%= @sections[path]['head'] %>](<%= path %>)<% end %> + +<% @sections[path]['body'].sort_by{|a|a.published}.reverse.each do |a| %> + * <span><a <% if a.is_a?(ExternPage) %>class="external" <% end %>href="<%= @url.route_to(a.url) %>" title="Published on <%= a.published.strftime('%Y-%m-%d') %><% if a.updated != a.published %> (updated on<%= a.updated.strftime('%Y-%m-%d') %>)<% end %>"><%= a.title %></a></span><span><% a.tags.each do |t| %><%= t.html %><% end %></span><% end %> + +<% end %> diff --git a/tmpl/page.atom.erb b/tmpl/page.atom.erb new file mode 100644 index 0000000..ea37ea5 --- /dev/null +++ b/tmpl/page.atom.erb @@ -0,0 +1,10 @@ +<entry xmlns="http://www.w3.org/2005/Atom"> + <link rel="alternate" type="text/html" href="<%= page.url %>"/> + <id><%= page.url %></id> + <updated><%= page.updated.rfc3339 %></updated> + <published><%= page.published.rfc3339 %></published> + <title><%= page.title %></title> + <author><%= page.author.atom %></author> + <% if page.content %><content type="html"><%= html_escape(page.content) %></content><% end %> + <% if page.rights %><rights type="html"><%= html_escape(page.rights) %></rights><% end %> +</entry> |