Module: RuneBlog::Helpers

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

Instance Method Summary collapse

Instance Method Details

#copy_data(dest) ⇒ Object



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

def copy_data(dest)
  data  = RuneBlog::Path + "/../data"    # files kept inside gem
  files = %w[ROOT VIEW EDITOR universal.lt3 global.lt3 features.txt]
  files.each {|file| copy(data + "/" + file, dest) unless File.exist?(dest/file) }
end

#find_draft_slugsObject



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

def find_draft_slugs
  log!(enter: __method__, level: 3)
  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_all_widgets(dest) ⇒ Object



69
70
71
72
# File 'lib/helpers-blog.rb', line 69

def get_all_widgets(dest)
  wdir  = RuneBlog::Path + "/widgets"    # files kept inside gem
  copy!(wdir/"*", dest)
end

#get_repo_config(root = ".blogs") ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/helpers-blog.rb', line 57

def get_repo_config(root = ".blogs")
  log!(enter: __method__, level: 3)
  @editor = File.read("#{root}/data/EDITOR").chomp
  @current_view = File.read("#{root}/data/VIEW").chomp
  @root = File.read("#{root}/data/ROOT").chomp  
  # Huh? Why not just @root = root?  Hal.wtf?
rescue => err
  puts "Can't read config: #{err}"
  puts err.backtrace.join("\n")
  puts "dir = #{Dir.pwd}"
end

#get_widget(dest, widget: :all) ⇒ Object

recursive



74
75
76
77
78
79
80
81
82
# File 'lib/helpers-blog.rb', line 74

def get_widget(dest, widget: :all)   # recursive
  wdir  = ".blogs/widgets"
  wdir  = RuneBlog::Path + "/widgets"    # files kept inside gem
  if widget == :all
    copy!(wdir/"*", dest)
  else
    copy!(wdir/widget, dest)
  end
end

#new_sequenceObject



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

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

#quit_RubyTextObject



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

def quit_RubyText
  return unless defined? RubyText
  sleep 6
  RubyText.stop
  exit
end

#read_features(view = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/helpers-blog.rb', line 18

def read_features(view = nil)
  hash = {}
  root = @blog.root rescue ".blogs"
  if view.nil?  # toplevel
    dir = root/"data"
  else
    dir = root/:views/view/:settings
  end
  file = dir/"features.txt"
  pairs = read_pairs(file)
  enabled = {}
  pairs.each {|k,v| enabled[k] = (v == "1") }
  @features = enabled
end

#read_vars(file) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/helpers-blog.rb', line 90

def read_vars(file)
  log!(enter: __method__, args: [file], level: 3)
  lines = File.readlines(file).map(&:chomp)
  hash = {}
  skip = ["\n", "#", "."]
  lines.each do |line|
    line = line.strip
    next if skip.include?(line[0])
    key, val = line.split(" ", 2)
    next if key.nil?
    hash[key] = hash[key.to_sym] = val
  end
  hash
rescue => err
  puts "Can't read vars file '#{file}': #{err}"
  puts err.backtrace.join("\n")
  puts "dir = #{Dir.pwd}"
  stop_RubyText rescue nil
end

#subdirs(dir) ⇒ Object



124
125
126
127
128
129
# File 'lib/helpers-blog.rb', line 124

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

#write_features(hash, view = nil) ⇒ Object



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

def write_features(hash, view = nil)
  root = @blog.root rescue ".blogs"
  if view.nil?  # toplevel
    dir = root/"data"
  else
    dir = root/:views/view/:settings
  end
  file = dir/"features.txt"
  lines = File.readlines(file)
  names = hash.keys
  names.each do |name|
    n, item = find_item!(lines) {|x| x =~ /^#{name} / }
    k = name.length + 1  # 2nd blank after name
    loop { break if item[k] != " "; k += 1 }
    item[k] = hash[name]
    lines[n] = item
  end
  File.write(file, lines.join)
rescue => err
  puts "Error: #{err}"
  puts err.backtrace.join("\n")
  puts
end

#write_repo_config(root: "#{Dir.pwd}/.blogs", view: nil, editor: "/usr/local/bin/vim") ⇒ Object



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

def write_repo_config(root: "#{Dir.pwd}/.blogs", view: nil, editor: "/usr/local/bin/vim")
  view ||= File.read("#{root}/data/VIEW").chomp rescue "[no view]"
  File.write(root + "/data/ROOT",   root + "\n")
  File.write(root + "/data/VIEW",   view.to_s + "\n")
  File.write(root + "/data/EDITOR", editor + "\n")
end