Module: RuneBlog::Helpers

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

Overview

Home = Dir.pwd # unless Home

Instance Method Summary collapse

Instance Method Details

#copy(src, dst) ⇒ Object



7
8
9
# File 'lib/helpers-blog.rb', line 7

def copy(src, dst)
  system("cp #{src}  #{dst}")
end

#copy!(src, dst) ⇒ Object



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

def copy!(src, dst)
  system("cp -r #{src}  #{dst}")
end

#create_dirs(*dirs) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/helpers-blog.rb', line 136

def create_dirs(*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



157
158
159
# File 'lib/helpers-blog.rb', line 157

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

#error(err) ⇒ Object

Hmm, this is duplicated



151
152
153
154
155
# File 'lib/helpers-blog.rb', line 151

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

#find_draft_slugsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/helpers-blog.rb', line 122

def find_draft_slugs
  verify(@root => "#@root is nil",
         Dir.exist?(@root) => "#@root doesn't exist",
         Dir.exist?("#@root/drafts") => "#@root/drafts doesn't exist")
  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_rootObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/helpers-blog.rb', line 21

def get_root
  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

#get_viewsObject

read from filesystem



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

def get_views   # read from filesystem
#   Dir.chdir(::Home) do
    verify(@root => "#@root is nil",
           Dir.exist?(@root) => "#@root doesn't exist",
           Dir.exist?("#@root/views") => "#@root/views doesn't exist")
    dirs = subdirs("#@root/views/").sort
    dirs.map {|name| RuneBlog::View.new(name) }
#   end
end

#interpolate(str, binding) ⇒ Object



146
147
148
149
# File 'lib/helpers-blog.rb', line 146

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

#livetext(src, dst) ⇒ Object



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

def livetext(src, dst)
  src << ".lt3" unless src.end_with?(".lt3")
  dst << ".html" unless src.end_with?(".html")
  system("livetext #{src} >#{dst}")
end

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



100
101
102
103
104
105
106
107
# File 'lib/helpers-blog.rb', line 100

def new_dotfile(root: ".blogs", current_view: "test_view", editor: "vi")
#   raise BlogAlreadyExists if Dir.exist?(".blogs")
#   Dir.mkdir(".blogs")
  root = Dir.pwd + "/" + root
  x = OpenStruct.new
  x.root, x.current_view, x.editor = root, current_view, editor
  write_config(x, ".blogs/" + RuneBlog::ConfigFile)
end

#new_sequenceObject



109
110
111
112
113
# File 'lib/helpers-blog.rb', line 109

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

#put_config(root:, view: "test_view", editor: "/bin/vim") ⇒ Object



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

def put_config(root:, view:"test_view", editor: "/bin/vim")
  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

#read_config(file, *syms) ⇒ Object



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

def read_config(file, *syms)
# puts "dir   = #{Dir.pwd}"
  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



115
116
117
118
119
120
# File 'lib/helpers-blog.rb', line 115

def subdirs(dir)
  verify(Dir.exist?(dir) => "Directory #{dir} not found")
  dirs = Dir.entries(dir) - %w[. ..]
  dirs.reject! {|x| ! File.directory?("#@root/views/#{x}") }
  dirs
end

#try_read_config(file, hash) ⇒ Object



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

def try_read_config(file, hash)
  return hash.values unless File.exist?(file)
  vals = read_config(file, *hash.keys)
# STDERR.puts vals.inspect
  vals
end

#write_config(obj, file) ⇒ Object



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

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