class BookmarkMachine::NetscapeFormatter::Writer
This is a simple writer for outputting bookmark appropriate HTML. Since the expected HTML doesn’t have a root, uses a custom doctype, and doesn’t close most tags, it’s easier to just write the output manually rather than try to get Nokogiri to format it poorly for us.
Plus this is just kind of fun in a bizarre un-fun kind of way.
Constants
- HEADER
Attributes
folders[R]
io[R]
Public Class Methods
new(io)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 50 def initialize(io) @io = io @folders = [] io.set_encoding(Encoding::UTF_8) start end
Public Instance Methods
<<(bookmark)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 62 def << bookmark adjust_folders(bookmark.folders) write_bookmark(bookmark) end
done()
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 58 def done close_all_folders end
Private Instance Methods
adjust_folders(new_folders)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 74 def adjust_folders(new_folders) # Find where the new and old folders differ: diverge_index = folders.zip(new_folders).find_index{|a,b| a != b} || 0 # Close old folders after that point: folders[diverge_index .. -1].reverse_each do |name| close_folder(name) end # Open new folders after that point: new_folders[diverge_index .. -1].each do |name| open_folder(name) end end
close_all_folders()
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 89 def close_all_folders folders.reverse_each{|name| close_folder(name)} # Close root folder close_collection end
close_collection()
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 147 def close_collection close_tag(:DL) end
close_folder(name)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 137 def close_folder(name) popped = folders.pop close_collection end
close_tag(tag)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 164 def close_tag(tag) io.write("</#{tag}>") io.write("\n") end
encode(str)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 173 def encode(str) str.to_s .gsub('&', "&") .gsub('"', """) .gsub("'", "'") .gsub("<", "<") .gsub(">", ">") end
open_collection()
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 142 def open_collection open_tag(:DL) open_tag(:p) end
open_folder(name)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 131 def open_folder(name) folders.push name open_collection write_folder(name) end
open_tag(tag, attributes={}) { || ... }
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 151 def open_tag(tag, attributes={}) io.write("<#{tag} ") yield if block_given? io.write(">") end
start()
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 69 def start io.puts HEADER open_collection end
write_attr(name, value)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 157 def write_attr(name, value) io.write(name) io.write('="') io.write(encode(value)) io.write('" ') end
write_bookmark(bookmark)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 95 def write_bookmark(bookmark) open_tag(:DT) open_tag(:A) do write_bookmark_attrs(bookmark) end write_text(bookmark.name) close_tag(:A) if bookmark.description open_tag(:DD) write_text(bookmark.description) end end
write_bookmark_attrs(bookmark)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 111 def write_bookmark_attrs(bookmark) write_attr(:HREF, bookmark.url) if bookmark.url write_attr(:ADD_DATE, bookmark.created_at.to_i) if bookmark.created_at write_attr(:LAST_MODIFIED, bookmark.updated_at.to_i) if bookmark.updated_at write_attr(:TAGS, bookmark.tags.join(",")) if bookmark.tags icon = bookmark.icon if icon attr_name = icon.start_with?("data:") ? :ICON_URI : :ICON write_attr(attr_name, icon) end end
write_folder(name)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 124 def write_folder(name) open_tag(:DT) open_tag(:H3) write_text(name) close_tag(:H3) end
write_text(str)
click to toggle source
# File lib/bookmark_machine/netscape_formatter.rb, line 169 def write_text(str) io.write(encode(str)) end