Sat, 23 May 2009 21:15:19 +0100
Fix potential skipping of published posts if prior posts weren't published
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | require "LuaTemplates.templates" |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | require "LuaTemplates.parsers" |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | require "lfs" |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | -- Config -- |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | dofile(arg[1] or ".blogrc") |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | posts_dir = posts_dir or "_posts/" |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | layouts_dir = layouts_dir or "_layouts/" |
2
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
11 | output_dir = output_dir or "./" |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | -- - -- - -- |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | -- Logging -- |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | function make_msg(prefix, name) return function (fmt, ...) print(prefix, string.format(fmt, ...)); end end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | msg_info, msg_warn, msg_error = make_msg("II", "info"), make_msg("WW", "warn"), make_msg("EE", "error"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | -- -- - -- -- |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | -- Append '/' to end of posts_dir if necessary |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | posts_dir = posts_dir:gsub("([^/])$", "%1/") |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | |
2
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
22 | -- Append '/' to end of output_dir if necessary |
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
23 | output_dir = output_dir:gsub("([^/])$", "%1/") |
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
24 | |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | -- Append '/' to end of base_url if necessary |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | base_url = base_url:gsub("([^/])$", "%1/") |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | posts = {}; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | -- Read posts, interpret |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | for post_filename in lfs.dir(posts_dir:gsub("/$", "")) do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | -- Ignore dotfiles |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | if not post_filename:match("^%.") then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | msg_info("Processing %s", post_filename); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | local post_file, err = io.open(posts_dir..post_filename); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | if post_file then |
4
cebf96c3b522
Correctly parse file extension to determine post format
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
38 | local post_format = post_filename:match("%.(%w+)$") or "text"; |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | local post = { filename = post_filename, path = posts_dir..post_filename, format = post_format }; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | if not parsers[post_format] then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | msg_warn("Post format '%s' not supported", post_format); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | local inside_header, line_num = nil, 0; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | for line in post_file:lines() do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | line_num = line_num + 1; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | if line:match("^%-%-%-") then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | if not inside_header then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | inside_header = true; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | inside_header = false; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | break; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | elseif not inside_header then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | break; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | local k,v = line:match("^(%w+):%s?(.*)$"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | if k and v then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | post[k] = v; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | msg_warn("Couldn't parse header at line %d", line_num); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | local post_data, err = post_file:read("*a"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | -- Parse post_data according to extension, add to post list |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | --msg_info("Parsing %s...", post_filename) --:gsub("%.[^%.]+$", ""):match("[^/]+")); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | post.content = templates.load(parsers[post_format](post_data)):render{ page = post }; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | post.shorttitle = post.title:gsub("%W+", "-"):lower():sub(1,45) |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | if #post.title:gsub("%W+", "-") > 45 then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | post.shorttitle = post.shorttitle:gsub("%-%w+$", ""); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | post.url = base_url..post.shorttitle.."/" |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | post.post = post |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | post.updated = post.updated or os.date("!%Y-%m-%dT%H:%M:%SZ", lfs.attributes(post.path).modification); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | if post.published then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | if not post.published:match("^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d%:%d%dZ$") then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | msg_error("Post has invalid published date"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | table.insert(posts, post); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | msg_error("Failed to open %s, %s", post_filename, err); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | local layouts = setmetatable({ default = templates.init(layouts_dir.."default.html"); }, |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | { __index = function (t, k) rawset(t, k, templates.init(layouts_dir..k..".html")); return rawget(t, k); end }); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | table.sort(posts, function (a, b) |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | do return (a.updated or "") < (b.updated or ""); end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | if not a.updated then return true; end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | if not b.updated then return false; end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 | local a_year, a_month, a_day = a.updated:match("(%d%d%d%d)%-(%d%d)%-(%d%d)"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | local b_year, b_month, b_day = b.updated:match("(%d%d%d%d)%-(%d%d)%-(%d%d)"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | if b_year >= a_year then return true; end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | if b_month >= a_month then return true; end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 | if b_day >= a_day then return true; end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 | return false; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | end); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | local shorttitles = {}; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 | for _, post in ipairs(posts) do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 | while shorttitles[post.shorttitle] do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 | post.shorttitle = post.shorttitle:gsub("%~%d+$", "").."~"..((tonumber(post.shorttitle:match("%~(%d*)$")) or 0) + 1) |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | post.url = base_url..post.shorttitle.."/" |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | shorttitles[post.shorttitle] = post; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 | msg_info("%s post%s processed", #posts, #posts ~= 1 and "s" or ""); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 | msg_info(" - - - "); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 | |
5
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
120 | local unpublished, removed_posts = 0, {}; |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 | for n, post in ipairs(posts) do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 | if post.content and (not post.layout or layouts[post.layout]) then |
2
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
123 | lfs.mkdir(output_dir..post.shorttitle); |
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
124 | local outfile, err = io.open(output_dir..post.shorttitle.."/index.html", "w+"); |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 | if outfile then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 | local layout = layouts[post.layout or "default"]; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 | -- Link to the previous and next published posts |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 | local i = 1; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 | repeat |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 | post.next = posts[n+i]; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 | i = i + 1; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 | until (not post.next) or post.next.published; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 | i = 1; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 | repeat |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 | post.previous = posts[n-i]; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 | i = i + 1; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 | until (not post.previous) or post.previous.published; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 | post.id = n; |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 | outfile:write(layout:render(post)); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 | outfile:close(); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 | if not post.published then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 | -- Not published yet, hide it |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 | --msg_info("Hiding unpublished post"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 | unpublished = unpublished + 1; |
5
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
147 | table.insert(removed_posts, n); |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 | msg_error("Failed to write HTML: %s", err); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 | if post.published then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 | msg_info("Published: %s", post.shorttitle); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 | else |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 | msg_info("Generated: %s", post.shorttitle); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 | |
5
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
160 | local removed = 0; |
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
161 | for _, n in ipairs(removed_posts) do |
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
162 | table.remove(posts, n-removed); |
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
163 | removed = removed + 1; |
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
164 | end |
951de9625338
Fix potential skipping of published posts if prior posts weren't published
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
165 | |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 | -- Do main page |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 | local main_tpl = templates.init(layouts_dir.."/main.html"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 | |
3
f22a275a3a33
Fix for index.html to be saved in output_dir
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
169 | main_tpl:saverender(output_dir.."index.html", { title = blog_title, posts = posts }); |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 | msg_info("%s post%s published, %d post%s unpublished", #posts, #posts ~= 1 and "s" or "", unpublished, unpublished ~= 1 and "s" or ""); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 | msg_info(" - - - "); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 | -- Do feed |
2
80eb1f7784c4
Support for output_dir config option
Matthew Wild <mwild1@gmail.com>
parents:
1
diff
changeset
|
176 | local atom = io.open(output_dir.."feed/atom.xml.new", "w+"); |
1
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 | atom:write[[<?xml version="1.0" encoding="UTF-8"?> |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 | <feed xmlns="http://www.w3.org/2005/Atom"> |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 | ]] |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 | atom:write("\t<id>", base_url, "</id>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 | atom:write("\t<title>", blog_title, "</title>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 | atom:write("\t<subtitle>", blog_subtitle, "</subtitle>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 | atom:write("\t<link href='", base_url, "feed/atom.xml' rel='self' />\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 | atom:write("\t<link href='", base_url, "' />\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 | atom:write("\t<updated>", os.date "!%Y-%m-%dT%H:%M:%SZ", "</updated>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 | for n, post in ipairs(posts) do |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 | if n > 10 then break; end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 | if post.published then |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 | atom:write("<entry>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 | atom:write("\t<author>\n\t\t<name>", post.author or default_author or "Unknown Author", "</name>\n\t</author>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 | atom:write("\t<id>", post.url, "</id>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 | atom:write("\t<title>", post.title, "</title>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 | atom:write("\t<published>", post.published, "</published>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 | atom:write("\t<updated>", post.updated, "</updated>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 | atom:write("\t<link href='", post.url, "' />\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 | atom:write("\t<content type='html'>\n", post.content:gsub("&", "&"):gsub("<", "<"), "\t</content>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 | atom:write("</entry>\n"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 | end |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 | atom:write("</feed>"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 | atom:close(); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 | os.rename("feed/atom.xml.new", "feed/atom.xml"); |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 | |
74deddff1202
Add build.lua now that all Prosody-specific things are out of it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 | msg_info("ATOM feed written to feed/atom.xml"); |