jorvick

Sat, 10 Nov 2012 04:02:30 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 10 Nov 2012 04:02:30 +0000
changeset 18
a96836139ff9
parent 15
e784e417a3e2
permissions
-rwxr-xr-x

parsers.markdown: Make module callable, to allow parsing text as a module

#!/usr/bin/env lua

require "jorvick.util"

local commands = {};

function commands.build()
	require "jorvick.build";
end

function commands.publish(post)
	local f, err = io.open(post);
	if not f then
		print("Can't open post: "..(err or "unknown error"));
	end
	
	local f2, err = io.open(post..".published", "w+");
	local in_header;
	for line in f:lines() do
		if in_header == nil and line:match("^%-%-%-%s*$") then
			in_header = true;
		elseif in_header == true and line:match("^%-%-%-%s*$") then
			f2:write("published: "..os.date("!%Y-%m-%dT%H:%M:%SZ"), "\n");
			in_header = false;
		end
		f2:write(line, "\n");
	end
	f:close();
	f2:close();
	os.rename(post..".published", post);
	print("Post marked as published. Run build to update site.");
end

function commands.create(...)
	local title = table.concat({...}, " ");
	if #title == 0 then
		title = os.date("New Post %Y-%m-%d");
	end
	
	local filename = posts_dir..make_short_title(title)..".markdown";
	print("Creating post: "..filename)
	local f = io.open(filename, "r");
	if f then print("File already exists or is unreadable"); return; end
	f = io.open(filename, "w+");
	if not f then
		print("File not writeable");
		return;
	end
	
	-- YAML
	f:write("---\n");
	f:write("title: ", title, "\n");
	f:write("layout: post\n");
	f:write("tags: \n");
	f:write("x-published: \n");
	f:write("---\n\n");
	f:close();
	local blog_editor = os.getenv("BLOG_EDITOR");
	if blog_editor then
		blog_editor = blog_editor:gsub("%U+", { LINE = "8", POST = filename });
	else
		blog_editor = (os.getenv("EDITOR") or "nano").." +8 "..filename;
	end
	os.execute(blog_editor);
end

function commands.help()
	print "Jorvick - Blog Generator"
	print ""
	print "Commands:"
	print "   create - Create a new blank post with a title"
	print "  publish - Mark a post as published and stamp it with a date"
	print "    build - Generate the HTML files and feed for all posts"
	print ""
end

local command = arg[1] or "help";
table.remove(arg, 1);
(commands[command] or commands.help)(unpack(arg));

mercurial