Twitter scripts/layouts, Twitter import

This commit is contained in:
Stefan Schlott 2013-08-23 18:49:13 +02:00
parent 65a6a20708
commit bc62533852
12 changed files with 480 additions and 1 deletions

View file

@ -2,4 +2,5 @@
# before nanoc starts compiling.
include Nanoc3::Helpers::Rendering
include Nanoc::Helpers::HTMLEscape

46
lib/twitter.rb Normal file
View file

@ -0,0 +1,46 @@
# encoding: utf-8
module Nanoc::DataSources::Contrib
# Fetch data from downloaded twitter csv file
class Twitter < Nanoc::DataSource
identifier :twitter_file
def items
@items ||= begin
require 'csv'
if @site.config[:twitter_file].nil?
raise Nanoc::Errors::GenericTrivial.new('Cannot read Twitter data: site configuration has no twitter_file')
end
# Get data
if ! File.exists?(@site.config[:twitter_file])
return []
end
# Convert to items
@items = []
CSV.foreach(@site.config[:twitter_file]) do |row|
# Get data
content = row[3]
attributes = {
:kind => 'twitter',
:id => row[0],
:date => Time.parse(row[1]),
:nick => row[2],
:url => "https://twitter.com/#{row[2]}/status/#{row[0]}"
}
identifier = "/_twitter/#{row[0]}/"
mtime = Time.parse(row[1])
# Build item
@items += [ Nanoc::Item.new(content, attributes, identifier, mtime) ]
end
@items
end
end
end
end

53
lib/twitterarchive.rb Normal file
View file

@ -0,0 +1,53 @@
require 'ostruct'
def split_yearmonth(yearmonth)
s = yearmonth.split('-')
month = case s[1]
when '01'
'Januar'
when '02'
'Februar'
when '03'
'März'
when '04'
'April'
when '05'
'Mai'
when '06'
'Juni'
when '07'
'Juli'
when '08'
'August'
when '09'
'September'
when '10'
'Oktober'
when '11'
'November'
when '12'
'Dezember'
end
[ s[1], month, s[0] ]
end
def generate_twitter_archive_pages()
monthlist = twitter_grouped_by_month.keys.sort
monthlist.each_index { |i|
linkprev = if (i>0)
", :linkprev => OpenStruct.new(:title => '#{split_yearmonth(monthlist[i-1])[1]} #{split_yearmonth(monthlist[i-1])[2]}', :link => '/archives/twitter/#{monthlist[i-1]}/')"
else
""
end
linknext = if (i<monthlist.size-1)
", :linknext => OpenStruct.new(:title => '#{split_yearmonth(monthlist[i+1])[1]} #{split_yearmonth(monthlist[i+1])[2]}', :link => '/archives/twitter/#{monthlist[i+1]}/')"
else
""
end
@items << Nanoc::Item.new(
"<%= render 'twitter_archive', :month => '#{monthlist[i]}' #{linkprev} #{linknext} %>",
{ :title => "Twitterarchiv #{split_yearmonth(monthlist[i])[1]} #{split_yearmonth(monthlist[i])[2]}", :kind => "fullpage" },
"/archives/twitter/#{monthlist[i]}/")
}
end

28
lib/twitterhelpers.rb Normal file
View file

@ -0,0 +1,28 @@
def twitter_items()
@cache_twitter_items ||= items.select { |d| d[:kind] == 'twitter' }
@cache_twitter_items
end
def twitter_by_date()
@cache_twitter_by_date ||= twitter_items().sort_by { |d| d[:date] }.reverse()
@cache_twitter_by_date
end
def twitter_grouped_by_month
@cache_twitter_grouped_by_month ||= twitter_by_date().group_by { |d| "#{d[:date].year}-%02d" % d[:date].month }
@cache_twitter_grouped_by_month
end
def tweet_to_html(tweet)
links = tweet.scan(/https?:\/\/[^ ]*[^ .);:!?]/)
result = tweet.dup
links.each_with_index do |link,n|
result.gsub!(link, "{{{link#{n}}}}")
end
result = html_escape(result)
links.each_with_index do |link,n|
result.gsub!("{{{link#{n}}}}", "<a href=\"#{link}\">(Link)</a>")
end
return result
end