Class: LiveBlog
- Inherits:
-
Object
- Object
- LiveBlog
- Defined in:
- lib/liveblog.rb
Instance Method Summary collapse
-
#add_entry(raw_entry) ⇒ Object
add a single line entry.
- #date ⇒ Object
-
#find_hashtag(hashtag) ⇒ Object
returns a RecordX object for a given hashtag.
-
#initialize(x = nil, config: nil, datetoday: Date.today, plugins: {}, log: nil) ⇒ LiveBlog
constructor
the config can either be a hash, a config filepath, or nil.
- #initialize_plugins(plugins) ⇒ Object
-
#link_today ⇒ Object
Use with yesterday’s liveblog;.
- #load_file(dxfile) ⇒ Object
- #new_day(date: Date.today) ⇒ Object
- #new_file(x = nil) ⇒ Object (also: #import)
- #plugins ⇒ Object
- #raw_view(tag) ⇒ Object
- #save ⇒ Object
- #tags ⇒ Object
- #update(val) ⇒ Object
-
#update_entry(raw_entry) ⇒ Object
update a page (contains multiple liveblog entries for a section) entry.
-
#valid_entry?(entry) ⇒ Boolean
determines if the entry to be added uses a valid hashtag.
Constructor Details
#initialize(x = nil, config: nil, datetoday: Date.today, plugins: {}, log: nil) ⇒ 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 |
# File 'lib/liveblog.rb', line 17 def initialize(x=nil, config: nil, datetoday: Date.today, plugins: {}, log: nil) @log = log log.info 'LiveBlog: inside initialize' if @log 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
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 102 103 104 105 106 |
# File 'lib/liveblog.rb', line 72 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 = "%s %s#%s" % [entry, static_urlpath(), hashtag] [true, ] end |
#date ⇒ Object
108 109 110 111 112 |
# File 'lib/liveblog.rb', line 108 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
116 117 118 |
# File 'lib/liveblog.rb', line 116 def find_hashtag(hashtag) @dx.find hashtag[/\w+$/] end |
#initialize_plugins(plugins) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/liveblog.rb', line 120 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 |
#link_today ⇒ Object
Use with yesterday’s liveblog;
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/liveblog.rb', line 140 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
154 155 156 157 |
# File 'lib/liveblog.rb', line 154 def load_file(dxfile) @dx = Dynarex.new(dxfile) @d = Date.parse @dx.title end |
#new_day(date: Date.today) ⇒ Object
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 |
# File 'lib/liveblog.rb', line 159 def new_day(date: Date.today) @d = date new_file() link_today() if @log then @log.info 'LiveBlog/new_day: before [plugins]#on_new_day' end @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.info 'LiveBlog: inside new_day' if @log 'new_day() successful' end |
#new_file(x = nil) ⇒ Object Also known as: import
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 239 240 |
# File 'lib/liveblog.rb', line 186 def new_file(x=nil) @log.info 'LiveBlog/new_file: inside new_file()' if @log s = nil s, _ = RXFHelper.read(x) if x @log.info 'LiveBlog/new_file: 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.info 'LiveBLog/new_file: 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.info 'LiveBlog/new_file: about to save new_file' if @log save() end |
#plugins ⇒ Object
244 245 246 |
# File 'lib/liveblog.rb', line 244 def plugins() @plugins.map(&:class) end |
#raw_view(tag) ⇒ Object
248 249 250 251 |
# File 'lib/liveblog.rb', line 248 def raw_view(tag) r = self.find_hashtag tag r.x if r end |
#save ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/liveblog.rb', line 253 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 |
#tags ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/liveblog.rb', line 266 def () 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
283 284 285 |
# File 'lib/liveblog.rb', line 283 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
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 |
# File 'lib/liveblog.rb', line 289 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() if @log then @log.info 'LiveBlog/update_entry: before [plugins]#on_update_entry' end @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
320 321 322 323 324 325 326 327 |
# File 'lib/liveblog.rb', line 320 def valid_entry?(entry) return false unless entry =~ /#\w+/ tag = entry[/#(\w+)$/,1] entry.lstrip[/^#\s+/] or (tag and ().include?(tag)) ? true : false end |