Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2014 20:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sheepeeh/10417852 to your computer and use it in GitHub Desktop.
For a given TXT file with Omeka item IDs, generate HTML for a text-only version of a Neatline Timeline.
# Using the same query used for your Neatline Timeline, generate a list of Omeka IDs with
# When script is finished, you will have an HTML file with the same name as your TXT file. It will be ugly, you should probably pretty-print it.
require 'nokogiri'
require 'open-uri'
Item =, :title, :date, :link, :year, :flag)
def get_metadata(from_file)
items = []
years = []
# Read lines from target file
File.readlines(from_file).each do |line|
flag = false
id = line.gsub("\n","")
# NOTE: Subsitute appropriate address for YOUR_OMEKA_URL
doc = Nokogiri::HTML(open("YOUR_OMEKA_URL/items/show/#{id}"))
title = doc.css('#show-item-title').text
date = doc.css('#dublin-core-date .element-text').text
date = date.gsub("\n","").gsub(" ","")
year, month, day = date.split('-')
flag = true if day.nil?
# Convert date strings to date objects for sorting
day = "01" if day.nil?
date = [year, month, day].join('-')
date = DateTime.parse(date)
date = date.to_date
datei = date.strftime("%s").to_i
link = "/items/show/#{id}"
year = date.strftime('%Y').to_s
time_item =, title, date, link, year, flag)
items << time_item
years << year
rescue => e
puts "#{e}: Skipping item #{line}"
years = years.compact
years = years.uniq
items.sort_by! { |e| e[:date] }
puts "Items created. Now generating HTML..."
html = "<div id='text-only'>"
years.each do |y|
html += "<h3>#{y}</h3>"
html += "<ul>"
itemy = {|e| e[:year] =~ /#{y}/ }
itemy.each do |e|
# Revert flagged dates to year + month only.
if e[:flag] == true
e[:date] = e[:date].to_s
year, month, day = e[:date].split ('-')
e[:date] = [year, month].join('-')
html += "<li>#{e[:date]}: <a href='#{e[:link]}'>#{e[:title]}</a></li>"
html += "</ul>"
rescue => e
puts "#{e}: Moving to next item."
html += "</div>"
# Print HTML to file
fseed = File.basename(from_file,'txt')
hf ="#{fseed}.html",'a')
hf.puts html
puts "Done."
# Change to desired file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment