Improve 'create' command - now automatically fills in metadata and opens an editor

Mon, 20 Jul 2009 11:42:54 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 20 Jul 2009 11:42:54 +0100
changeset 14
7e9c3f32ec0a
parent 13
39add8e7fa99
child 15
e784e417a3e2

Improve 'create' command - now automatically fills in metadata and opens an editor

jorvick file | annotate | diff | comparison | revisions
--- a/jorvick	Tue Jul 14 02:07:18 2009 +0100
+++ b/jorvick	Mon Jul 20 11:42:54 2009 +0100
@@ -14,17 +14,31 @@
 		title = os.date("New Post %Y-%m-%d");
 	end
 	
-	local filename = make_short_title(title);
-	print(posts_dir..filename)
-	local f = io.open(posts_dir..filename..".markdown", "r");
-	if f then print("File already exists or is unreable"); return; end
-	f = io.open(posts_dir..filename..".markdown", "w+");
+	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("
+	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()

mercurial