Module: RuneBlog::Helpers

Included in:
RuneBlog, RuneBlog, Post, View
Defined in:
lib/global.rb,
lib/helpers-blog.rb

Instance Method Summary collapse

Instance Method Details

#create_dir(dir) ⇒ Object



71
72
73
74
75
76
# File 'lib/helpers-blog.rb', line 71

def create_dir(dir)
  return if Dir.exist?(dir)  #  && File.directory?(dir)
  cmd = "mkdir -p #{dir} >/dev/null 2>&1"
  result = system(cmd) 
  raise CantCreateDir(dir) unless result
end

#dump(obj, name) ⇒ Object



89
90
91
# File 'lib/helpers-blog.rb', line 89

def dump(obj, name)
  File.write(name, obj)
end

#error(err) ⇒ Object

Hmm, this is duplicated



83
84
85
86
87
# File 'lib/helpers-blog.rb', line 83

def error(err)  # Hmm, this is duplicated
  str = "\n  Error: #{err}"
  puts str
  puts err.backtrace.join("\n")
end

#find_src_slugsObject



64
65
66
67
68
69
# File 'lib/helpers-blog.rb', line 64

def find_src_slugs
  files = Dir.entries("#@root/src/").grep /\d{4}.*.lt3$/
  files.map! {|f| File.basename(f) }
  files = files.sort.reverse
  files
end

#get_viewsObject

read from filesystem



36
37
38
39
# File 'lib/helpers-blog.rb', line 36

def get_views   # read from filesystem
  dirs = subdirs("#@root/views/").sort
  dirs.map {|name| RuneBlog::View.new(name) }
end

#interpolate(str) ⇒ Object



78
79
80
81
# File 'lib/helpers-blog.rb', line 78

def interpolate(str)
  wrap = "<<-EOS\n#{str}\nEOS"
  eval wrap
end

#new_dotfile(root: "data", current_view: "no_default", editor: "vi") ⇒ Object

Raises:

  • (BlogAlreadyExists)


41
42
43
44
45
46
47
# File 'lib/helpers-blog.rb', line 41

def new_dotfile(root: "data", current_view: "no_default", editor: "vi")
  raise BlogAlreadyExists if Dir.exist?(".blog")
  Dir.mkdir(".blog")
  x = OpenStruct.new
  x.root, x.current_view, x.editor = root, current_view, editor
  write_config(x, RuneBlog::DotDir + "/config")
end

#new_sequenceObject



49
50
51
52
53
# File 'lib/helpers-blog.rb', line 49

def new_sequence
  dump(0, "sequence")
  version_info = "#{RuneBlog::VERSION}\nBlog created: #{Time.now.to_s}"
  dump(version_info, "VERSION")
end

#read_config(file, *syms) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/helpers-blog.rb', line 3

def read_config(file, *syms)
  lines = File.readlines(file).map(&:chomp)
  obj = OpenStruct.new
  lines.each do |line|
    next if line == "\n" || line[0] == "#"
    key, val = line.split(" ", 2)
    key = key[0..-2] # remove colon
    obj.send(key+"=", val)
  end
  return obj if syms.empty?
  vals = []
  if syms.empty?
    vals = obj.to_hash.values
  else
    syms.each {|sym| vals << obj.send(sym) }
  end
  return vals
rescue => err
  puts "Something hit the fan: #{err}"
  puts err.backtrace.join("\n")
  puts "dir = #{Dir.pwd}"
  exit
end

#subdirs(dir) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/helpers-blog.rb', line 55

def subdirs(dir)
  dirs = Dir.entries(dir) - %w[. ..]
  dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") }
  dirs
rescue
  STDERR.puts "Can't find dir '#{dir}'"
  exit
end

#write_config(obj, file) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/helpers-blog.rb', line 27

def write_config(obj, file)
  hash = obj.to_h
  File.open(file, "w") do |f| 
    hash.each_pair do |key, val|
      f.puts "#{key}: #{val}"
    end
  end
end