lib/bookmarks/document.rb in bookmarks-0.1.1 vs lib/bookmarks/document.rb in bookmarks-0.2.0

- old
+ new

@@ -18,10 +18,11 @@ def initialize format: :netscape @bookmarks_format = format @document = "" @bookmarks = [] @total = 0 + @h3_tags = [] end # Public: Returns the Symbol format of the document. Currently # there is only one format available: `:netscape`. attr_reader :bookmarks_format @@ -73,15 +74,35 @@ # Parse a single line from a bookmarks file. # # line - String. # # Returns nothing. + # TODO This should have its own parser class. def parse_a_bookmark line - if line =~ /^<DT>/ + line = line.strip + if line =~ /^<DT><H3>/ + @h3_tags << h3_tags(line) + elsif line =~ /^<\/DL>/ + @h3_tags.pop + elsif line =~ /<DT><A/ @bookmarks << NetscapeBookmark.from_string(line) + if (not @h3_tags.empty?) && (not @bookmarks.last.nil?) + @bookmarks.last.add_tags @h3_tags + end elsif line =~ /^<DD>/ @bookmarks.last.description = line[4..-1].chomp end + end + + # Get the h3's content of a line. H3 could be use as a tag in + # a netscape bookmark's file. + # + # line - String. + # + # Returns String h3 content or empty string. + def h3_tags line + md = /<H3>(.*?)<\/H3>/.match(line) + md ? md[1] : "" end # First part of a bookmark's file in netscape format. FIRST_PART = <<CODE