Top Level Namespace

Defined Under Namespace

Classes: RuneBlog

Instance Method Summary collapse

Instance Method Details

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



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

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



98
99
100
# File 'lib/liveblog.rb', line 98

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

#_find_recent_postsObject

on each one. (The code in _teaser really belongs in a small template

somewhere.)


420
421
422
423
424
425
426
427
428
429
430
# File 'lib/liveblog.rb', line 420

def _find_recent_posts
  @vdir = _var(:FileDir).match(%r[(^.*/views/.*?)/])[1]
  posts = nil
  dir_posts = @vdir + "/posts"
  entries = Dir.entries(dir_posts)
  posts = entries.grep(/^\d\d\d\d/).map {|x| dir_posts + "/" + x }
  posts.select! {|x| File.directory?(x) }
  # directories that start with four digits
  posts = posts.sort {|a, b| b.to_i <=> a.to_i }  # sort descending
  posts[0..19]  # return 20 at most
end

#_interpolate(str, context) ⇒ Object

FIXME move this later



473
474
475
476
# File 'lib/liveblog.rb', line 473

def _interpolate(str, context)   # FIXME move this later
  wrapped = "%[" + str.dup + "]"  # could fail...
  eval(wrapped, context)
end

#_passthru(line) ⇒ Object



102
103
104
105
106
107
# File 'lib/liveblog.rb', line 102

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

#_passthru_noline(line) ⇒ Object



109
110
111
112
113
114
# File 'lib/liveblog.rb', line 109

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



458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/liveblog.rb', line 458

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

#_teaser(slug) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/liveblog.rb', line 478

def _teaser(slug)
  id = slug.to_i
  text = nil
  post_entry_name = @theme + "blog/post_entry.lt3"
  @_post_entry ||= File.read(post_entry_name)
  vp = _post_lookup(id)
  nslug, aslug, title, date, teaser_text = 
    vp.nslug, vp.aslug, vp.title, vp.date, vp.teaser_text
  path = vp.path
  url = "#{path}/#{aslug}.html"    # Should be relative to .blogs!! FIXME
    date = Date.parse(date)
    date = date.strftime("%B %e<br>%Y")
    text = _interpolate(@_post_entry, binding)
  text
end

#_var(name) ⇒ Object

FIXME scope issue!



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

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

#_view_from_cwdObject



31
32
33
34
35
# File 'lib/liveblog.rb', line 31

def _view_from_cwd
  md = Dir.pwd.match(%r[.*/views/(.*?)/])
  return md[1] if md
  nil
end

#all_teasersObject



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/liveblog.rb', line 432

def all_teasers
  open = <<-HTML
      <section class="posts">
  HTML
  close = <<-HTML
      </section>
  HTML

  text = <<-HTML
    <html>
    <head><link rel="stylesheet" href="assets/blog.css"></head>
    <body>
  HTML
  posts = _find_recent_posts
  wanted = [5, posts.size].min  # estimate how many we want?
  enum = posts.each
  wanted.times do
    postid = File.basename(enum.next)
    postid = postid.to_i
    text << _teaser(postid)    # side effect! calls _out
  end
  text << "</body></html>"
  File.write("recent.html", text)
  _out %[<iframe style="width: 100vw;height: 100vh;position: relative;" src='recent.html' width=100% frameborder="0" allowfullscreen></iframe>]
end

#assure(hash) ⇒ Object

really the same as verify for now…



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

def assure(hash)  # really the same as verify for now...
  hash.each_pair do |expr, msg|
    puts "<< #{msg}" unless expr
  end
end

#card1Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/liveblog.rb', line 525

def card1
  title, lines = _data, _body
  lines.map!(&:chomp)

  card_text = lines[0]
  url, target, classname, cdata = lines[1].split(",", 4)

  middle = <<-HTML
    <p class="card-text">#{card_text}</p>
    <a href="#{url}" target="#{target}" class="#{classname}">#{cdata}</a>
  HTML

  _card_generic(card_title: title, middle: middle, extra: "bg-dark text-white")
end

#card2Object



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/liveblog.rb', line 540

def card2
  card_title = _data

# FIXME is this wrong??

  open = <<-HTML
    <div class="card mb-3">
      <div class="card-body">
        <h5 class="card-title">#{card_title}</h5>
      </div>
      <ul class="list-group list-group-flush">
  HTML
  _out open
  _body do |line|
    url, target, cdata = line.chomp.split(",", 3)
    _out %[<li class="list-group-item"><a href="#{url}" target="#{target}">#{cdata}</a> </li>]
  end
  close = %[       </ul>\n    </div>]
  _out close
rescue
  puts @live.body
end

#card_iframeObject



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/liveblog.rb', line 509

def card_iframe
  title, lines = _data, _body
  lines.map!(&:chomp)
  url = lines[0].chomp
  stuff = lines[1..-1].join(" ")  # FIXME later
  middle = <<-HTML
    <iframe src="#{url}" #{stuff} 
            style="border: 0" height="350" 
            frameborder="0" scrolling="no">
    </iframe>
  HTML

  _card_generic(card_title: title, middle: middle, extra: "bg-dark text-white")

end

#categoriesObject

does nothing right now



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

def categories   # does nothing right now
end

#check_meta(meta, where = "") ⇒ Object



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

def check_meta(meta, where = "")
  str =  "--- #{where}\n"
  str << "\ncheck_meta: \n" + caller.join("\n") + "\n  meta = #{meta.inspect}\n"
  str << "  title missing!\n" unless meta.title
  str << "  title missing! (empty)" if meta.title && meta.title.empty?
  str << "  num missing!\n" unless meta.num
  if str =~ /missing!/
    debug str
    raise str 
  end
end

#dump(obj, name) ⇒ Object

FIXME scope



163
164
165
# File 'lib/helpers-blog.rb', line 163

def dump(obj, name)      # FIXME scope
  File.write(name, obj)
end

#emitObject

send to STDOUT?



63
64
65
66
67
68
69
# File 'lib/liveblog.rb', line 63

def emit   # send to STDOUT?
  @emit = true
  case _args.first
    when "off";  @emit = false
    when "on";   @emit = true
  end
end

#finalizeObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/liveblog.rb', line 219

def finalize
  unless @meta
    puts @live.body
    return
  end
  if @blog.nil?
    return @meta
  end

  @slug = @blog.make_slug(@meta)
  slug_dir = @slug
  @postdir = @blog.view.dir + "/posts/#{slug_dir}"
  STDERR.puts "--- finalize: pwd = #{Dir.pwd} postdir = #@postdir"
  write_post
  @meta
end

#h1Object

Move elsewhere later!



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

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

#h2Object



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

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

#h3Object



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

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

#h4Object



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

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

#h5Object



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

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

#h6Object



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

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

#headObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/liveblog.rb', line 283

def head
  _out "<head>"
  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(:title)} | #{_var(:desc)}\n  </title>],
               "generator"      => %[<meta name="generator" content="Runeblog v #@version">],
               "og:title"       => %[<meta property="og:title" content="#{_var(:title)}">],
               "og:locale"      => %[<meta property="og:locale" content="en_US">],
               "description"    => %[<meta name="description" content="#{_var(:desc)}">],
               "og:description" => %[<meta property="og:description" content="#{_var(:desc)}">],
               "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(:title)}">],
               "style"          => %[<link rel="stylesheet" href="assets/blog.css">],
               "feed"           => %[<link type="application/atom+xml" rel="alternate" href="#{_var(:host)}/feed.xml" title="#{_var(:title)}">],
               "favicon"        => %[<link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">\n <link rel="apple-touch-icon" href="../assets/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="('/assets/#{remain}')">]
      # Later: allow other overrides
      when ""; break
      else
        puts "Unknown tag '#{word}'"
    end
  end
  hash = defaults.dup.update(result)  # FIXME collisions?
#  _out "<html lang=en_US>"
  hash.each_value {|x| _out "  " + x }
  _out "</head>"
  _out "<body>"
end

#hrObject



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

def hr; _passthru "<hr>"; end

#imageObject

primitive so far



137
138
139
140
141
142
143
# File 'lib/liveblog.rb', line 137

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



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/liveblog.rb', line 11

def init_liveblog    # FIXME - a lot of this logic sucks
  here = Dir.pwd
  dir = here
  loop { dir = Dir.pwd; break if File.exist?("config"); Dir.chdir("..") }
  Dir.chdir(here)
  @blog = $_blog = RuneBlog.new(dir)
  @root = @blog.root
  @view = @blog.view
  @view_name = @blog.view.name
  @vdir = @blog.view.dir
  @version = RuneBlog::VERSION
  @theme = @vdir + "/themes/standard/"
end

#insetObject

inset



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/liveblog.rb', line 73

def inset
  lines = _body
  box = ""
  lines.each do |line| 
    line = line.dup
    if line[0] == "/"  # Only into inset
      line[0] = ' '
      box << line.dup + " "
      line.replace(" ")
    end
    if line[0] == "|"  # Into inset and body
      line[0] = ' '
      box << line.dup + " "
    end
    _passthru(line)
  end
  lr = _args.first
  wide = _args[1] || "25"
  _passthru "<div style='float:#{lr}; width: #{wide}%; padding:8px; padding-right:12px; font-family:verdana'>"
  _passthru '<b><i>'
  _passthru box
  _passthru_noline '</i></b></div>'
  _optional_blank_line
end

#listObject



167
168
169
170
171
172
# File 'lib/liveblog.rb', line 167

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

#list!Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/liveblog.rb', line 174

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

#make_exception(sym, str) ⇒ Object

Refactor, move stuff elsewhere?



22
23
24
25
26
27
28
29
30
# File 'lib/global.rb', line 22

def make_exception(sym, str)
  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…



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/liveblog.rb', line 336

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


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

def navbar
  title = _var(:title)
  open = <<-HTML
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="index.html">#{title}</a>
      <button class="navbar-toggler" 
              type="button" 
              data-toggle="collapse" 
              data-target="#navbarSupportedContent"
              aria-controls="navbarSupportedContent" 
              aria-expanded="false" 
              aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse pull-right" 
           id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
  HTML
  close = <<-HTML
        </ul>
      </div>
    </nav>
  HTML

  first = true
  _out open
  _body do |line|
    href, cdata = line.chomp.strip.split(" ", 2)
    if first
      first = false
      _out %[<li class="nav-item active"> <a class="nav-link" href="#{href}">#{cdata}<span class="sr-only">(current)</span></a> </li>]
    else
      _out %[<li class="nav-item"> <a class="nav-link" href="#{href}">#{cdata}</a> </li>]
    end
  end
  _out close
end

#pinObject



159
160
161
162
163
164
165
# File 'lib/liveblog.rb', line 159

def pin
  raise "'post' was not called" unless @meta
  _debug "data = #{_args}"
  # verify only already-specified views?
  @meta.pinned = _args.dup
  _optional_blank_line
end

#postObject



25
26
27
28
29
# File 'lib/liveblog.rb', line 25

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

#prefix(num) ⇒ Object



32
33
34
# File 'lib/global.rb', line 32

def prefix(num)
  "#{'%04d' % num.to_i}"
end

#pubdateObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/liveblog.rb', line 125

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



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

def quote
  _passthru "<blockquote>"
  _passthru _body
  _passthru "</blockquote>"
  _optional_blank_line
end

#recent_postsObject

side-effect



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/liveblog.rb', line 355

def recent_posts    # side-effect
  _out %[<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">]
  all_teasers
  ## which = _args[0]
  ## case which
  ##   when "recent_posts"
  ##     all_teasers
  ##   when "post"   # No longer needed??
  ##     _out %[<iframe style="width: 100vw;height: 100vh;position: relative;" src='whats-at-stubbs.html' width=100% frameborder="0" allowfullscreen></iframe>]
  ## end
  _out %[</div>]
end

#scriptObject



396
397
398
399
400
401
402
# File 'lib/liveblog.rb', line 396

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


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

def sidebar
  _out %[<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">]
  _body do |line|
    tag = line.chomp.strip
    self.data = "sidebar/#{tag.downcase}.lt3"
    _include 
  end
  _out %[</div>]
end

#sidebar!Object



378
379
380
381
382
383
384
385
386
# File 'lib/liveblog.rb', line 378

def sidebar!
  _out %[<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">]
  _args do |line|
    tag = line.chomp.strip
    self.data = "sidebar/#{tag.downcase}.lt3"
    _include 
  end
  _out %[</div>]
end

#styleObject



47
48
49
50
# File 'lib/liveblog.rb', line 47

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

#stylesheetObject



388
389
390
391
392
393
394
# File 'lib/liveblog.rb', line 388

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

#tag_cloudObject



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/liveblog.rb', line 563

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">#{title}</h5>
  HTML
  _out open
  _body do |line|
    line.chomp!
    url, target, classname, cdata = line.split(",", 4)
    _out %[<a href="#{url}" target="#{target}" class="#{classname}">#{cdata}</a>]
  end
rescue
  puts @live.body
end

#tagsObject



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

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

#teaserObject



212
213
214
215
216
217
# File 'lib/liveblog.rb', line 212

def teaser
  raise "'post' was not called" unless @meta
  @meta.teaser = _body_text
  _out @meta.teaser + "\n"
  # FIXME
end

#titleObject



116
117
118
119
120
121
122
123
# File 'lib/liveblog.rb', line 116

def title
  raise "'post' was not called" unless @meta
  title = @_data.chomp
  @meta.title = title
  setvar :title, title
  _out %[<h1 class="post-title">#{title}</h1><br>]
  _optional_blank_line
end

#verify(hash) ⇒ Object



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

def verify(hash)
  hash.each_pair do |expr, msg|
    puts "<< #{msg}" unless expr
  end
end

#viewsObject



152
153
154
155
156
157
# File 'lib/liveblog.rb', line 152

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

#write_postObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/liveblog.rb', line 190

def write_post
  raise "'post' was not called" unless @meta
  save = Dir.pwd
  @postdir.gsub!(/\/\//, "/")  # FIXME unneeded?
  Dir.mkdir(@postdir) unless Dir.exist?(@postdir) # FIXME remember assets!
  Dir.chdir(@postdir)
  @meta.views = @meta.views.join(" ")
  @meta.tags  = @meta.tags.join(" ") rescue ""
  File.write("teaser.txt", @meta.teaser)
  
  fields = [:num, :title, :date, :pubdate, :views, :tags]
  
  fname2 = "metadata.txt"
  f2 = File.open(fname2, "w") do |f2| 
    fields.each {|fld| f2.puts "#{fld}: #{@meta.send(fld)}" }
  end
  Dir.chdir(save)
rescue => err
  puts "err = #{err}"
  puts err.backtrace.join("\n")
end