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