# encoding: utf-8 # Copyright (C) 2016 Ian Kelling # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . module B # blog module require 'fileutils' require 'time' require 'safe_yaml' require 'pygments' require 'sqlite3' require 'redcarpet' JS_INFO = "

All JavaScript has LibreJS support.

" DAY = 60*60*24 DN = 'iankelling' FQDN = DN + '.org' DURL = 'https://' + FQDN DESCRIPTION = "Ian Kelling's personal site and blog on software" DATE_LEN = 'YYYY-MM-DD'.length NOW = Time.now.to_f WAIT_DATE = NOW - 60*60*24*1 def db_init SQLite3::Database.new('../proposed-comments/comments.sqlite') end # from the redcarpet readme, then a bunch of googling to figure # out what to do on exception. class HTMLwithPygments < Redcarpet::Render::HTML def block_code(code, language) begin Pygments.highlight(code, lexer: language) rescue MentosError # when language detection fails Pygments.highlight(code, lexer: 'text') end end end def fwrite(output_path, string) output_path = File.join('./', output_path) FileUtils.mkdir_p(File.dirname(output_path)) File.write(output_path, string) end def fskel(rel_path, title, content, o={}) head = < EOF if rel_path =~ %r{^/blog/|^blog.html} head += < EOF end o[:head] = head fwrite(rel_path, skel(title, content, o)) end def skel(title, content, o={}) # got meta viewport from jekyll's default later. It's for better # mobile viewing. output = < #{title} #{o[:head]}
#{content}
#{o[:comments]}
EOF output end def stdpage(page_name, content) fskel("#{page_name}.html", "#{DN}/#{page_name}", content, header: " / #{page_name}") end def md_to_html(md) # Using redcarpet over kramdown because syntax highlighting is # simpler. kramdown uses some crap highlighter by default, # supports using rouge, but then the classes are all screwy # for what pygments css expects, rouge has a pygments compatibility mode, # but that is a pita to get working, then it doesn't even work right. # kramdown is jekyll's default markdown parser, but it doesn't use # it for code blocks, it strips them out using custom templating # extension class, then uses rouge, then wraps it in some # custom html for pygments compatibility. It's a complicated mess. Redcarpet::Markdown.new(HTMLwithPygments, fenced_code_blocks: true).render(md) end def comment_html(comment, date) # I tried putting the time, %I:%M %p UTC, but it looks kinda # clunky, going against my simple theme. user_input = Redcarpet::Markdown.new(Redcarpet::Render::Safe, fenced_code_blocks: true).render(comment) < #{user_input}

#{Time.at(date).strftime("%b %-d '%y")}

EOF end def post(file, build_time=false) content = File.read(file) content =~ %r{\A(---\s*\n.*?\n?)^((---)\s*$\n?)}m # yaml front matter # stuff after last match. jekyll uses $POSTMATCH, # but it's nil for me, I don't know what magic they are using., # but only $' is listed here http://ruby-doc.org/core-2.3.1/doc/globals_rdoc.html, content = $' front = SafeYAML.load(Regexp.last_match(1)) title = front['title'] $page_title = "#{title} | #{DN}" header_rel = ' / blog /' footer_extra = <<-EOF

Subscribe

EOF footer_extra += JS_INFO if content =~ /