Skip to content

Commit

Permalink
refactor sitegen command actions to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Jul 6, 2015
1 parent e20b9dc commit 7a39b2f
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 203 deletions.
207 changes: 4 additions & 203 deletions bin/sitegen
@@ -1,217 +1,18 @@
#!/usr/bin/env moon

moonscript = require "moonscript"
moon = require "moon"
cosmo = require "cosmo"

import run_with_scope, extend, dump from require "moon"
import SiteFile from require "sitegen.site_file"
import catch_error, throw_error, split, slugify, Path from require "sitegen.common"

action = ... or "build"
args = {...}

default = {
sitefile: "site.moon"
files:
page: [==[{
date: "$eval{"require('date')()"}"
$if{"title"}[[title: "$title"]]
}
Hello world!
]==]
sitefile: [==[
sitegen = require "sitegen"
sitegen.create =>
@title = $title
]==]
}

log = (...) ->
print "->", ...

scope = (t={}) ->
extend t, {
eval: (arg) ->
code = "return -> " .. arg[1]
moonscript.loadstring(code)!!
if: (arg) ->
var_name = arg[1]
cosmo.yield t if t[var_name]
nil
}

get_site = -> SiteFile!\get_site!

annotate = (obj, verbs) ->
setmetatable {}, {
__newindex: (name, value) =>
obj[name] = value
__index: (name) =>
fn = obj[name]
return fn if not type(fn) == "function"
if verbs[name]
(...) ->
fn ...
first = ...
log verbs[name], first
else
fn
}

Path = annotate Path, {
mkdir: "made directory"
write_file: "wrote"
}

-- wrap test based on tokens
wrap_text = (text, indent=0, max_width=80) ->
width = max_width - indent
words = split text, " "
pos = 1
lines = {}
while pos <= #words
line_len = 0
line = {}
while true
word = words[pos]
break if word == nil
error "can't wrap text, words too long" if #word > width
break if line_len + #word > width

pos += 1
table.insert line, word
line_len += #word + 1 -- +1 for the space

table.insert lines, table.concat line, " "

table.concat lines, "\n" .. (" ")\rep indent

columnize = (rows, indent=2, padding=4) ->
max = 0
max = math.max max, #row[1] for row in *rows

left_width = indent + padding + max

formatted = for row in *rows
padd = (max - #row[1]) + padding
table.concat {
(" ")\rep indent
row[1]
(" ")\rep padd
wrap_text row[2], left_width
}

table.concat formatted, "\n"

tasks = {
dump: -> print dump get_site!

new: (title) ->
if Path.exists default.sitefile
throw_error "sitefile already exists: " .. default.sitefile

title = ("%q")\format title or "Hello World"

Path.mkdir"www"
Path.mkdir"templates"

site_moon = cosmo.f(default.files.sitefile) scope{:title}
Path.write_file default.sitefile, site_moon

page: (path, title)->
get_site!

if not title
title = path
path_part, title_part = title\match"^(.-)([^/]+)$"
if path_part
title = title_part
path = path_part
else
path = '.'

Path.mkdir path if Path.normalize(path) != ""

-- iterater for all potential file names
names = (fname, ext=".md") ->
i = 0
coroutine.wrap ->
while true
coroutine.yield if i == 0
fname .. ext
else
table.concat {fname, "_", i, ext }
i += 1

full_path = nil
for name in names slugify title
full_path = Path.join path, name
if not Path.exists full_path
break

Path.write_file full_path, cosmo.f(default.files.page) scope{:title}

-- TODO: this should be implemented in the plugin, not here
deploy: (host, path) ->
site = get_site!
deploy = site\get_plugin "sitegen.plugins.deploy"
throw_error "deploy plugin not initialized" unless deploy

{:host, :path} = deploy

throw_error "need host" unless host
throw_error "need path" unless path

log "uploading to:", host, path

deploy\sync!

build: (...) ->
files = {...}
site = get_site!

local filter
if next files
filter = {}
for i, fname in ipairs files
filter[site.sitefile\relativeize fname] = true

site\write filter

watch: ->
site = get_site!
w = require "sitegen.watch"

with w.Watcher site
\loop!

help: ->
print "Sitegen"
print "usage: sitegen <action> [args]"
print!
print "Available actions:"

print!
print columnize {
{"new", "Create a new site in the current directory"}
{"build [input-files]", "Build (or rebuild) all pages, or only files"}
{"page <path> [title]", "Create a new markdown page at path"}
{"deploy [host] [path]", "Deploy site to host over ssh (rsync)"}
{"watch", "Compile pages automatically when inputs change (needs inotify)"}
}
print!
}
import actions from require "sitegen.cmd.actions"
import catch_error from require "sitegen.common"

-- potential commands:
-- plugins: sitegen blog:post "Hello World" -- did I already do this?

catch_error ->
if not tasks[action]
if not actions[action]
throw_error "unknown task: " .. action

tasks[action] unpack args, 2
actions[action] unpack args, 2

-- vim: set filetype=moon:
2 changes: 2 additions & 0 deletions sitegen-dev-1.rockspec
Expand Up @@ -29,6 +29,8 @@ build = {
modules = {
["sitegen"] = "sitegen/init.lua",
["sitegen.cache"] = "sitegen/cache.lua",
["sitegen.cmd.actions"] = "sitegen/cmd/actions.lua",
["sitegen.cmd.util"] = "sitegen/cmd/util.lua",
["sitegen.common"] = "sitegen/common.lua",
["sitegen.default.templates"] = "sitegen/default/templates.lua",
["sitegen.formatters.default"] = "sitegen/formatters/default.lua",
Expand Down

0 comments on commit 7a39b2f

Please sign in to comment.