Guest User

Untitled

a guest
Jan 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Converts tiddlywki tiddlers into files which can be imported into Notational Velocity
  3. require 'fileutils'
  4. require 'rexml/document'
  5. require 'time'
  6. require 'date'
  7.  
  8. tiddlywiki_location = ARGV[0]
  9. if tiddlywiki_location.nil?
  10. puts "Please specify the location of your Tiddliwki"
  11. exit
  12. end
  13.  
  14. # Load in the tiddlywiki
  15. File.open(tiddlywiki_location, 'r') do |file|
  16. xml = REXML::Document.new(file.read)
  17. puts "Parsed wiki file..."
  18.  
  19. start = xml.elements['html/body/div[@id=\'storeArea\']']
  20. if start.nil?
  21. puts "Looks like this isn't a TiddlyWiki!"
  22. return
  23. end
  24.  
  25. FileUtils.mkdir("notes") rescue nil
  26.  
  27. start.elements.each do |child|
  28. unless child.attributes['title'].nil?
  29. # Must have a tiddler
  30. title = child.attributes['title']
  31. modifier = child.attributes['modifier']
  32. created_on = Date.parse(child.attributes['created'])
  33. updated_on = child.attributes['modified'].nil? ? created_on : Date.parse(child.attributes['modified'])
  34. tags = child.attributes['tags'].nil? ? [] : child.attributes['tags'].split(' ')
  35.  
  36. content = child.elements[1]
  37. unless content.nil?
  38. content_data = content.text
  39. #puts "#{title} #{updated_on}"
  40. #puts "tags: #{tags}"
  41. #puts "------"
  42. #puts content_data
  43. #puts "######"
  44.  
  45.  
  46. File.open("notes/#{title}.txt", 'w') do |f|
  47. f.write "#{title}\n\n"
  48. f.write content_data
  49. end
  50.  
  51. sanitized_title = title.gsub(' ', '\\ ').gsub('(', '\\(').gsub(')', '\\)')
  52. puts ['touch', '-t', created_on.strftime("%Y%m%d%H%M.%S"), "notes/#{sanitized_title}.txt"].join(' ')
  53. `touch -t #{created_on.strftime("%Y%m%d%H%M.%S")} notes/#{sanitized_title}.txt`
  54. end
  55. end
  56. end
  57.  
  58. end
Add Comment
Please, Sign In to add comment