Mon, 20 Jul 2009 11:42:54 +0100
Improve 'create' command - now automatically fills in metadata and opens an editor
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 | |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | function commands.create(...) |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | local title = table.concat({...}, " "); |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | if #title == 0 then |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | 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
|
15 | end |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | |
14
7e9c3f32ec0a
Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
17 | 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
|
18 | 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
|
19 | 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
|
20 | 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
|
21 | 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
|
22 | if not f then |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | 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
|
24 | return; |
13
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | end |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | -- YAML |
14
7e9c3f32ec0a
Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
28 | 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
|
29 | 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
|
30 | 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
|
31 | 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
|
32 | 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
|
33 | 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
|
34 | f:close(); |
7e9c3f32ec0a
Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
35 | 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
|
36 | 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
|
37 | 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
|
38 | else |
7e9c3f32ec0a
Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
39 | 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
|
40 | end |
7e9c3f32ec0a
Improve 'create' command - now automatically fills in metadata and opens an editor
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
41 | 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
|
42 | end |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | function commands.help() |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | print "Jorvick - Blog Generator" |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | print "" |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | print "Commands:" |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | 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
|
49 | 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
|
50 | 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
|
51 | print "" |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | end |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | 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
|
55 | table.remove(arg, 1); |
39add8e7fa99
Add initial jorvick utility to manage posts and build the site
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | (commands[command] or commands.help)(unpack(arg)); |