BookmarkParser (bookmark_parser v0.1.0)

View Source

BookmarkParser is a library for parsing Netscape bookmark files.

It can parse bookmark files exported from browsers like Chrome, Firefox, Safari, etc. The parser transforms the bookmarks into a tree structure of folders and entries.

Examples

iex> content = """
...> <!DOCTYPE NETSCAPE-Bookmark-file-1>
...> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
...> <TITLE>Bookmarks</TITLE>
...> <H1>Bookmarks</H1>
...> <DL><p>
...>   <DT><A HREF="https://example.com/">Example</A>
...> </DL><p>
...> """
iex> {:ok, bookmarks} = BookmarkParser.parse_string(content)
iex> bookmarks.title
"Bookmarks"
iex> flat_list = BookmarkParser.flatten(bookmarks)
iex> length(flat_list) > 0
true
iex> Enum.all?(flat_list, fn entry -> Map.has_key?(entry, :href) end)
true

Summary

Functions

Counts the total number of bookmark entries in the tree.

Filters bookmarks by a given predicate function.

Flattens a bookmark tree structure into a list of BookmarkEntry structs.

Parses a bookmark file and returns a tree structure of folders and entries.

Parses bookmark content from a string.

Functions

count_entries(bookmark_tree)

Counts the total number of bookmark entries in the tree.

Parameters

  • bookmark_tree: A BookmarkFolder or BookmarkEntry struct

Returns

  • Integer count of bookmark entries

filter_bookmarks(bookmark_tree, predicate)

Filters bookmarks by a given predicate function.

Parameters

  • bookmark_tree: A BookmarkFolder or BookmarkEntry struct
  • predicate: Function that takes a BookmarkEntry and returns boolean

Returns

  • List of BookmarkEntry structs that match the predicate

flatten(bookmark_tree)

Flattens a bookmark tree structure into a list of BookmarkEntry structs.

Parameters

  • bookmark_tree: A BookmarkFolder or BookmarkEntry struct

Returns

  • List of BookmarkEntry structs

parse_file(path)

@spec parse_file(Path.t()) ::
  {:ok, BookmarkParser.BookmarkFolder.t()} | {:error, term()}

Parses a bookmark file and returns a tree structure of folders and entries.

Parameters

  • path: The file path to the bookmark file

Returns

  • {:ok, bookmark_folder} on success with the parsed BookmarkFolder struct
  • {:error, reason} on failure with reason for the error

parse_string(content)

@spec parse_string(String.t()) ::
  {:ok, BookmarkParser.BookmarkFolder.t()} | {:error, term()}

Parses bookmark content from a string.

Parameters

  • content: String containing the bookmark HTML content

Returns

  • {:ok, bookmark_folder} on success with the parsed BookmarkFolder struct
  • {:error, reason} on failure with reason for the error