~sivers/sive.rs

sive.rs/script/articles.rb -rwxr-xr-x 2.6 KiB
5fcf6634 — Derek Sivers cover and hardcover 19 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
require_relative 'init.rb'

files = Dir[ROOT + 'content/articles/*']
template = File.read(ROOT + 'templates/article.mustache')

# given tags, return parent link (tags: nocomments top10 ayw ymap hyon)
def parent(tags)
  if tags.nil?
    '<a href="/blog">Articles</a>'
  elsif tags.include?('ayw')
    'from the book “<a href="/a">Anything You Want</a>”'
  elsif tags.include?('ymap')
    'from the book “<a href="/m">Your Music and People</a>”'
  elsif tags.include?('hyon')
    'from the book “<a href="/n">Hell Yeah or No</a>”'
  else
    '<a href="/blog">Articles</a>'
  end
end

# given uri, does it have MP3 audio file?
def audio(uri)
  filename = 'sive.rs.%s.mp3' % uri
  File.exist?(MEDIA_DIR + filename) ? filename : nil
end

# get first <p> text. strip newlines, quotes, and tags
def get_description(txt)
  r = %r{<p>(.+?)</p>}m
  m = r.match(txt)
  return nil unless m
  m[1].gsub(/[\r\n\t"]/, ' ').strip.gsub(%r{</?[^>]+?>}, '')
end

# after articles sorted, add prev1 and next1
def add_prev_next(articles)
  articles.each_with_index do |a, i|
    articles[i][:prev1] = (i == 0) ? nil : articles[i - 1][:uri]
    articles[i][:next1] = (i == articles.size - 1) ? nil : articles[i + 1][:uri]
  end
  articles
end

def parse(fn)
  res = meta(fn)
  res[:body] = body(fn)
  res[:uri] = File.basename(fn)
  res[:year] = res[:date].year
  res[:parent] = parent(res[:tags])
  res[:audio] = audio(res[:uri])
  res[:bodyid] = 'article'
  res[:pagetitle] = res[:title].dup
  res[:pagedescription] = get_description(res[:body])
  res
end

articles = files.map {|fn| parse(fn)}.sort_by {|x| x[:date]}
articles = add_prev_next(articles)

articles.each do |article|
  wput(article[:uri], page(template, article))
end

vars = {}
vars[:bodyid] = 'blog'
vars[:pagetitle] = 'blog articles'
vars[:pagedescription] = 'all articles and blog posts'
vars[:articles] = articles.sort_by {|x| x[:date]}.reverse
wput('blog', page(File.read(ROOT + 'templates/blog.mustache'), vars))

atom = RSS::Maker.make('atom') do |f|
  f.channel.about = 'https://sive.rs/en.atom'
  f.channel.author = 'Derek Sivers'
  f.channel.title = 'Derek Sivers'
  f.channel.updated = DateTime.parse(articles[0][:date].to_s).to_s
  articles.reverse.each do |r|
    f.items.new_item do |e|
      e.link = 'https://sive.rs/' + r[:uri]
      e.updated = DateTime.parse(r[:date].to_s).to_s
      e.title = r[:title]
      e.content.type = 'html'
      # convert relative links to full links & strip tabs
      e.content.content = r[:body].gsub('href="/', 'href="https://sive.rs/').gsub("\t", '')
    end
  end
end
wput('en.atom', atom.to_s)

ARTICLES = articles.reverse