netscape

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 8 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

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal returns the Netscape Bookmark encoding of d.

Example
document := types.Document{
	Title: "Bookmarks",
	Root: types.Folder{
		Name: "Bookmarks",
		Bookmarks: []types.Bookmark{
			{
				Href:  "https://domain.tld",
				Title: "Test Domain",
			},
			{
				Description: "Local\nLocal\nLocal",
				Href:        "https://local.domain.tld",
				Title:       "Local Test Domain",
			},
		},
		Subfolders: []types.Folder{
			{
				Name: "Sub",
				Bookmarks: []types.Bookmark{
					{
						Href:  "https://domain.tld",
						Title: "Test Domain",
						Attributes: map[string]string{
							"ATTR1": "v1",
							"ATTR2": "42",
						},
					},
					{
						Description: "Local\nLocal\nLocal",
						Href:        "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>

func Unmarshal

func Unmarshal(b []byte) (*types.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">Arch Linux</A>
	    <DT><A HREF="https://debian.org">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": [
          {
            "title": "Arch Linux",
            "url": "https://archlinux.org",
            "private": false
          },
          {
            "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) (*types.Document, error)

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

func UnmarshalString

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

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

Types

This section is empty.

Directories

Path Synopsis
Package ast declares the types used to represent syntax trees for Netscape Bookmark files.
Package ast declares the types used to represent syntax trees for Netscape Bookmark files.
cmd
Package decoder implements decoding to convert AST nodes to Netscape Bookmark domain types.
Package decoder implements decoding to convert AST nodes to Netscape Bookmark domain types.
Package encoder implements encoding and printing of domain types for Netscape Bookmark files.
Package encoder implements encoding and printing of domain types for Netscape Bookmark files.
Package parser implements a parser for Netscape Bookmark files.
Package parser implements a parser for Netscape Bookmark files.

Jump to

Keyboard shortcuts

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