Class: LiveBlog

Inherits:
Object
  • Object
show all
Defined in:
lib/liveblog.rb

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, config: nil, datetoday: Date.today, plugins: {}, logpath: '') ⇒ LiveBlog

the config can either be a hash, a config filepath, or nil



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/liveblog.rb', line 17

def initialize(x=nil, config: nil, datetoday: Date.today, plugins: {}, logpath: '')
  
  @log = nil
  
  if logpath.length > 0
    @log = Logger.new logpath, 'daily'
    @log.debug 'inside initialize'
  end
  
  config = if x or config then
  
    x || config
  else
    
    if File.exists? 'liveblog.conf' then 
      'liveblog.conf'
    else
      sc = SimpleConfig.new(new_config())
      File.write 'liveblog.conf', sc.write
      sc
    end
  end
  
  h = SimpleConfig.new(config).to_h

  @dir, @urlbase, @edit_url, @css_url, @xsl_path, @xsl_today_path, \
          @xsl_url, @bannertext, @title, @rss_title, @rss_lang, \
          @hyperlink_today, plugins, @photo_upload_url = \
   (%i(dir urlbase edit_url css_url xsl_path xsl_today_path xsl_url) \
            + %i( bannertext title rss_title rss_lang hyperlink_today ) \
            + %i(plugins photo_upload_url)).map{|x| h[x]}

  @title ||= 'LiveBlog'
  @rss_lang ||= 'en-gb'
  
  Dir.chdir @dir    

  @d = datetoday
  dxfile = File.join(@dir, path(), 'index.xml')
  
  @plugins = initialize_plugins(plugins || [])

  if File.exists? dxfile then 
  
    load_file(dxfile)
          
  else
    
    new_day()        
    
  end
  

end

Instance Method Details

#add_entry(raw_entry) ⇒ Object

add a single line entry



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
102
103
104
105
106
107
108
109
# File 'lib/liveblog.rb', line 75

def add_entry(raw_entry)
  
  new_day() if date() != Date.today

  entry, hashtag = raw_entry.split(/\s*#(?=\w+$)/)
  hashtag.downcase!
  
  success, msg = case raw_entry
  
  when /^#\s*\w.*#\w+$/ then
    
    add_section raw_entry, hashtag
    
  when /#\w+$/ then 
    
    entry.gsub!(/(?:^|\s)!t\s/, '\1' + time())
    add_section_entry entry, hashtag
    
  else 
    [false, 'no valid entry found']
  end
  
  return [false, msg] unless success
  
  save()
  
  # we reserve 30 characters for the link
  len = (140 - 30 - hashtag.length)
  raw_entry.gsub!(/(?:^|\s)!t\z/,'')
  entry = raw_entry.length > len ? "%s... %s" % [raw_entry.slice(0, len),\
                                                         hashtag] : raw_entry
  message = "%s %s#%s" % [entry, static_urlpath(), hashtag]
  
  [true, message]
end

#dateObject



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

def date()
  #@logger = Logger.new '/tmp/liveblog.log', 'daily'
  #@logger.debug 'inside date'
  @d
end

#find_hashtag(hashtag) ⇒ Object

returns a RecordX object for a given hashtag



119
120
121
# File 'lib/liveblog.rb', line 119

def find_hashtag(hashtag)    
  @dx.find hashtag[/\w+$/]
end

#initialize_plugins(plugins) ⇒ Object



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

def initialize_plugins(plugins)
  
  plugins.inject([]) do |r, plugin|
    
    name, settings = plugin
    return r if settings[:active] == false and !settings[:active]
    
    klass_name = 'LiveBlogPlugin' + name.to_s

    r << Kernel.const_get(klass_name).new(settings: settings, \
         variables: {filepath: @dir, todays_filepath: path(@d), \
                                                 urlbase: @urlbase })

  end
      
end

Use with yesterday’s liveblog;



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/liveblog.rb', line 143

def link_today()
  
  raw_formatted_filepath = File.join(@dir, path(@d-1), 'formatted.xml')

  return unless File.exists? raw_formatted_filepath

  doc = Rexle.new File.read(raw_formatted_filepath)    
  doc.root.element('summary/next_day').text = static_urlpath()
  File.write raw_formatted_filepath, doc.xml(pretty: true)
  
  render_html doc, @d-1
  
end

#load_file(dxfile) ⇒ Object



157
158
159
160
# File 'lib/liveblog.rb', line 157

def load_file(dxfile)
  @dx = Dynarex.new(dxfile)
  @d = Date.parse @dx.title    
end

#new_day(date: Date.today) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/liveblog.rb', line 162

def new_day(date: Date.today)
  
  @d = date
  
  new_file()
  link_today()

  @plugins.each do |x|
    
    if x.respond_to? :on_new_day then
      
      yesterdays_index_file = File.join(@dir, path(@d-1), 'index.xml')

       x.on_new_day(yesterdays_index_file, urlpath(@d-1)) 
      
    end
    
  end      
  @log.debug 'inside new_day' if @log
  
  'new_day() successful'
end

#new_file(x = nil) ⇒ Object Also known as: import



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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/liveblog.rb', line 185

def new_file(x=nil)
  
  @log.debug 'inside new_file' if @log
  s = nil
  s, _ = RXFHelper.read(x) if x
  @log.debug 's: ' + s.inspect  if @log
s ||= <<EOF    
<?dynarex schema="sections[title]/section(x)"?>
title: #{@title} #{ordinalize(@d.day) + @d.strftime(" %B %Y")}
--#

EOF

  t = Time.now
  # keyword substitions
  # a !t becomes the Time.now.strftime("%-I:%M%P") #=> 4:27pm
  s.gsub!(/\B\*started\s+<time>(\d+:\d+[ap]m)<\/time>\*\B\s*!tc\z/) do |x|

    raw_start_time = $1

    start_time = Time.parse raw_start_time
    seconds = t - start_time
    list = Subunit.new(units={minutes:60, hours:60}, seconds: seconds )\
                                                                   .to_h.to_a
    n = list.find {|_,v| v > 0 }
    duration = list[list.index(n)..-2].map {|x|"%d %s" % x.reverse}\
                                                                  .join(', ')
    "*completed %s; duration: %s*" % [time(t), duration]
  end
  
  s.gsub!(/(?:^|\s)ts\z/, "*started #{time(t)}*")
  s.gsub!(/(?:^|\s)!tc\z/, "*completed #{time(t)}*")
  s.gsub!(/(?:^|\s)!t\s/,  '\1' + time(t))

  @log.debug 'before mkdir_p: ' + path().inspect  if @log
  FileUtils.mkdir_p File.join(@dir, path())

  @dx = Dynarex.new

  @dx.import(s) {|x| sanitise x } 
  
  @dx.xpath('records/section').each do |rec|
    
    rec.attributes[:uid] = rec.attributes[:id]
    rec.attributes[:id] = rec.text('x').lines.first[/#(\w+)$/,1]

  end
  
  @dx.instance_variable_set :@dirty_flag, true
  @log.debug 'about to save new_file'  if @log

  save()
  
end

#pluginsObject



242
243
244
# File 'lib/liveblog.rb', line 242

def plugins()
  @plugins.map(&:class)
end

#raw_view(tag) ⇒ Object



246
247
248
249
# File 'lib/liveblog.rb', line 246

def raw_view(tag)
  r = self.find_hashtag tag
  r.x if r
end

#saveObject



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/liveblog.rb', line 251

def save()

  @dx.save File.join(@dir, path(), 'index.xml')

  File.write File.join(@dir, path(), 'index.txt'), @dx.to_s
  save_html()
  save_rss()
  save_frontpage()
  FileUtils.cp File.join(@dir, path(),'raw_formatted.xml'), \
                                  File.join(@dir, path(),'raw_formatted.xml.bak')
  'save() successful'
end

#tagsObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/liveblog.rb', line 264

def tags()

  file = @dir + Date.today.to_time.strftime("%Y/%b/%-d/formatted2.xml").downcase
  
  if File.exists? file then

    Rexle.new(File.read file).root
          .xpath('summary/tags/tag/text()').map{|x| x.to_s}
    
  else
    
    []
    
  end    
  
end

#update(val) ⇒ Object



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

def update(val)
  self.method(val[/^<\?dynarex/] ? :import : :update_entry).call val
end

#update_entry(raw_entry) ⇒ Object

update a page (contains multiple liveblog entries for a section) entry



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/liveblog.rb', line 287

def update_entry(raw_entry)
  
  hashtag = raw_entry.lines.first[/#(\w+)$/,1]

  record_found = find_hashtag hashtag
  
  if record_found then
    
    record_found.x = sanitise raw_entry
    save()

    @plugins.each do |x|
      
      if x.respond_to? :on_update_entry then
         x.on_update_section(raw_entry, hashtag) 
      end
      
    end      
    [true]
  else
    [false, 'record for #' + hashtag + ' not found.']
  end
  
end

#valid_entry?(entry) ⇒ Boolean

determines if the entry to be added uses a valid hashtag

Returns:

  • (Boolean)


314
315
316
317
318
319
320
321
# File 'lib/liveblog.rb', line 314

def valid_entry?(entry)
  
  return false unless entry =~ /#\w+/    
  
  tag = entry[/#(\w+)$/,1]    
  entry.lstrip[/^#\s+/] or (tag and tags().include?(tag)) ? true : false

end