Top Level Namespace

Defined Under Namespace

Modules: ErrorChecks, PathSep Classes: RuneBlog

Constant Summary collapse

Already_publish =
nil
Already_runeblog =
nil
LIVE =
Livetext.new
LEXT =
".lt3"

Instance Method Summary collapse

Instance Method Details

#_blank(url) ⇒ Object



845
846
847
# File 'lib/liveblog.rb', line 845

def _blank(url)
  %[href='#{url}' target='blank']
end

#_card_generic(card_title:, middle:, extra: "") ⇒ Object



822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/liveblog.rb', line 822

def _card_generic(card_title:, middle:, extra: "")
  front = <<-HTML
    <div class="card #{extra} mb-3">
      <div class="card-body">
        <h5 class="card-title">#{card_title}</h5>
  HTML

  tail = <<-HTML
      </div>
    </div>
  HTML
  text = front + middle + tail
  _out text + "\n "
end

#_errout(*args) ⇒ Object



781
782
783
# File 'lib/liveblog.rb', line 781

def _errout(*args)
  ::STDERR.puts *args
end

#_get_data(file) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/lowlevel.rb', line 38

def _get_data(file)
  lines = File.readlines(file)
  lines = lines.map do |line|
    line = line.chomp.strip
    line.sub(/ *# .*$/, "")    # allow leading/trailing comments
  end
  lines.reject! {|x| x.empty? }
  lines
end

#_get_data?(file) ⇒ Boolean

File need not exist

Returns:

  • (Boolean)


34
35
36
# File 'lib/lowlevel.rb', line 34

def _get_data?(file)   # File need not exist
  File.exist?(file) ? _get_data(file) : []
end

#_got_python?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/liveblog.rb', line 60

def _got_python?
  # Dumb - fix later - check up front as needed
  # Should also check for praw lib
  str = `which python3`
  str.length > 0
end

#_handle_standard_widget(tag) ⇒ Object



571
572
573
574
575
576
577
578
579
580
# File 'lib/liveblog.rb', line 571

def _handle_standard_widget(tag)
  wtag = :widgets/tag
  code = _load_local(tag)
  if code 
    Dir.chdir(wtag) do 
      widget = code.new(@blog)
      widget.build
    end
  end
end

#_html_body(file, css = nil) ⇒ Object

helper methods



769
770
771
772
773
774
775
776
777
778
779
# File 'lib/liveblog.rb', line 769

def _html_body(file, css = nil)
  file.puts "<html>"
  if css
    file.puts "    <head>"  
    file.puts "        <style>\n#{css}\n          </style>"
    file.puts "    </head>"  
  end
  file.puts "  <body>"
  yield
  file.puts "  </body>\n</html>"
end

#_load_local(widget) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/liveblog.rb', line 557

def _load_local(widget)
  Dir.chdir("widgets/#{widget}") do
    rclass = _make_class_name(widget)
    found = (require("./#{widget}") if File.exist?("#{widget}.rb"))
    code = found ? ::RuneBlog::Widget.class_eval(rclass) : nil
    code
  end
rescue => err
  STDERR.puts err.to_s
  STDERR.puts err.backtrace.join("\n")
  sleep 6; RubyText.stop
  exit
end

#_main(url) ⇒ Object



841
842
843
# File 'lib/liveblog.rb', line 841

def _main(url)
  %[href="javascript: void(0)" onclick="javascript:open_main('#{url}')"]
end

#_make_class_name(app) ⇒ Object



547
548
549
550
551
552
553
554
555
# File 'lib/liveblog.rb', line 547

def _make_class_name(app)
  if app =~ /[-_]/
    words = app.split(/[-_]/)
    name = words.map(&:capitalize).join
  else
    name = app.capitalize
  end
  return name
end

#_make_navbar(orient = :horiz) ⇒ Object



714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/liveblog.rb', line 714

def _make_navbar(orient = :horiz)
  vdir = @root/:views/@blog.view
  title = _var("view.title")

  if orient == :horiz
    name = "navbar.html"
    li1, li2 = "", ""
    extra = "navbar-expand-lg" 
    list1 = list2 = ""
  else
    name = "vnavbar.html"
    li1, li2 = '<li class="nav-item">', "</li>"
    extra = ""
    list1, list2 = '<l class="navbar-nav mr-auto">', "</ul>"
  end
  
  start = <<-HTML
   <table><tr><td>
   <nav class="navbar #{extra} navbar-light bg-light">
      #{list1}
  HTML
  finish = <<-HTML
      #{list2}
    </nav>
    </td></tr></table>
  HTML

  html_file = @blog.root/:views/@blog.view/"themes/standard/banner/navbar"/name
  output = File.new(html_file, "w")
  output.puts start
  lines = _read_navbar_data
  lines = ["index  Home"] + lines  unless _args.include?("nohome")
  lines.each do |line|
    basename, cdata = line.chomp.strip.split(" ", 2)
    full = :banner/:navbar/basename+".html"
    href_main = _main(full)
    if basename == "index"  # special case
      output.puts %[#{li1} <a class="nav-link" href="index.html">#{cdata}<span class="sr-only">(current)</span></a> #{li2}]
    else
      dir = @blog.root/:views/@blog.view/"themes/standard/banner/navbar"
      dest = vdir/"remote/banner/navbar"/basename+".html"
      preprocess cwd: dir, src: basename, dst: dest, call: ".nopara" # , debug: true
      output.puts %[#{li1} <a class="nav-link" #{href_main}>#{cdata}</a> #{li2}]
    end
  end
  output.puts finish
  output.close
  return File.read(html_file)
end

#_passthru(line) ⇒ Object



785
786
787
788
789
790
# File 'lib/liveblog.rb', line 785

def _passthru(line)
  return if line.nil?
  line = _format(line)
  _out line + "\n"
  _out "<p>" if line.empty? && ! @_nopara
end

#_passthru_noline(line) ⇒ Object



792
793
794
795
796
797
# File 'lib/liveblog.rb', line 792

def _passthru_noline(line)
  return if line.nil?
  line = _format(line)
  _out line
  _out "<p>" if line.empty? && ! @_nopara
end

#_post_lookup(postid) ⇒ Object

side-effect



808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/liveblog.rb', line 808

def _post_lookup(postid)    # side-effect
  # .. = templates, ../.. = views/thisview
  slug = title = date = teaser_text = nil

  dir_posts = @vdir/:posts
  posts = Dir.entries(dir_posts).grep(/^\d\d\d\d/).map {|x| dir_posts/x }
  posts.select! {|x| File.directory?(x) }

  posts = posts.select {|x| File.basename(x).to_i == postid }
  postdir = exactly_one(posts)
  vp = RuneBlog::ViewPost.new(@blog.view, postdir)
  vp
end

#_read_navbar_dataObject



150
151
152
153
154
155
# File 'lib/liveblog.rb', line 150

def _read_navbar_data
  vdir = @blog.root/:views/@blog.view
  dir = vdir/"themes/standard/banner/navbar/"
  datafile = dir/"list.data"
  _get_data(datafile)
end

#_reddit_post_url(vdir, date, title, url) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/liveblog.rb', line 67

def _reddit_post_url(vdir, date, title, url)
  _got_python?
  tmpfile = "/tmp/reddit-post-url.txt"
  File.open(tmpfile, "w") do |tmp|
    tmp.puts "[#{date}]  #{title}"
    tmp.puts url
  end
  rid = nil
  Dir.chdir(vdir/:config) { rid = `python3 reddit/reddit_post_url.py` }
  system("rm #{tmpfile}")
  rid  # returns reddit id
end

#_svg_title(*args) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/liveblog.rb', line 221

def _svg_title(*args)
  width    = "95%"
  height   = 90
  bgcolor  = "black"
  style    = nil
  size     = ""
  font     = "sans-serif"
  color    = "white"
  xy       = "5,5"
  align    = "center"
  style2   = nil
  size2    = ""
  font2    = "sans-serif"
  color2   = "white"
  xy2      = "5,5"
  align2   = "center"

  e = args.each
  hash = {}  # TODO get rid of hash??

  valid = %w[width height bgcolor style size font color xy 
             align style2 size2 font2 color2 xy2 align2]
  os = OpenStruct.new
  loop do
    arg = e.next
    arg = arg.chop
    raise "Don't know '#{arg}'" unless valid.include?(arg)
    os.send(arg+"=", e.next)
  end
  x, y = xy.split(",")
  x2, y2 = xy2.split(",")
  names = %w[x y x2 y2] + valid
  names.each {|name| hash[name] = os.send(name) }
  result = <<~HTML
    <svg width="#{width}" height="#{height}"
         viewBox="0 0 #{width} #{height}">
      <defs>
        <linearGradient id="grad1" x1="100%" y1="100%" x2="0%" y2="100%">
          <stop offset="0%" style="stop-color:rgb(198,198,228);stop-opacity:1" />
          <stop offset="100%" style="stop-color:rgb(30,30,50);stop-opacity:1" />
        </linearGradient>
      </defs>
      <style>
        .title    { font: #{style} #{size} #{font}; fill: #{color} }
        .subtitle { font: #{style2} #{size2} #{font2}; fill: #{color2} }
      </style>
      <rect x="10" y="10" rx="10" ry="10" width="#{width}" height="#{height}" fill="url(#grad1)"/>
      <text text-anchor="#{align}"  x="#{x}" y="#{y}" class="title">#{Livetext::Vars["view.title"]} </text>
      <text text-anchor="#{align2}" x="#{x2}" y="#{y2}" class="subtitle">#{Livetext::Vars["view.subtitle"]} </text>
    </svg> 
    <!-- ^ how does syntax highlighting get messed up? </svg> -->
  HTML
  [result, hash]
end

#_tmp_error(err) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/lowlevel.rb', line 2

def _tmp_error(err)
  out = "/tmp/blog#{rand(100)}.txt"
  File.open(out, "w") do |f|
    f.puts err.to_s + "\n--------"
    f.puts err.backtrace.join("\n")
  end
  puts "Error: See #{out}"
end

#_var(name) ⇒ Object

FIXME scope issue!



837
838
839
# File 'lib/liveblog.rb', line 837

def _var(name)  # FIXME scope issue!
  ::Livetext::Vars[name] || "[:#{name} is undefined]"
end

#_write_metadataObject



799
800
801
802
803
804
805
806
# File 'lib/liveblog.rb', line 799

def 
  File.write("teaser.txt", @meta.teaser)
  fields = [:num, :title, :date, :pubdate, :views, :tags, :pinned]
  fname = "metadata.txt"
  File.open(fname, "w") do |f| 
    fields.each {|fld| f.puts "#{'%8s' % fld}  #{@meta.send(fld)}" }
  end
end


141
142
143
# File 'lib/liveblog.rb', line 141

def backlink
  _out %[<br><a href="javascript:history.go(-1)">[Back]</a>]
end


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/liveblog.rb', line 157

def banner
  count = 0
  bg = "white"  # outside loop
  wide = nil
  high = 250
  str2 = ""
  navbar = nil
  vdir = @blog.root/:views/@blog.view
  lines = _body.to_a

  lines.each do |line|
    count += 1
    tag, *data = line.split
    data ||= []
    case tag
      when "width";   wide = data[0]
      when "height";  high = data[0]
      when "bgcolor"; bg = data[0] || "white"
      when "image"
        image = data[0] || "banner.jpg"
        image = "banner"/image
        wide = data[0]
        width = wide ? "width=#{wide}" : "" 
        str2 << "      <td><img src=#{image} #{width} height=#{high}></img></td>" + "\n"
      when "svg_title"
        stuff, hash = _svg_title(*data)
        wide = hash["width"]
        str2 << "      <td width=#{wide}>#{stuff}</td>" + "\n"
      when "text"
        data[0] ||= "top.html"
        file = "banner"/data[0]
        if ! File.exist?(file) 
          src = file.sub(/html$/, "lt3")
          if File.exist?(src)
            preprocess src: src, dst: file, call: ".nopara" # , vars: @blog.view.globals
          else
            raise "Neither #{file} nor #{src} found"
          end
        end
        str2 << "<td>" + File.read(file) + "</td>" + "\n"
      when "navbar"
        navbar = _make_navbar  # horiz is default
      when "vnavbar"
        navbar = _make_navbar(:vert)
      when "break"
         str2 << "  </tr>\n  <tr>"  + "\n"
    else
      str2 << "        '#{tag}' isn't known" + "\n"
    end
  end
  _out <<~HTML
    <table width=100% bgcolor=#{bg}>
      <tr>
        #{str2}
      </tr>
    </table>
  HTML
  _out navbar if navbar
rescue => err
  STDERR.puts "err = #{err}"
  STDERR.puts err.backtrace.join("\n")
  gets
end

#categoriesObject

does nothing right now



283
284
# File 'lib/liveblog.rb', line 283

def categories   # does nothing right now
end

#codeObject



145
146
147
148
# File 'lib/liveblog.rb', line 145

def code
  lines = _body # _text
  _out "<font size=+1><pre>\n#{lines}\n</pre></font>"
end

#copy(src, dst) ⇒ Object



70
71
72
73
74
# File 'lib/lowlevel.rb', line 70

def copy(src, dst)
  log!(enter: __method__, args: [src, dst], level: 2)
  cmd = "cp #{src} #{dst} 2>/dev/null"
  system!(cmd)
end

#copy!(src, dst) ⇒ Object



76
77
78
79
80
# File 'lib/lowlevel.rb', line 76

def copy!(src, dst)
  log!(enter: __method__, args: [src, dst], level: 2)
  cmd = "cp -r #{src} #{dst} 2>/dev/null"
  system!(cmd)
end

#create_dirs(*dirs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/lowlevel.rb', line 82

def create_dirs(*dirs)
  log!(enter: __method__, args: [*dirs], level: 3)
  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

#dropcapObject

“dot” commands



44
45
46
47
48
49
50
51
52
# File 'lib/liveblog.rb', line 44

def dropcap
  # Bad form: adds another HEAD
  text = _data
  _out " "
  letter = text[0]
  remain = text[1..-1]
  _out %[<div class='mydrop'>#{letter}</div>]
  _out %[<div style="padding-top: 1px">#{remain}]
end

#dump(obj, name) ⇒ Object



11
12
13
# File 'lib/lowlevel.rb', line 11

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

#error(err) ⇒ Object



99
100
101
102
103
104
# File 'lib/lowlevel.rb', line 99

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

#exactly_one(list) ⇒ Object



106
107
108
109
110
# File 'lib/lowlevel.rb', line 106

def exactly_one(list)
  raise "List: Zero instances" if list.empty?
  raise "List: More than one instance" if list.size > 1
  list.first
end

#faqObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/liveblog.rb', line 127

def faq
  @faq_count ||= 0
  _out "<br>" if @faq_count == 0
  @faq_count += 1
  ques = _data.chomp
  ans  = _body.join("\n")
  id = "faq#@faq_count"
  _out %[&nbsp;<a data-toggle="collapse" href="##{id}" role="button" aria-expanded="false" aria-controls="collapseExample"><font size=+3>&#8964;</font></a>]
  _out %[&nbsp;<b>#{ques}</b>]
  _out %[<div class="collapse" id="#{id}"><br><font size=+1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{ans}</font></div>\n]
  _out "<br>" # unless @faq_count == 1
  _optional_blank_line
end

#finalizeObject



452
453
454
455
456
457
458
459
460
461
# File 'lib/liveblog.rb', line 452

def finalize
  return unless @meta
  return @meta if @blog.nil?

  @slug = @blog.make_slug(@meta)
  slug_dir = @slug
  @postdir = @blog.view.dir/:posts/slug_dir
  write_post
  @meta
end

#get_live_vars(src) ⇒ Object



54
55
56
57
58
# File 'lib/processing.rb', line 54

def get_live_vars(src)
  live = Livetext.customize(call: [".nopara"])
  live.xform_file(src)
  live
end

#h1Object

Move elsewhere later!



293
# File 'lib/liveblog.rb', line 293

def h1; _passthru "<h1>#{@_data}</h1>"; end

#h2Object



294
# File 'lib/liveblog.rb', line 294

def h2; _passthru "<h2>#{@_data}</h2>"; end

#h3Object



295
# File 'lib/liveblog.rb', line 295

def h3; _passthru "<h3>#{@_data}</h3>"; end

#h4Object



296
# File 'lib/liveblog.rb', line 296

def h4; _passthru "<h4>#{@_data}</h4>"; end

#h5Object



297
# File 'lib/liveblog.rb', line 297

def h5; _passthru "<h5>#{@_data}</h5>"; end

#h6Object



298
# File 'lib/liveblog.rb', line 298

def h6; _passthru "<h6>#{@_data}</h6>"; end

#headObject

Does NOT output <head> tags



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/liveblog.rb', line 463

def head  # Does NOT output <head> tags
  args = _args
  args.each do |inc|
    self.data = inc
    _include
  end
  # Depends on vars: title, desc, host
  defaults = {}
  defaults = { "charset"        => %[<meta charset="utf-8">],
               "http-equiv"     => %[<meta http-equiv="X-UA-Compatible" content="IE=edge">],
               "title"          => %[<title>\n  #{_var("view.title")} | #{_var("view.subtitle")}\n  </title>],
               "generator"      => %[<meta name="generator" content="Runeblog v #@version">],
               "og:title"       => %[<meta property="og:title" content="#{_var("view.title")}">],
               "og:locale"      => %[<meta property="og:locale" content="#{_var(:locale)}">],
               "description"    => %[<meta name="description" content="#{_var("view.subtitle")}">],
               "og:description" => %[<meta property="og:description" content="#{_var("view.subtitle")}">],
               "linkc"          => %[<link rel="canonical" href="#{_var(:host)}">],
               "og:url"         => %[<meta property="og:url" content="#{_var(:host)}">],
               "og:site_name"   => %[<meta property="og:site_name" content="#{_var("view.title")}">],
#              "style"          => %[<link rel="stylesheet" href="etc/blog.css">],
# ^ FIXME
               "feed"           => %[<link type="application/atom+xml" rel="alternate" href="#{_var(:host)}/feed.xml" title="#{_var("view.title")}">],
               "favicon"        => %[<link rel="shortcut icon" type="image/x-icon" href="etc/favicon.ico">\n <link rel="apple-touch-icon" href="etc/favicon.ico">]
             }
  result = {}
  lines = _body
  lines.each do |line|
    line.chomp
    word, remain = line.split(" ", 2)
    case word
      when "viewport"
        result["viewport"] = %[<meta name="viewport" content="#{remain}">]
      when "script"  # FIXME this is broken
        file = remain
        text = File.read(file)
        result["script"] = Livetext.new.transform(text)
      when "style"
        result["style"] = %[<link rel="stylesheet" href="etc/#{remain}">]
      # Later: allow other overrides
      when ""; break
    else
      if defaults[word]
        result[word] = %[<meta property="#{word}" content="#{remain}">]
      else
        puts "Unknown tag '#{word}'"
      end
    end
  end
  hash = defaults.dup.update(result)  # FIXME collisions?

  hash.each_value {|x| _out "  " + x }
end

#hnavbarObject



706
707
708
# File 'lib/liveblog.rb', line 706

def hnavbar
  str = _make_navbar  # horiz is default
end

#hrObject



300
# File 'lib/liveblog.rb', line 300

def hr; _passthru "<hr>"; end

#imageObject

primitive so far



386
387
388
389
390
391
392
# File 'lib/liveblog.rb', line 386

def image   # primitive so far
  _debug "img: huh? <img src=#{_args.first}></img>"
  fname = _args.first
  path = :assets/fname
  _out "<img src=#{path}></img>"
  _optional_blank_line
end

#init_liveblogObject

FIXME - a lot of this logic sucks



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
# File 'lib/liveblog.rb', line 14

def init_liveblog    # FIXME - a lot of this logic sucks
  dir = Dir.pwd.sub(/\.blogs.*/, "")
  @blog = nil
  Dir.chdir(dir) { @blog = RuneBlog.new }
  @root = @blog.root
  @view = @blog.view
  @view_name = @blog.view.name unless @view.nil?
  @vdir = @blog.view.dir rescue "NONAME"
  @version = RuneBlog::VERSION
  @theme = @vdir/:themes/:standard

  @reddit_comments = ""
  @reddit_enabled = @blog.features["reddit"]
  if @reddit_enabled
    @reddit_comments = <<~HTML
      <a href="#reddit_comments">
        <img src="assets/reddit-logo.png" 
             width=24 height=24
             alt="Scroll to reddit comments"></img>
      </a>
    HTML
  end
rescue
  raise "Only works inside a blog repo"
end

#insetObject

inset



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/liveblog.rb', line 334

def inset
  lines = _body
  box = ""
  output = []
  lines.each do |line| 
    line = line
    case line[0]
      when "/"  # Only into inset
        line[0] = ' '
        box << line
        line.replace(" ")
      when "|"  # Into inset and body
        line[0] = ' '
        box << line
        output << line
    else  # Only into body
      output << line 
    end
  end
  lr = _args.first
  wide = _args[1] || "25"
  stuff = "<div style='float:#{lr}; width: #{wide}%; padding:8px; padding-right:12px'>"
  stuff << '<b><i>' + box + '</i></b></div>'
  _out "</p>"   #  kludge!! nopara
  0.upto(2) {|i| _passthru output[i] }
  _passthru stuff
  3.upto(output.length-1) {|i| _passthru output[i] }
  _out "<p>"  #  kludge!! para
  _optional_blank_line
end

#interpolate(str, bind) ⇒ Object



93
94
95
96
97
# File 'lib/lowlevel.rb', line 93

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

#listObject



309
310
311
312
313
314
# File 'lib/liveblog.rb', line 309

def list
  _out "<ul>"
  _body {|line| _out "<li>#{line}</li>" }
  _out "</ul>"
  _optional_blank_line
end

#list!Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/liveblog.rb', line 316

def list!
  _out "<ul>"
  lines = _body.each 
  loop do 
    line = lines.next
    line = _format(line)
    if line[0] == " "
      _out line
    else
      _out "<li>#{line}</li>"
    end
  end
  _out "</ul>"
  _optional_blank_line
end

#log!(str: "", enter: nil, args: [], pwd: false, dir: false, level: 0, stderr: false) ⇒ Object



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
# File 'lib/logging.rb', line 10

def log!(str: "", enter: nil, args: [], pwd: false, dir: false, level: 0, stderr: false)
  return unless $logging
  @err_level ||= ENV['RUNEBLOG_ERROR_LEVEL']
  @err_level ||= 0
  return if level < @err_level 

  time = Time.now.strftime("%H:%M:%S")

  meth = ""
  meth = "#{enter}" if enter

  para = "(#{args.inspect[1..-2]})"

  source = caller[0].sub(/.*\//, " in ").sub(/:/, " line ").sub(/:.*/, "")
  source = "in #{source} (probably liveblog.rb)" if source.include? "(eval)"

  str = "  ... #{str}" unless str.empty?
  indent = " "*12

  outlog "#{time} #{meth}#{para}"
  outlog "#{indent} #{str} " unless str.empty?
  outlog "#{indent} #{source}"
  outlog "#{indent} pwd = #{Dir.pwd} " if pwd
  if dir
    files = (Dir.entries('.') - %w[. ..]).join(" ")
    outlog "#{indent} dir/* = #{files}"
  end
#   outlog "#{indent} livetext params = #{livedata.inpect} " unless livedata.nil?
  outlog 
  $log.close
  $log = File.new("/tmp/runeblog.log","a")
end

#make_exception(sym, str, target_class = Object) ⇒ Object

Refactor, move stuff elsewhere?



33
34
35
36
37
38
39
40
41
42
# File 'lib/runeblog_version.rb', line 33

def make_exception(sym, str, target_class = Object)
  return if target_class.constants.include?(sym)

  target_class.const_set(sym, StandardError.dup)
  define_method(sym) do |*args|
    msg = str.dup
    args.each.with_index {|arg, i| msg.sub!("$#{i+1}", arg) }
    target_class.class_eval(sym.to_s).new(msg)
  end
end

#metaObject

newer stuff…



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/liveblog.rb', line 518

def meta
  args = _args
  enum = args.each
  str = "<meta"
  arg = enum.next
  loop do 
    if arg.end_with?(":")
      str << " " << arg[0..-2] << "="
      a2 = enum.next
      str << %["#{a2}"]
    else
      STDERR.puts "=== meta error?"
    end
    arg = enum.next
  end
  str << ">"
  _out str
end


710
711
712
# File 'lib/liveblog.rb', line 710

def navbar
  str = _make_navbar  # horiz is default
end

#newer?(f1, f2) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/processing.rb', line 8

def newer?(f1, f2)
  File.mtime(f1) > File.mtime(f2)
end

#nlistObject



302
303
304
305
306
307
# File 'lib/liveblog.rb', line 302

def nlist
  _out "<ol>"
  _body {|line| _out "<li>#{line}</li>" }
  _out "</ol>"
  _optional_blank_line
end

#outlog(str = "", stderr: false) ⇒ Object



5
6
7
8
# File 'lib/logging.rb', line 5

def outlog(str = "", stderr: false)
  $log.puts str
  STDERR.puts str if stderr
end

#pinObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/liveblog.rb', line 408

def pin
  raise "'post' was not called" unless @meta
  _debug "data = #{_args}"  # verify only valid views?
  pinned = @_args
  @meta.pinned = pinned
  pinned.each do |pinview|
    dir = @blog.root/:views/pinview/"themes/standard/widgets/pinned/"
    datafile = dir/"list.data"
    pins = _get_data?(datafile)
    pins << "#{@meta.num} #{@meta.title}\n"
    pins.uniq!
    File.open(datafile, "w") {|out| pins.each {|pin| out.puts pin } }
  end
  _optional_blank_line
rescue => err
  STDERR.puts "err = #{err}"
  STDERR.puts err.backtrace.join("\n")
  gets
end

#postObject



54
55
56
57
58
# File 'lib/liveblog.rb', line 54

def post
  @meta = OpenStruct.new
  @meta.num = _args[0]
  _out "  <!-- Post number #{@meta.num} -->\n "
end

#post_toolbarObject



80
81
82
83
84
85
86
# File 'lib/liveblog.rb', line 80

def post_toolbar
  back_icon = %[<img src="assets/back-icon.png" width=24 height=24 alt="Go back"></img>]
  back = %[<a style="text-decoration: none" href="javascript:history.go(-1)">#{back_icon}</a>]
  _out <<~HTML
    <div align='right'>#{back} #@reddit_comments</div>
  HTML
end

#post_trailerObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/liveblog.rb', line 88

def post_trailer
  perma = _var("publish.proto") + "://" + _var("publish.server") +
          "/" + _var("publish.path") + "/" + _var("post.aslug") + 
          ".html"
Livetext::Vars.each_pair do |k,v|
  STDERR.puts "#{k}: #{v.inspect}" unless k.is_a? Symbol
end
  tags = _var("post.tags")
  taglist = tags.empty? ? "" : "Tags: #{tags}"

  reddit_txt = ""
  if @reddit_enabled
    vdir  = @blog.root/:views/@blog.view
    nslug = "#{_var("post.num")}-#{_var("post.aslug")}"
    date  = _var("post.date")
    rid_file = vdir/:posts/nslug/"reddit.id"
    rid = File.exist?(rid_file) ? File.read(rid_file).chomp : nil
    if rid.nil?
      title = _var("title")
      rid = _reddit_post_url(vdir, date, title, perma)
      dump(rid, rid_file)
    end
    reddit_txt = <<~HTML
      <a name='reddit_comments'>
      <script src='https://redditjs.com/post.js' 
              data-url="#{rid}" data-width=800 ></script>
    HTML
  # damned syntax highlighting </>
  end
  _out <<~HTML
  #{reddit_txt}
  <hr>
  <table width=100%><tr>
    <td width=10%><a style="text-decoration: none" href="javascript:history.go(-1)">[Back]</a></td>
    <td width=10%><a style="text-decoration: none" href="#{perma}"> [permalink] </a></td>
    <td width=80% align=right><font size=-3>#{taglist}</font></td></tr></table>
  HTML
end

#prefix(num) ⇒ Object



44
45
46
47
# File 'lib/runeblog_version.rb', line 44

def prefix(num)
  log!(enter: __method__, args: [num], level: 3)
  "#{'%04d' % num.to_i}"
end

#preprocess(cwd: Dir.pwd, src:, dst: nil, strip: false, deps: [], copy: nil, debug: false, force: false, mix: [], call: [], vars: {}) ⇒ Object



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
# File 'lib/processing.rb', line 23

def preprocess(cwd: Dir.pwd, src:, 
          dst: nil, strip: false,
          deps: [], copy: nil, debug: false, force: false, 
          mix: [], call: [], vars: {})
  src += LEXT unless src.end_with?(LEXT)
  if strip
    dst = File.basename(src).sub(/.lt3$/,"")
  else
    dst += ".html" unless dst.end_with?(".html")
  end
  sp = " "*12
  Dir.chdir(cwd) do
    if debug
      STDERR.puts "#{sp} -- preprocess "
      STDERR.puts "#{sp}      src:  #{src}"
      STDERR.puts "#{sp}      dst:  #{dst}"
      STDERR.puts "#{sp}      in:   #{Dir.pwd}"
      STDERR.puts "#{sp}      from: #{caller[0]}"
      STDERR.puts "#{sp}      copy: #{copy}" if copy
    end
    stale = stale?(src, dst, deps, force)
    if stale
      live = Livetext.customize(mix: "liveblog", call: call, vars: vars)
      out = live.xform_file(src)
      File.write(dst, out)
      system!("cp #{dst} #{copy}") if copy
    end
    puts "#{sp} -- ^ Already up to date!" if debug && ! stale
  end
end

#pubdateObject



374
375
376
377
378
379
380
381
382
383
384
# File 'lib/liveblog.rb', line 374

def pubdate
  raise "'post' was not called" unless @meta
  _debug "data = #@_data"
  # Check for discrepancy?
  match = /(\d{4}).(\d{2}).(\d{2})/.match @_data
  junk, y, m, d = match.to_a
  y, m, d = y.to_i, m.to_i, d.to_i
  @meta.date = ::Date.new(y, m, d)
  @meta.pubdate = "%04d-%02d-%02d" % [y, m, d]
  _optional_blank_line
end

#quoteObject



276
277
278
279
280
281
# File 'lib/liveblog.rb', line 276

def quote
  _passthru "<blockquote>"
  _passthru _body.join(" ")
  _passthru "</blockquote>"
  _optional_blank_line
end

#read_pairs(file) ⇒ Object

returns a hash



48
49
50
51
52
53
54
55
56
57
# File 'lib/lowlevel.rb', line 48

def read_pairs(file)       # returns a hash
  lines = _get_data(file)
  hash = {}
  lines.each do |line|
    key, val = line.split(" ", 2)
    val ||= ""
    hash[key] = val
  end
  hash
end

#read_pairs!(file) ⇒ Object

returns an openstruct



59
60
61
62
63
64
65
66
67
68
# File 'lib/lowlevel.rb', line 59

def read_pairs!(file)       # returns an openstruct
  lines = _get_data(file)
  obj = OpenStruct.new
  lines.each do |line|
    key, val = line.split(" ", 2)
    val ||= ""
    obj.send("#{key}=", val)
  end
  obj
end

#recent_postsObject

side-effect



537
538
539
540
541
542
543
544
545
# File 'lib/liveblog.rb', line 537

def recent_posts    # side-effect
  _out <<-HTML
    <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
      <iframe id="main" style="width: 70vw; height: 100vh; position: relative;" 
       src='recent.html' width=100% frameborder="0" allowfullscreen>
      </iframe>
    </div>
  HTML
end

#scriptObject



630
631
632
633
634
635
636
# File 'lib/liveblog.rb', line 630

def script
  lines = _body
  url = lines[0]
  integ = lines[1]
  cross = lines[2] || "anonymous"
  _out %[<script src="#{url}" integrity="#{integ}" crossorigin="#{cross}"></script>]
end


582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/liveblog.rb', line 582

def sidebar
  _debug "--- handling sidebar\r"
  if _args.include? "off"
    _body { }  # iterate, do nothing
    return 
  end

  _out %[<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">]

  standard = %w[pinned pages links news]

  _body do |token|
    tag = token.chomp.strip.downcase
    wtag = :widgets/tag
    raise "Can't find #{wtag}" unless Dir.exist?(wtag)
    tcard = "#{tag}-card.html"

    case
      when standard.include?(tag)
        _handle_standard_widget(tag)
      when tag == "ad"
        num = rand(1..4)
        img = "widgets/ad/ad#{num}.png"
        src, dst = img, @root/:views/@view_name/"remote/widgets/ad/"
        system!("cp #{src} #{dst}")
        File.open(wtag/"vars.lt3", "w") {|f| f.puts ".set ad.image = #{img}" }
        preprocess cwd: wtag, src: tag, dst: tcard, call: ".nopara", 
                   force: true # , debug: true # , deps: depend 
    end

    _include_file wtag/tcard
  end
  _out %[</div>]
rescue => err
  puts "err = #{err}"
  puts err.backtrace.join("\n")
  sleep 6; RubyText.stop
  exit
end

#stale?(src, dst, deps, force = false) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
# File 'lib/processing.rb', line 12

def stale?(src, dst, deps, force = false)
  meh = File.new("/tmp/dammit-#{src.gsub(/\//, "-")}", "w")
  log!(enter: __method__, args: [src, dst], level: 3)
  raise "Source #{src} not found in #{Dir.pwd}" unless File.exist?(src)
  return true if force
  return true unless File.exist?(dst)
  return true if newer?(src, dst)
  deps.each {|dep| return true if newer?(dep, dst) }
  return false
end

#styleObject



286
287
288
289
# File 'lib/liveblog.rb', line 286

def style
  fname = _args[0]
  _passthru %[<link rel="stylesheet" href="???/etc/#{fname}')">]
end

#stylesheetObject



622
623
624
625
626
627
628
# File 'lib/liveblog.rb', line 622

def stylesheet
  lines = _body
  url = lines[0]
  integ = lines[1]
  cross = lines[2] || "anonymous"
  _out %[<link rel="stylesheet" href="#{url}" integrity="#{integ}" crossorigin="#{cross}"></link>]
end

#system!(str, show: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lowlevel.rb', line 19

def system!(str, show: false)
  log!(enter: __method__, args: [str], level: 2)
  STDERR.puts str if show
  rc = system(str)
  return rc if rc
  STDERR.puts "FAILED: #{str.inspect}"
  STDERR.puts "\ncaller = \n#{caller.join("\n  ")}\n"
  if defined?(RubyText)
    sleep 6
    RubyText.stop
    exit
  end
  return rc
end

#tag_cloudObject



679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/liveblog.rb', line 679

def tag_cloud
  title = _data
  title = "Tag Cloud" if title.empty?
  open = <<-HTML
        <div class="card mb-3">
          <div class="card-body">
            <h5 class="card-title">
              <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#tag-cloud">+</button>
              #{title}
            </h5>
            <div class="collapse" id="tag-cloud">
  HTML
  _out open
  _body do |line|
    line.chomp!
    url, classname, cdata = line.split(",", 3)
    main = _main(url)
    _out %[<a #{main} class="#{classname}">#{cdata}</a>]
  end
  close = %[       </div>\n    </div>\n  </div>]
  _out close
end

#tagsObject



394
395
396
397
398
399
# File 'lib/liveblog.rb', line 394

def tags
  raise "'post' was not called" unless @meta
  _debug "args = #{_args}"
  @meta.tags = _args.dup || []
  _optional_blank_line
end

#teaserObject



438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/liveblog.rb', line 438

def teaser
  raise "'post' was not called" unless @meta
  text = _body.join("\n")
  @meta.teaser = text
  setvar :teaser, @meta.teaser
  if _args[0] == "dropcap"   # FIXME doesn't work yet!
    letter, remain = text[0], text[1..-1]
    _out %[<div class='mydrop'>#{letter}</div>]
    _out %[<div style="padding-top: 1px">#{remain}] + "\n"
  else
    _out @meta.teaser + "\n"
  end
end

#timelog(line, file) ⇒ Object



15
16
17
# File 'lib/lowlevel.rb', line 15

def timelog(line, file)
  File.open(file, "a") {|f| f.puts "#{Time.now} #{line}" }
end

#titleObject



365
366
367
368
369
370
371
372
# File 'lib/liveblog.rb', line 365

def title
  raise "'post' was not called" unless @meta
  title = @_data.chomp
  @meta.title = title
  setvar :title, title
  # FIXME refactor -- just output variables for a template
  _optional_blank_line
end

#viewsObject



401
402
403
404
405
406
# File 'lib/liveblog.rb', line 401

def views
  raise "'post' was not called" unless @meta
  _debug "data = #{_args}"
  @meta.views = _args.dup
  _optional_blank_line
end

#vnavbarObject



702
703
704
# File 'lib/liveblog.rb', line 702

def vnavbar
  str = _make_navbar(:vert)
end

#write_postObject



428
429
430
431
432
433
434
435
436
# File 'lib/liveblog.rb', line 428

def write_post
  raise "'post' was not called" unless @meta
  @meta.views = @meta.views.join(" ") if @meta.views.is_a? Array
  @meta.tags  = @meta.tags.join(" ") if @meta.tags.is_a? Array
  
rescue => err
  puts "err = #{err}"
  puts err.backtrace.join("\n")
end