~sivers/sive.rs

sive.rs/script/init.rb -rwxr-xr-x 1.5 KiB
5fcf6634 — Derek Sivers cover and hardcover 18 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'psych'
require 'mustache'
require 'date'
require 'cgi'
require 'rss'

# root directory of /sive.rs/
ROOT = File.expand_path('../..', __FILE__) + '/'

# https://sive.rs/ files
WEB_DIR = ROOT + '/site/'

# https://m.sive.rs/ files
MEDIA_DIR = File.expand_path('../../../m', __FILE__) + '/'

# write file only if different than what's already there
def wput(uri, contents)
  filepath = WEB_DIR + uri
  if File.exist?(filepath)
    old_contents = File.read(filepath).strip
    return if contents.strip == old_contents
  end
  File.open(filepath, 'w') {|f| f.puts contents }
end

# Hash of YAML-formatted metadata at the top of this file
def meta(filename)
  yaml = File.read(filename).split("\n\n")[0]
  Psych.load(yaml, symbolize_names: true)
end

# contents of filename after the meta header
def body(filename)
  File.read(filename).split("\n\n")[1..-1].join("\n\n").strip
end

def autolink(str)
  str.gsub(/(http\S*)/, '<a href="\1">\1</a>')
end

# if template.nil? use vars[:body]
def page(template, vars)
  # if no pagetitle, make one
  if vars[:pagetitle].nil?
    vars[:pagetitle] = 'Derek Sivers'
  else # but if there is one, add my name
    vars[:pagetitle] << ' | Derek Sivers'
  end
  # if no bodyid, make one
  if vars[:bodyid].nil?
    vars[:bodyid] = 'site'
  end
  v = Mustache.new
  vars.keys.each {|k| v[k] = vars[k]}
  html = v.render(File.read(ROOT + 'templates/header.mustache'))
  if template
    html << v.render(template)
  else
    html << vars[:body]
  end
  html << v.render(File.read(ROOT + 'templates/footer.mustache'))
  html
end