opml

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 12 Imported by: 2

README

opml-go - Parse and export OPML outline files

Continuous integration workflow status

opml-go provides a library to marshal and unmarshal outlines using the Outline Processor Markup Language (OPML) 2.0 format.

Usage

See examples under:

  • example_marshal_test.go to create an OPML document and marshal it to XML;
  • example_unmarshal_test.go to read a file containing an OPML document.

Change Log

See CHANGELOG

License

opml-go is licensed under the MIT License.

Documentation

Index

Examples

Constants

View Source
const (
	Version1   string = "1.0"
	Version1_1 string = "1.1"
	Version2   string = "2.0"

	OutlineTypeInclusion    OutlineType = "include"
	OutlineTypeLink         OutlineType = "link"
	OutlineTypeSubscription OutlineType = "rss"
	OutlineTypeText         OutlineType = "text"

	RSSVersion1 RSSVersion = "RSS"
	RSSVersion2 RSSVersion = "RSS2"
)

Variables

This section is empty.

Functions

func AssertDocumentsEqual added in v1.1.0

func AssertDocumentsEqual(t *testing.T, got, want Document)

AssertDocumentsEqual asserts two OPML documents are equal.

func AssertOutlinesEqual added in v1.1.0

func AssertOutlinesEqual(t *testing.T, gotOutlines, wantOutlines []Outline)

AssertOutlinesEqual asserts two lists of OPML outlines are equal.

func Marshal

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

Marshal returns the XML encoding of a Document.

Example
document := opml.Document{
	Version: "2.0",
	Head: opml.Head{
		Title:      "Feed subscriptions",
		OwnerName:  "Jane Doe",
		OwnerEmail: "jane@thedo.es",
	},
	Body: opml.Body{
		Outlines: []opml.Outline{
			{
				Text:  "Linux",
				Title: "Linux",
				Outlines: []opml.Outline{
					{
						Type:    opml.OutlineTypeSubscription,
						Text:    "Bits from Debian",
						Title:   "Bits from Debian",
						HtmlUrl: "https://bits.debian.org/feeds/atom.xml",
						XmlUrl:  "https://bits.debian.org/feeds/atom.xml",
					},
					{
						Type:    opml.OutlineTypeSubscription,
						Text:    "KXStudio News",
						Title:   "KXStudio News",
						HtmlUrl: "https://kx.studio/News",
						XmlUrl:  "https://kx.studio/News/?action=feed",
					},
				},
			},
			{
				Text:  "Social News",
				Title: "Social News",
				Outlines: []opml.Outline{
					{
						Type:    opml.OutlineTypeSubscription,
						Text:    "Hacker News",
						Title:   "Hacker News",
						HtmlUrl: "https://news.ycombinator.com/",
						XmlUrl:  "https://news.ycombinator.com/rss",
					},
					{
						Type:    opml.OutlineTypeSubscription,
						Text:    "Lobsters",
						Title:   "Lobsters",
						HtmlUrl: "https://lobste.rs",
						XmlUrl:  "https://lobste.rs/rss",
					},
					{
						Type:    opml.OutlineTypeSubscription,
						Text:    "Phoronix",
						Title:   "Phoronix",
						HtmlUrl: "https://www.phoronix.com/",
						XmlUrl:  "https://www.phoronix.com/rss.php",
					},
				},
			},
		},
	},
}

m, err := opml.Marshal(&document)
if err != nil {
	fmt.Println("failed to marshal data as XML:", err)
	os.Exit(1)
}

fmt.Print(string(m))
Output:

<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
  <head>
    <title>Feed subscriptions</title>
    <ownerName>Jane Doe</ownerName>
    <ownerEmail>jane@thedo.es</ownerEmail>
  </head>
  <body>
    <outline text="Linux" title="Linux">
      <outline text="Bits from Debian" htmlUrl="https://bits.debian.org/feeds/atom.xml" title="Bits from Debian" type="rss" xmlUrl="https://bits.debian.org/feeds/atom.xml"></outline>
      <outline text="KXStudio News" htmlUrl="https://kx.studio/News" title="KXStudio News" type="rss" xmlUrl="https://kx.studio/News/?action=feed"></outline>
    </outline>
    <outline text="Social News" title="Social News">
      <outline text="Hacker News" htmlUrl="https://news.ycombinator.com/" title="Hacker News" type="rss" xmlUrl="https://news.ycombinator.com/rss"></outline>
      <outline text="Lobsters" htmlUrl="https://lobste.rs" title="Lobsters" type="rss" xmlUrl="https://lobste.rs/rss"></outline>
      <outline text="Phoronix" htmlUrl="https://www.phoronix.com/" title="Phoronix" type="rss" xmlUrl="https://www.phoronix.com/rss.php"></outline>
    </outline>
  </body>
</opml>

Types

type Body

type Body struct {
	Outlines []Outline `xml:"outline" json:"outlines"`
}

A Body contains one or more Outline elements.

type Document

type Document struct {
	XMLName xml.Name `xml:"opml" json:"-"`
	Version string   `xml:"version,attr" json:"version"`
	Head    Head     `xml:"head" json:"head"`
	Body    Body     `xml:"body" json:"body"`
}

A Document represents an OPML Document.

See https://opml.org/spec2.opml for details on the OPML specification.

func Unmarshal

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

Unmarshal unmarshals a []byte representation of an OPML file and returns the corresponding Document.

Example
blob := `<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
  <head>
    <title>Feed subscriptions</title>
    <ownerName>Jane Doe</ownerName>
    <ownerEmail>jane@thedo.es</ownerEmail>
  </head>
  <body>
    <outline text="Linux" title="Linux">
      <outline text="Bits from Debian" htmlUrl="https://bits.debian.org/feeds/atom.xml" title="Bits from Debian" type="rss" xmlUrl="https://bits.debian.org/feeds/atom.xml"></outline>
      <outline text="KXStudio News" htmlUrl="https://kx.studio/News" title="KXStudio News" type="rss" xmlUrl="https://kx.studio/News/?action=feed"></outline>
    </outline>
    <outline text="Social News" title="Social News">
      <outline text="Hacker News" htmlUrl="https://news.ycombinator.com/" title="Hacker News" type="rss" xmlUrl="https://news.ycombinator.com/rss"></outline>
      <outline text="Lobsters" htmlUrl="https://lobste.rs" title="Lobsters" type="rss" xmlUrl="https://lobste.rs/rss"></outline>
      <outline text="Phoronix" htmlUrl="https://www.phoronix.com/" title="Phoronix" type="rss" xmlUrl="https://www.phoronix.com/rss.php"></outline>
    </outline>
  </body>
</opml>
`

document, err := opml.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:

{
  "version": "2.0",
  "head": {
    "title": "Feed subscriptions",
    "owner_name": "Jane Doe",
    "owner_email": "jane@thedo.es"
  },
  "body": {
    "outlines": [
      {
        "text": "Linux",
        "title": "Linux",
        "outlines": [
          {
            "text": "Bits from Debian",
            "html_url": "https://bits.debian.org/feeds/atom.xml",
            "title": "Bits from Debian",
            "type": "rss",
            "xml_url": "https://bits.debian.org/feeds/atom.xml"
          },
          {
            "text": "KXStudio News",
            "html_url": "https://kx.studio/News",
            "title": "KXStudio News",
            "type": "rss",
            "xml_url": "https://kx.studio/News/?action=feed"
          }
        ]
      },
      {
        "text": "Social News",
        "title": "Social News",
        "outlines": [
          {
            "text": "Hacker News",
            "html_url": "https://news.ycombinator.com/",
            "title": "Hacker News",
            "type": "rss",
            "xml_url": "https://news.ycombinator.com/rss"
          },
          {
            "text": "Lobsters",
            "html_url": "https://lobste.rs",
            "title": "Lobsters",
            "type": "rss",
            "xml_url": "https://lobste.rs/rss"
          },
          {
            "text": "Phoronix",
            "html_url": "https://www.phoronix.com/",
            "title": "Phoronix",
            "type": "rss",
            "xml_url": "https://www.phoronix.com/rss.php"
          }
        ]
      }
    ]
  }
}

func UnmarshalFile

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

UnmarshalFile unmarshals an OPML file and returns the corresponding Document.

func UnmarshalString

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

Unmarshal unmarshals a string representation of an OPML file and returns the corresponding Document.

type Head struct {
	// The title of the document.
	Title string

	// The date the document was created.
	DateCreated time.Time

	// The date indicating when the document was last modified.
	DateModified time.Time

	// The owner of the document.
	OwnerName string

	// The email address of the owner of the document.
	OwnerEmail string

	// A list of line numbers that are expanded.
	// The line numbers in the list indicate which headlines to expand.
	ExpansionState []int

	// A number, saying which  line of the outline is displayed on the top line of the window.
	VertScrollState int

	// The pixel location of the top edge of the window.
	WindowTop int

	// The pixel location of the left edge of the window.
	WindowLeft int

	// The pixel location of the bottom edge of the window.
	WindowBottom int

	// The pixel location of the right edge of the window.
	WindowRight int
}

A Head contains the metadata for the OPML Document.

func (*Head) MarshalJSON

func (h *Head) MarshalJSON() ([]byte, error)

func (*Head) MarshalXML

func (h *Head) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Head) UnmarshalXML

func (h *Head) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type Outline

type Outline struct {
	// The Text that is displayed when an outliner opens the OPML document.
	Text string

	// Special: The Type indicates how the attributes of the Outline are interpreted.
	Type OutlineType

	// Special: Indicates whether a breakpoint is set on this outline.
	IsBreakpoint bool

	// Special: Indicates whether the outline is commented.
	//
	// If an outline is commented, all subordinate outlines are considered to also be commented.
	IsComment bool

	// Special: A list of category strings.
	Categories []string

	// Special: The date when the outline node was created.
	Created time.Time

	// Inclusion: The HTTP address of the included link.
	Url string

	// Subscription: Version of RSS/Atom that is being supplied by the feed.
	Version RSSVersion

	// Subscription: Title is the top-level title from the feed.
	Title string

	// Subscription: Description is the top-level description element from the feed.
	Description string

	// Subscription: Language is the top-level language element for the feed.
	Language string

	// Subscription: HtmlUrl is the top-level link element from the feed.
	HtmlUrl string

	// Subscription: XmlUrl is the address of the feed.
	XmlUrl string

	// Directory: Subordinated outlines, arbitrarily structured.
	Outlines []Outline
}

An Outline represents a text element, a subscription list item or a directory.

func (*Outline) IsDirectory added in v1.2.0

func (o *Outline) IsDirectory() bool

IsDirectory returns whether this Outline is a directory and contains subordinated Outlines.

func (*Outline) MarshalJSON

func (o *Outline) MarshalJSON() ([]byte, error)

func (*Outline) MarshalXML

func (o *Outline) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Outline) OutlineType added in v1.2.0

func (o *Outline) OutlineType() OutlineType

OutlineType returns the type of this Outline.

If the Type is not set explicitly, it is inferred from the Outline's field values.

func (*Outline) UnmarshalXML

func (o *Outline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type OutlineType

type OutlineType string

type RSSVersion

type RSSVersion string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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