util.lua

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 17
1d0ae741807e
permissions
-rw-r--r--

parsers.markdown: Make module callable, to allow parsing text as a module

6
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Takes "Why I like fish" and returns "why-i-like-fish"
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 function make_short_title(long_title)
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local short_title = long_title:gsub("%W+", "-"):lower():sub(1,45)
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 if #long_title:gsub("%W+", "-") > 45 then
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 short_title = short_title:gsub("%-%w+$", "");
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 end
17
1d0ae741807e util.lua: Trim leading non-alphanumerics when generating URL as well as trailing ones
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
8 return (short_title:gsub("%W+$", ""):gsub("^%W+", ""));
6
cf40d55f8122 Add util module and move first helper function into it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 end
8
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
10
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
11 -- Config --
9
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
12 local conffile = ".blogrc";
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
13 for i=1,#arg do
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
14 if arg[i] == "-c" or arg[i] == "--config" then
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
15 table.remove(arg, i);
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
16 conffile = arg[i] or conffile;
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
17 table.remove(arg, i);
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
18 break;
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
19 end
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
20 end
13eb9435e1c0 Allow config file to be specified with flag on the command-line
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
21 dofile(conffile)
8
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
22
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
23 posts_dir = posts_dir or "_posts/"
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
24 layouts_dir = layouts_dir or "_layouts/"
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
25 output_dir = output_dir or "./"
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
26 -- - -- - --
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
27
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
28 -- Append '/' to end of posts_dir if necessary
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
29 posts_dir = posts_dir:gsub("([^/])$", "%1/")
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
30
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
31 -- Append '/' to end of output_dir if necessary
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
32 output_dir = output_dir:gsub("([^/])$", "%1/")
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
33
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
34 -- Append '/' to end of base_url if necessary
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
35 base_url = base_url:gsub("([^/])$", "%1/")
6dc28a66f4d9 Move config into util.lua
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
36

mercurial