Top Level Namespace

Defined Under Namespace

Modules: 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



822
823
824
# File 'lib/liveblog.rb', line 822

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

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



799
800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/liveblog.rb', line 799

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



757
758
759
# File 'lib/liveblog.rb', line 757

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

#_get_data(file) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/lowlevel.rb', line 29

def _get_data(file)
  lines = File.readlines(file)
  lines.reject! {|line| line[0] == "-" }  # allow rejection of lines
  lines = lines.map do |line|
    line.sub(/ *# .*$/, "")               # allow trailing comments
  end
  lines
end

#_get_data?(file) ⇒ Boolean

File need not exist

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/lowlevel.rb', line 21

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

#_got_python?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/liveblog.rb', line 48

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



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

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



745
746
747
748
749
750
751
752
753
754
755
# File 'lib/liveblog.rb', line 745

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



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

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



818
819
820
# File 'lib/liveblog.rb', line 818

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

#_make_class_name(app) ⇒ Object



523
524
525
526
527
528
529
530
531
# File 'lib/liveblog.rb', line 523

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



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
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
# File 'lib/liveblog.rb', line 690

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



761
762
763
764
765
766
# File 'lib/liveblog.rb', line 761

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

#_passthru_noline(line) ⇒ Object



768
769
770
771
772
773
# File 'lib/liveblog.rb', line 768

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



784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/liveblog.rb', line 784

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) }

  post = posts.select {|x| File.basename(x).to_i == postid }
  raise "Error: More than one post #{postid}" if post.size > 1
  postdir = post.first
  vp = RuneBlog::ViewPost.new(@blog.view, postdir)
  vp
end

#_read_navbar_dataObject



126
127
128
129
130
131
# File 'lib/liveblog.rb', line 126

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, title, url) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/liveblog.rb', line 55

def _reddit_post_url(vdir, title, url)
  _got_python?
  tmpfile = "/tmp/reddit-post-url.txt"
  File.open(tmpfile, "w") do |tmp|
    tmp.puts "[Post] " + 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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
# File 'lib/liveblog.rb', line 197

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

#_var(name) ⇒ Object

FIXME scope issue!



814
815
816
# File 'lib/liveblog.rb', line 814

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

#_write_metadataObject



775
776
777
778
779
780
781
782
# File 'lib/liveblog.rb', line 775

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


117
118
119
# File 'lib/liveblog.rb', line 117

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


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
# File 'lib/liveblog.rb', line 133

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



259
260
# File 'lib/liveblog.rb', line 259

def categories   # does nothing right now
end

#codeObject



121
122
123
124
# File 'lib/liveblog.rb', line 121

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

#copy(src, dst) ⇒ Object



38
39
40
41
42
# File 'lib/lowlevel.rb', line 38

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



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

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



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

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



32
33
34
35
36
37
38
39
40
# File 'lib/liveblog.rb', line 32

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



2
3
4
# File 'lib/lowlevel.rb', line 2

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

#error(err) ⇒ Object



67
68
69
70
71
72
# File 'lib/lowlevel.rb', line 67

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

#faqObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/liveblog.rb', line 103

def faq
  @faq_count ||= 0
  _out "<br>" if @faq_count == 0
  @faq_count += 1
  ques = _data.chomp
  ans  = _body_text
  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



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

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!



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

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

#h2Object



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

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

#h3Object



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

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

#h4Object



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

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

#h5Object



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

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

#h6Object



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

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

#headObject

Does NOT output <head> tags



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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
# File 'lib/liveblog.rb', line 439

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



682
683
684
# File 'lib/liveblog.rb', line 682

def hnavbar
  str = _make_navbar  # horiz is default
end

#hrObject



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

def hr; _passthru "<hr>"; end

#imageObject

primitive so far



362
363
364
365
366
367
368
# File 'lib/liveblog.rb', line 362

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
# 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
rescue
  raise "Only works inside a blog repo"
end

#insetObject

inset



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/liveblog.rb', line 310

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



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

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

#listObject



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

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

#list!Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/liveblog.rb', line 292

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) ⇒ 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)
  log!(enter: __method__, args: [sym, str], level: 3)
  return if Object.constants.include?(sym)
  Object.const_set(sym, StandardError.dup)
  define_method(sym) do |*args|
    msg = str
    args.each.with_index {|arg, i| msg.sub!("$#{i+1}", arg) }
    Object.class_eval(sym.to_s).new(msg)
  end
end

#metaObject

newer stuff…



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/liveblog.rb', line 494

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


686
687
688
# File 'lib/liveblog.rb', line 686

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



278
279
280
281
282
283
# File 'lib/liveblog.rb', line 278

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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/liveblog.rb', line 384

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



42
43
44
45
46
# File 'lib/liveblog.rb', line 42

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

#post_trailerObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/liveblog.rb', line 68

def post_trailer
  perma = _var("publish.proto") + "://" + _var("publish.server") +
          "/" + _var("publish.path") + "/" + _var("post.aslug") + 
          ".html"
  tags = _var("post.tags")
  taglist = tags.empty? ? "" : "Tags: #{tags}"

  reddit_enabled = @blog.features["reddit"] rescue nil
  reddit_txt = ""
  if reddit_enabled
    vdir  = @blog.root/:views/@blog.view
    nslug = "#{_var("post.num")}-#{_var("post.aslug")}"
    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, title, perma)
      dump(rid, rid_file)
    end
    reddit_txt = <<~HTML
      <hr>
      <script src='https://redditjs.com/post.js' 
              data-url="#{rid}" data-width=800 ></script>
    HTML
  # damned syntax highlighting </>
  end
  _out <<~HTML
  <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>
  #{reddit_txt}
  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



350
351
352
353
354
355
356
357
358
359
360
# File 'lib/liveblog.rb', line 350

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



252
253
254
255
256
257
# File 'lib/liveblog.rb', line 252

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

#recent_postsObject

side-effect



513
514
515
516
517
518
519
520
521
# File 'lib/liveblog.rb', line 513

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



606
607
608
609
610
611
612
# File 'lib/liveblog.rb', line 606

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


558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/liveblog.rb', line 558

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



262
263
264
265
# File 'lib/liveblog.rb', line 262

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

#stylesheetObject



598
599
600
601
602
603
604
# File 'lib/liveblog.rb', line 598

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lowlevel.rb', line 6

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



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/liveblog.rb', line 655

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



370
371
372
373
374
375
# File 'lib/liveblog.rb', line 370

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

#teaserObject



414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/liveblog.rb', line 414

def teaser
  raise "'post' was not called" unless @meta
  text = _body_text
  @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

#titleObject



341
342
343
344
345
346
347
348
# File 'lib/liveblog.rb', line 341

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



377
378
379
380
381
382
# File 'lib/liveblog.rb', line 377

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

#vnavbarObject



678
679
680
# File 'lib/liveblog.rb', line 678

def vnavbar
  str = _make_navbar(:vert)
end

#write_postObject



404
405
406
407
408
409
410
411
412
# File 'lib/liveblog.rb', line 404

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