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

#copy(src, dst) ⇒ Object



8
9
10
11
12
13
# File 'lib/helpers-blog.rb', line 8

def copy(src, dst)
  log!(enter: __method__, args: [src, dst])
  cmd = "cp #{src} #{dst} 2>/dev/null"
  rc = system!(cmd)
  puts "    Failed: #{cmd} - from #{caller[0]}" unless rc
end

#copy!(src, dst) ⇒ Object



15
16
17
18
19
20
# File 'lib/helpers-blog.rb', line 15

def copy!(src, dst)
  log!(enter: __method__, args: [src, dst])
  cmd = "cp -r #{src} #{dst} 2>/dev/null"
  rc = system!(cmd)
  puts "    Failed: #{cmd} - from #{caller[0]}" unless rc
end

#create_dirs(*dirs) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/helpers-blog.rb', line 130

def create_dirs(*dirs)
  log!(enter: __method__, args: [*dirs])
  dirs.each do |dir|
    dir = dir.to_s  # symbols allowed
    next if Dir.exist?(dir)
    cmd = "mkdir -p #{dir} >/dev/null"
    result = system!(cmd) 
    raise CantCreateDir(dir) unless result
  end
end

#dump(obj, name) ⇒ Object



154
155
156
157
# File 'lib/helpers-blog.rb', line 154

def dump(obj, name)
  log!(enter: __method__, args: [obj, name])
  File.write(name, obj)
end

#error(err) ⇒ Object

Hmm, this is duplicated



147
148
149
150
151
152
# File 'lib/helpers-blog.rb', line 147

def error(err)  # Hmm, this is duplicated
  log!(str: "duplicated method", enter: __method__, args: [err])
  str = "\n  Error: #{err}"
  puts str
  puts err.backtrace.join("\n")
end

#find_draft_slugsObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/helpers-blog.rb', line 118

def find_draft_slugs
  log!(enter: __method__)
  files = Dir["#@root/drafts/**"].grep /\d{4}.*.lt3$/
  flagfile = "#@root/drafts/last_rebuild"
  last = File.exist?(flagfile) ? File.mtime(flagfile) : (Time.now - 86_400)
  files.reject! {|f| File.mtime(f) > last }
  files.map! {|f| File.basename(f) }
  files = files.sort.reverse
  debug "fss: #{files.inspect}"
  files
end

#get_viewsObject

read from filesystem



90
91
92
93
94
# File 'lib/helpers-blog.rb', line 90

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

#interpolate(str, bind) ⇒ Object



141
142
143
144
145
# File 'lib/helpers-blog.rb', line 141

def interpolate(str, bind)
  log!(enter: __method__, args: [str, bind])
  wrap = "<<-EOS\n#{str}\nEOS"
  eval wrap, bind
end

#new_dotfile(root: ".blogs", current_view: "test_view", editor: "vi") ⇒ Object



96
97
98
99
100
101
102
# File 'lib/helpers-blog.rb', line 96

def new_dotfile(root: ".blogs", current_view: "test_view", editor: "vi")
  log!(enter: __method__, args: [root, current_view, editor])
  root = Dir.pwd/root
  x = OpenStruct.new
  x.root, x.current_view, x.editor = root, current_view, editor
  write_config(x, root/RuneBlog::ConfigFile)
end

#new_sequenceObject



104
105
106
107
108
109
# File 'lib/helpers-blog.rb', line 104

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

#read_config(file, *syms) ⇒ Object

def get_root

  log!(enter: __method__)
  if $_blog
    if $_blog.root
      puts "0. Returned: #{$_blog.root}/"
      return $_blog.root + "/"
    else
      puts "1. Returned: ./"
      return "./"
    end
  else
    puts "2. Returned: ./"
    return "./"
  end
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/helpers-blog.rb', line 38

def read_config(file, *syms)
  log!(enter: __method__, args: [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)
    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 "Can't read config file '#{file}': #{err}"
  puts err.backtrace.join("\n")
  puts "dir = #{Dir.pwd}"
  exit
end

#subdirs(dir) ⇒ Object



111
112
113
114
115
116
# File 'lib/helpers-blog.rb', line 111

def subdirs(dir)
  log!(enter: __method__, args: [dir])
  dirs = Dir.entries(dir) - %w[. ..]
  dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") }
  dirs
end

#try_read_config(file, hash) ⇒ Object



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

def try_read_config(file, hash)
  log!(enter: __method__, args: [file, hash])
  return hash.values unless File.exist?(file)
  vals = read_config(file, *hash.keys)
  vals
end

#write_config(obj, file) ⇒ Object

def put_config(root:, view:“test_view”, editor: “/usr/local/bin/vim”)

log!(enter: __method__, args: [root, view, editor])
Dir.mkdir(root) unless Dir.exist?(root)
Dir.chdir(root) do 
  File.open("config", "w") do |cfg|
    cfg.puts "root: #{root}"
    cfg.puts "current_view: #{view}"
    cfg.puts "editor: #{editor}"
  end
end

end



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

def write_config(obj, file)
  log!(enter: __method__, args: [obj, file])
  hash = obj.to_h
  File.open(file, "w") do |out|
    hash.each_pair {|key, val| out.puts "#{key}: #{val}" }
  end
end