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

13
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env lua
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 require "jorvick.util"
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local commands = {};
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function commands.build()
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 require "jorvick.build";
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 end
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
15
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
11 function commands.publish(post)
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
12 local f, err = io.open(post);
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
13 if not f then
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
14 print("Can't open post: "..(err or "unknown error"));
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
15 end
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
16
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
17 local f2, err = io.open(post..".published", "w+");
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
18 local in_header;
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
19 for line in f:lines() do
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
20 if in_header == nil and line:match("^%-%-%-%s*$") then
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
21 in_header = true;
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
22 elseif in_header == true and line:match("^%-%-%-%s*$") then
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
23 f2:write("published: "..os.date("!%Y-%m-%dT%H:%M:%SZ"), "\n");
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
24 in_header = false;
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
25 end
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
26 f2:write(line, "\n");
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
27 end
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
28 f:close();
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
29 f2:close();
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
30 os.rename(post..".published", post);
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
31 print("Post marked as published. Run build to update site.");
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
32 end
e784e417a3e2 Add publish command to jorvick
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
33
13
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function commands.create(...)
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local title = table.concat({...}, " ");
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if #title == 0 then
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 title = os.date("New Post %Y-%m-%d");
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
14
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
40 local filename = posts_dir..make_short_title(title)..".markdown";
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
41 print("Creating post: "..filename)
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
42 local f = io.open(filename, "r");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
43 if f then print("File already exists or is unreadable"); return; end
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
44 f = io.open(filename, "w+");
13
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if not f then
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 print("File not writeable");
14
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
47 return;
13
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 -- YAML
14
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
51 f:write("---\n");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
52 f:write("title: ", title, "\n");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
53 f:write("layout: post\n");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
54 f:write("tags: \n");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
55 f:write("x-published: \n");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
56 f:write("---\n\n");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
57 f:close();
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
58 local blog_editor = os.getenv("BLOG_EDITOR");
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
59 if blog_editor then
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
60 blog_editor = blog_editor:gsub("%U+", { LINE = "8", POST = filename });
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
61 else
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
62 blog_editor = (os.getenv("EDITOR") or "nano").." +8 "..filename;
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
63 end
7e9c3f32ec0a Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
64 os.execute(blog_editor);
13
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 function commands.help()
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 print "Jorvick - Blog Generator"
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 print ""
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 print "Commands:"
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 print " create - Create a new blank post with a title"
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 print " publish - Mark a post as published and stamp it with a date"
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 print " build - Generate the HTML files and feed for all posts"
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 print ""
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local command = arg[1] or "help";
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 table.remove(arg, 1);
39add8e7fa99 Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 (commands[command] or commands.help)(unpack(arg));

mercurial