diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-12-31 17:43:33 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-12-31 17:43:33 -0700 |
commit | c52557f0a969e539138b0fdd4d7dd25f8795b509 (patch) | |
tree | 128bcfab061bc5f7c703dbbfed9ec6c209e71f7e /bin/util.rb | |
parent | dd2b6e83987786dbd53113fa984630119334925d (diff) |
wip
Diffstat (limited to 'bin/util.rb')
-rw-r--r-- | bin/util.rb | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/bin/util.rb b/bin/util.rb new file mode 100644 index 0000000..ce91007 --- /dev/null +++ b/bin/util.rb @@ -0,0 +1,111 @@ +# coding: utf-8 +load 'pandoc.rb' +require 'erb' +require 'date' + +$license_urls = { + "CC BY-SA-3.0" => 'https://creativecommons.org/licenses/by-sa/3.0/', + 'WTFPL-2' => "http://www.wtfpl.net/txt/copying/", +} +$person_uris = { + "Luke Shumaker" => "https://lukeshu.com/", + "Andrew Murrell" => "https://andrewdm.me/", +} +$person_emails = { + "Luke Shumaker" => "lukeshu@parabola.nu", + "Andrew Murrell" => "ImFromNASA@gmail.com", +} + +class Person + def initialize(name) + @name = name + end + def name + @name + end + def uri + $person_uris[@name] + end + def email + $person_emails[@name] + end + def html + if not email.nil? + return "<a href=\"mailto:#{email}\">#{name}</a>" + elsif not uri.nil? + return "<a href=\"#{uri}\">#{name}</a>" + else + return @name + end + end + def atom + ret = "" + ret += "<name>#{name}</name>" unless name.nil? + ret += "<uri>#{uri}</uri>" unless uri.nil? + ret += "<email>#{email}</email>" unless email.nil? + end +end + +class License + def initialize(name) + @name = name + end + def name + @name + end + def url + $license_urls[@name] + end + def html + "<a href=\"#{url}\">#{name}</a>" + end +end + +class Page + def initialize(infile) + @infile = infile + end + + def infile ; @infile ; end + def input ; @input ||= File.read(infile) ; end + def pandoc + if @pandoc.nil? + types = { + 'md' => 'markdown' + } + + ext = File.extname(infile).gsub(/^[.]/, '') + type = types[ext] || ext + @pandoc = Pandoc::load(type, input) + + if @pandoc['pandoc_format'] + @pandoc = Pandoc::load(@pandoc['pandoc_format'], input) + end + end + @pandoc + end + + def title ; @title ||= pandoc['title'] || input.split("\n",2).first ; end + def author ; @author ||= Person.new( pandoc['author'] || "Andrew Murrell") ; end + def license ; @license ||= License.new(pandoc['license'] || "CC BY-SA-3.0") ; end + def date ; @date ||= Date.parse(pandoc['date']) unless pandoc['date'].nil? ; end + def slug ; @slug ||= infile.sub(/\..*$/,'').sub(/^.*\//,'') ; end + def content ; @content ||= pandoc.to('html5 '+(pandoc['pandoc_flags']||'')) ; end + def head ; @head ||= pandoc['html_head_extra'] ; end + + def rights + @rights ||= "<p>The content of this page is Copyright © #{date.year unless date.nil?} #{author.html}.</p>\n" + + "<p>This page is licensed under the #{license.html} license.</p>" + end + + def breadcrumbs + @breadcrumbs ||= '<a href="/">Andrew Murrell</a> » ' + ( (slug == 'index') ? "blog" : "<a href=/blog>blog</a> » #{slug}" ) + end +end + +def html_escape(html) + html + .gsub('&', '&') + .gsub('>', '>') + .gsub('<', '<') +end |