netscape

package module
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2023 License: MIT Imports: 13 Imported by: 0

README

netscape-go - Parse and export bookmarks using the Netscape format

Build and test workflow status

netscape-go provides a library to marshal and unmarshal bookmark collections using the Netscape Bookmark File Format, as exported by common Web browsers and bookmarking services.

Change Log

See CHANGELOG

License

netscape-go is licenced under the MIT License.

Documentation

Overview

Package netscape provides utilities to parse and export Web bookmarks using the Netscape Bookmark format.

Index

Examples

Constants

View Source
const (
	NetscapeBookmarkDoctype string = "NETSCAPE-Bookmark-file-1"
)

Variables

View Source
var (
	ErrDoctypeMissing error = errors.New("missing DOCTYPE")
	ErrDoctypeInvalid error = errors.New("invalid DOCTYPE")
)

Functions

func Marshal

func Marshal(d *Document) ([]byte, error)

Marshal returns the Netscape Bookmark encoding of d.

Example
document := netscape.Document{
	Title: "Bookmarks",
	Root: netscape.Folder{
		Name: "Bookmarks",
		Bookmarks: []netscape.Bookmark{
			{
				URL:   "https://domain.tld",
				Title: "Test Domain",
			},
			{
				Description: "Local\nLocal\nLocal",
				URL:         "https://local.domain.tld",
				Title:       "Local Test Domain",
			},
		},
		Subfolders: []netscape.Folder{
			{
				Name: "Sub",
				Bookmarks: []netscape.Bookmark{
					{
						URL:   "https://domain.tld",
						Title: "Test Domain",
						Attributes: map[string]string{
							"ATTR1": "v1",
							"ATTR2": "42",
						},
					},
					{
						Description: "Local\nLocal\nLocal",
						URL:         "https://local.domain.tld",
						Title:       "Local Test Domain",
					},
				},
			},
		},
	},
}

m, err := netscape.Marshal(&document)
if err != nil {
	panic(err)
}

fmt.Print(string(m))
Output:

<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
     It will be read and overwritten.
     DO NOT EDIT! -->
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
    <DT><A HREF="https://domain.tld" PRIVATE="0">Test Domain</A>
    <DT><A HREF="https://local.domain.tld" PRIVATE="0">Local Test Domain</A>
    <DD>Local
Local
Local
    <DT><H3>Sub</H3>
    <DL><p>
        <DT><A HREF="https://domain.tld" PRIVATE="0" ATTR1="v1" ATTR2="42">Test Domain</A>
        <DT><A HREF="https://local.domain.tld" PRIVATE="0">Local Test Domain</A>
        <DD>Local
Local
Local
    </DL><p>
</DL><p>

Types

type Bookmark

type Bookmark struct {
	CreatedAt time.Time
	UpdatedAt time.Time

	Title string
	URL   string

	Description string
	Private     bool
	Tags        []string

	Attributes map[string]string
}

A Bookmark represents a Netscape Bookmark.

func (*Bookmark) MarshalJSON added in v2.1.0

func (b *Bookmark) MarshalJSON() ([]byte, error)

type BookmarkNode

type BookmarkNode struct {
	Href        string
	Title       string
	Description string
	Attributes  map[string]string
}

A BookmarkNode represents a Netscape bookmark.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder walks a Netscape Bookmark AST and returns the corresponding document.

func NewDecoder

func NewDecoder() *Decoder

NewDecoder initializes and returns a new Decoder.

type Document

type Document struct {
	Title string `json:"title"`
	Root  Folder `json:"root"`
}

A Document represents a collection of Netscape Bookmarks.

func Decode

func Decode(f FileNode) (*Document, error)

Decode walks a Netscape Bookmark AST and returns the corresponding document.

func Unmarshal

func Unmarshal(b []byte) (*Document, error)

Unmarshal unmarshals a []byte representation of a Netscape Bookmark file and returns the corresponding Document.

Example
blob := `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
    <DT><H3>Linux Distributions</H3>
	<DL><p>
		<DT><A HREF="https://archlinux.org" ADD_DATE="1654077848">Arch Linux</A>
	    <DT><A HREF="https://debian.org" ADD_DATE="1653057612" LAST_MODIFIED="1653058043">Debian</A>
	</DL><p>
    <DT><H3>Programming Languages</H3>
	<DL><p>
		<DT><A HREF="https://go.dev">Go</A>
		<DT><A HREF="https://www.rust-lang.org/">Rust</A>
	</DL><p>
    <DT><H3>Secret stuff</H3>
	<DL><p>
		<DT><A HREF="https://https://en.wikipedia.org/wiki/Caesar_cipher" PRIVATE="1">Caesar cipher</A>
		<DT><A HREF="https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher" PRIVATE="1">Vigenère cipher</A>
	</DL><p>
</DL><p>
`

document, err := netscape.Unmarshal([]byte(blob))
if err != nil {
	fmt.Println("failed to unmarshal file:", err)
	os.Exit(1)
}

jsonData, err := json.MarshalIndent(document, "", "  ")
if err != nil {
	fmt.Println("failed to marshal data as JSON:", err)
	os.Exit(1)
}

fmt.Println(string(jsonData))
Output:

{
  "title": "Bookmarks",
  "root": {
    "name": "Bookmarks",
    "subfolders": [
      {
        "name": "Linux Distributions",
        "bookmarks": [
          {
            "created_at": "2022-06-01T10:04:08Z",
            "updated_at": "2022-06-01T10:04:08Z",
            "title": "Arch Linux",
            "url": "https://archlinux.org",
            "private": false
          },
          {
            "created_at": "2022-05-20T14:40:12Z",
            "updated_at": "2022-05-20T14:47:23Z",
            "title": "Debian",
            "url": "https://debian.org",
            "private": false
          }
        ]
      },
      {
        "name": "Programming Languages",
        "bookmarks": [
          {
            "title": "Go",
            "url": "https://go.dev",
            "private": false
          },
          {
            "title": "Rust",
            "url": "https://www.rust-lang.org/",
            "private": false
          }
        ]
      },
      {
        "name": "Secret stuff",
        "bookmarks": [
          {
            "title": "Caesar cipher",
            "url": "https://https://en.wikipedia.org/wiki/Caesar_cipher",
            "private": true
          },
          {
            "title": "Vigenère cipher",
            "url": "https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher",
            "private": true
          }
        ]
      }
    ]
  }
}

func UnmarshalFile

func UnmarshalFile(filePath string) (*Document, error)

UnmarshalFile unmarshals a Netscape Bookmark file and returns the corresponding Document.

func UnmarshalString

func UnmarshalString(data string) (*Document, error)

UnmarshalString unmarshals a string representation of a Netscape Bookmark file and returns the corresponding Document.

func (*Document) Flatten

func (d *Document) Flatten() *Document

Flatten returns a flat version of this Document, with all Bookmarks attached to the Root Folder.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder writes Netscape Bookmark data to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(d *Document) error

Encode writes the Netscape Bookmark encoding of d to the stream.

type FileNode

type FileNode struct {
	Title string
	Root  FolderNode
}

A FileNode represents a Netscape Bookmark file.

func Parse

func Parse(readseeker io.ReadSeeker) (*FileNode, error)

Parse reads a Netscape Bookmark document and processes it token by token to build and return the corresponding AST.

type Folder

type Folder struct {
	CreatedAt time.Time
	UpdatedAt time.Time

	Description string
	Name        string

	Attributes map[string]string

	Bookmarks  []Bookmark
	Subfolders []Folder
}

A Folder represents a folder containing Netscape Bookmarks and child Folders.

func (*Folder) MarshalJSON added in v2.1.0

func (f *Folder) MarshalJSON() ([]byte, error)

type FolderNode

type FolderNode struct {
	Parent *FolderNode

	Name        string
	Description string
	Attributes  map[string]string

	Bookmarks  []BookmarkNode
	Subfolders []FolderNode
}

A FolderNode represents a bookmark (sub-)folder that may contain Bookmarks and child Folders.

type ParseError

type ParseError struct {
	Msg string
	Pos int64
	Err error
}

A ParseError is returned when we fail to parse a Netscape Bookmark token or XML element.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns the string representation for this error.

func (*ParseError) Is

func (e *ParseError) Is(target error) bool

Is compares this Error with a target error to satisfy an equality check.

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

Unwrap returns the inner error wrapped by this Error.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL