Created
October 8, 2011 22:07
-
-
Save jamesu/1272952 to your computer and use it in GitHub Desktop.
Convert a TiddlyWiki to Notational Velocity notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Converts tiddlywki tiddlers into files which can be imported into Notational Velocity | |
require 'fileutils' | |
require 'rexml/document' | |
require 'time' | |
require 'date' | |
tiddlywiki_location = ARGV[0] | |
if tiddlywiki_location.nil? | |
puts "Please specify the location of your Tiddliwki" | |
exit | |
end | |
# Load in the tiddlywiki | |
File.open(tiddlywiki_location, 'r') do |file| | |
xml = REXML::Document.new(file.read) | |
puts "Parsed wiki file..." | |
start = xml.elements['html/body/div[@id=\'storeArea\']'] | |
if start.nil? | |
puts "Looks like this isn't a TiddlyWiki!" | |
return | |
end | |
FileUtils.mkdir("notes") rescue nil | |
start.elements.each do |child| | |
unless child.attributes['title'].nil? | |
# Must have a tiddler | |
title = child.attributes['title'] | |
modifier = child.attributes['modifier'] | |
created_on = Date.parse(child.attributes['created']) | |
updated_on = child.attributes['modified'].nil? ? created_on : Date.parse(child.attributes['modified']) | |
tags = child.attributes['tags'].nil? ? [] : child.attributes['tags'].split(' ') | |
content = child.elements[1] | |
unless content.nil? | |
content_data = content.text | |
#puts "#{title} #{updated_on}" | |
#puts "tags: #{tags}" | |
#puts "------" | |
#puts content_data | |
#puts "######" | |
File.open("notes/#{title}.txt", 'w') do |f| | |
f.write "#{title}\n\n" | |
f.write content_data | |
end | |
sanitized_title = title.gsub(' ', '\\ ').gsub('(', '\\(').gsub(')', '\\)') | |
puts ['touch', '-t', created_on.strftime("%Y%m%d%H%M.%S"), "notes/#{sanitized_title}.txt"].join(' ') | |
`touch -t #{created_on.strftime("%Y%m%d%H%M.%S")} notes/#{sanitized_title}.txt` | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment