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.
- #hashtag_exists?(tag) ⇒ Boolean
-
#initialize(x = nil, config: nil, date: Date.today, plugins: {}) ⇒ 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
- #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, date: Date.today, plugins: {}) ⇒ 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 |
# File 'lib/liveblog.rb', line 17 def initialize(x=nil, config: nil, date: Date.today, plugins: {}) @logger = Logger.new '/tmp/liveblog.log', 'daily' @logger.debug 'inside initialize' 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 = \ (%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)).map{|x| h[x]} @title ||= 'LiveBlog' @rss_lang ||= 'en-gb' Dir.chdir @dir @d = date 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
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 102 103 |
# File 'lib/liveblog.rb', line 71 def add_entry(raw_entry) 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
105 106 107 108 109 |
# File 'lib/liveblog.rb', line 105 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
113 114 115 |
# File 'lib/liveblog.rb', line 113 def find_hashtag(hashtag) @dx.find hashtag[/\w+$/] end |
#hashtag_exists?(tag) ⇒ Boolean
295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/liveblog.rb', line 295 def hashtag_exists?(tag) file = @dir + Date.today.to_time.strftime("%Y/%b/%-d/formatted2.xml").downcase if File.exists? file then doc = Rexle.new(File.read file) = doc.root.xpath('summary/tags/tag/text()') return true if .include? tag end return false end |
#initialize_plugins(plugins) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/liveblog.rb', line 117 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;
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/liveblog.rb', line 137 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
151 152 153 154 |
# File 'lib/liveblog.rb', line 151 def load_file(dxfile) @dx = Dynarex.new(dxfile) @d = Date.parse @dx.title end |
#new_day(date: Date.today) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/liveblog.rb', line 156 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 @logger.debug 'inside new_day' 'new_day() successful' end |
#new_file(x = nil) ⇒ Object Also known as: import
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 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/liveblog.rb', line 179 def new_file(x=nil) @logger.debug 'inside new_file1' s = nil s, _ = RXFHelper.read(x) if x @logger.debug 's: ' + s.inspect 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)) @logger.debug 'before mkdir_p: ' + path().inspect 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 @logger.debug 'about to save new_file' save() end |
#plugins ⇒ Object
236 237 238 |
# File 'lib/liveblog.rb', line 236 def plugins() @plugins.map(&:class) end |
#raw_view(tag) ⇒ Object
240 241 242 243 |
# File 'lib/liveblog.rb', line 240 def raw_view(tag) r = self.find_hashtag tag r.x if r end |
#save ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/liveblog.rb', line 245 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 |
#update(val) ⇒ Object
258 259 260 |
# File 'lib/liveblog.rb', line 258 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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/liveblog.rb', line 264 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
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/liveblog.rb', line 291 def valid_entry?(entry) return false unless entry =~ /#\w+/ def hashtag_exists?(tag) file = @dir + Date.today.to_time.strftime("%Y/%b/%-d/formatted2.xml").downcase if File.exists? file then doc = Rexle.new(File.read file) = doc.root.xpath('summary/tags/tag/text()') return true if .include? tag end return false end tag = entry[/#(\w+)$/,1] entry.lstrip[/^#\s+/] or (tag and hashtag_exists?(tag)) ? true : false end |