fb2parse

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 8 Imported by: 0

README

go-fb2parse

Go Reference Go Report Card Coverage Pipeline

Golang package for parsing FB2-files.

Examples:

package main

import (
	"encoding/xml"
	"io"

	"github.com/egnd/go-fb2parse"
)

func main() {
	var reader io.Reader
	var fb2Doc fb2parse.FB2File

	// get fb2 struct by xml decoder
	if err := xml.NewDecoder(reader).Decode(&fb2Doc); err != nil {
		panic(err)
	}

	// get fb2 struct by configured xml decoder
	if err := fb2parse.NewDecoder(reader).Decode(&fb2Doc); err != nil {
		panic(err)
	}

	// get fb2 struct by parsing
	if fb2Doc, err := fb2parse.NewFB2File(fb2parse.NewDecoder(reader)); err != nil {
		panic(err)
	}

	// get fb2 struct by parsing withour "binary" images
	if fb2Doc, err := fb2parse.NewFB2File(fb2parse.NewDecoder(reader),
		func(next fb2parse.TokenHandler) fb2parse.TokenHandler {
			return func(obj interface{},
				node xml.StartElement, r xml.TokenReader,
			) (err error) {
				if _, ok := obj.(*fb2parse.FB2File); ok &&
					node.Name.Local == "binary" {
					return SkipToken(node.Name.Local, r)
				}

				return next(obj, node, r)
			}
		},
	); err != nil {
		panic(err)
	}
}

Benchmarks:

goos: linux
goarch: amd64
pkg: github.com/egnd/go-fb2parse
cpu: AMD Ryzen 7 5800U with Radeon Graphics         
Benchmark_Decoding/xml-16        42  44711604 ns/op  4723695 B/op   6382 allocs/op
Benchmark_Decoding/fb2-16        43  45216919 ns/op  4723693 B/op   6382 allocs/op
Benchmark_Parsing/rules_0-16     42  29950641 ns/op  2284588 B/op   3975 allocs/op
Benchmark_Parsing/rules_1-16     34  29852034 ns/op  2284509 B/op   3983 allocs/op
Benchmark_Parsing/rules_10-16    43  25313036 ns/op  2285624 B/op   4055 allocs/op
Benchmark_Parsing/rules_100-16   34  31983362 ns/op  2297366 B/op   4774 allocs/op
Benchmark_Parsing/rules_500-16   42  27396962 ns/op  2348383 B/op   7974 allocs/op
Benchmark_Parsing/rules_1000-16  39  30218804 ns/op  2412397 B/op  11975 allocs/op

Documentation

Overview

Package fb2parse contains tools for parsing fb2-files

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContent

func GetContent(tokenName string, reader xml.TokenReader) (res string, err error)

GetContent returns xml token inner content.

func NewDecoder

func NewDecoder(reader io.Reader) *xml.Decoder

NewDecoder creates decoder for fb2 xml data.

func SkipToken added in v0.10.0

func SkipToken(tokenName string, reader xml.TokenReader) (err error)

SkipToken skips current token at reader.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal converts fb2 xml data some struct.

Types

type FB2Annotation

type FB2Annotation struct {
	HTML string `xml:",innerxml"`
}

FB2Annotation struct of fb2 annotation. http://www.fictionbook.org/index.php/Элемент_annotation

func NewFB2Annotation

func NewFB2Annotation(tokenName string, reader xml.TokenReader) (res FB2Annotation, err error)

NewFB2Annotation factory for FB2Annotation.

type FB2Author

type FB2Author struct {
	FirstName  []string `xml:"first-name"`
	MiddleName []string `xml:"middle-name"`
	LastName   []string `xml:"last-name"`
	Nickname   []string `xml:"nickname"`
	HomePage   []string `xml:"home-page"`
	Email      []string `xml:"email"`
	ID         []string `xml:"id"`
}

FB2Author struct of fb2 author. http://www.fictionbook.org/index.php/Элемент_author

func NewFB2Author

func NewFB2Author(tokenName string, reader xml.TokenReader) (res FB2Author, err error)

NewFB2Author factory for FB2Author.

type FB2Binary

type FB2Binary struct {
	ContentType string `xml:"content-type,attr"`
	ID          string `xml:"id,attr"`
	Data        string `xml:",innerxml"`
}

FB2Binary struct of fb2 binary data. http://www.fictionbook.org/index.php/Элемент_binary

func NewFB2Binary

func NewFB2Binary(token xml.StartElement, reader xml.TokenReader) (res FB2Binary, err error)

NewFB2Binary factory for FB2Binary.

type FB2Body

type FB2Body struct {
	HTML string `xml:",innerxml"`
}

FB2Body struct of fb2 body. http://www.fictionbook.org/index.php/Элемент_body

func NewFB2Body

func NewFB2Body(tokenName string, reader xml.TokenReader) (res FB2Body, err error)

NewFB2Body factory for FB2Body.

type FB2Cover

type FB2Cover struct {
	Images []FB2Image `xml:"image"`
}

FB2Cover struct of fb2 cover page. http://www.fictionbook.org/index.php/Элемент_coverpage

func NewFB2Cover

func NewFB2Cover(tokenName string, reader xml.TokenReader) (res FB2Cover, err error)

NewFB2Cover factory for FB2Cover.

type FB2CustomInfo

type FB2CustomInfo struct {
	InfoType string `xml:"info-type,attr"`
	Data     string `xml:",innerxml"`
}

FB2CustomInfo struct of fb2 custom info. http://www.fictionbook.org/index.php/Элемент_custom-info

func NewFB2CustomInfo

func NewFB2CustomInfo(token xml.StartElement, reader xml.TokenReader) (res FB2CustomInfo, err error)

NewFB2CustomInfo factory for FB2CustomInfo.

type FB2Description

type FB2Description struct {
	TitleInfo    []FB2TitleInfo  `xml:"title-info"`
	SrcTitleInfo []FB2TitleInfo  `xml:"src-title-info"`
	DocInfo      []FB2DocInfo    `xml:"document-info"`
	PublishInfo  []FB2Publisher  `xml:"publish-info"`
	CustomInfo   []FB2CustomInfo `xml:"custom-info"`
}

FB2Description struct of fb2 description. http://www.fictionbook.org/index.php/Элемент_description

func NewFB2Description

func NewFB2Description(
	tokenName string, reader xml.TokenReader, rules []HandlingRule,
) (res FB2Description, err error)

NewFB2Description factory for FB2Description.

type FB2DocInfo

type FB2DocInfo struct {
	Authors    []FB2Author `xml:"author"`
	SrcURL     []string    `xml:"src-url"`
	ID         []string    `xml:"id"`
	Version    []string    `xml:"version"`
	Publishers []FB2Author `xml:"publisher"`
}

FB2DocInfo struct of fb2 document info. http://www.fictionbook.org/index.php/Элемент_document-info

func NewFB2DocInfo

func NewFB2DocInfo(
	tokenName string, reader xml.TokenReader, rules []HandlingRule,
) (res FB2DocInfo, err error)

NewFB2DocInfo factory for NewFB2DocInfo.

type FB2File

type FB2File struct {
	Description []FB2Description `xml:"description"`
	// Body        []FB2Body      `xml:"body"`
	Binary []FB2Binary `xml:"binary"`
}

FB2File struct of fb2 file. http://www.fictionbook.org/index.php/Элемент_FictionBook http://www.fictionbook.org/index.php/Описание_формата_FB2_от_Sclex

func NewFB2File

func NewFB2File(doc *xml.Decoder, rules ...HandlingRule) (res FB2File, err error)

NewFB2File factory for FB2File.

type FB2Image

type FB2Image struct {
	Type  string `xml:"type,attr"`
	Href  string `xml:"href,attr"`
	Alt   string `xml:"alt,attr"`
	Title string `xml:"title,attr"`
	ID    string `xml:"id,attr"`
}

FB2Image struct of fb2 image. http://www.fictionbook.org/index.php/Элемент_image

func NewFB2Image

func NewFB2Image(token xml.StartElement) (res FB2Image, err error)

NewFB2Image factory for FB2Image.

type FB2Publisher

type FB2Publisher struct {
	BookAuthor []string      `xml:"book-author"`
	BookName   []string      `xml:"book-name"`
	Publisher  []string      `xml:"publisher"`
	City       []string      `xml:"city"`
	Year       []string      `xml:"year"`
	ISBN       []string      `xml:"isbn"`
	Sequence   []FB2Sequence `xml:"sequence"`
}

FB2Publisher struct of fb2 publisher info. http://www.fictionbook.org/index.php/Элемент_publish-info

func NewFB2Publisher

func NewFB2Publisher(
	tokenName string, reader xml.TokenReader, rules []HandlingRule,
) (res FB2Publisher, err error)

NewFB2Publisher factory for FB2Publisher.

type FB2Sequence

type FB2Sequence struct {
	Number string `xml:"number,attr"`
	Name   string `xml:"name,attr"`
}

FB2Sequence struct of fb2 sequence info. http://www.fictionbook.org/index.php/Элемент_sequence

func NewFB2Sequence

func NewFB2Sequence(token xml.StartElement) (res FB2Sequence, err error)

NewFB2Sequence factory for FB2Sequence.

type FB2TitleInfo

type FB2TitleInfo struct {
	Genre      []string        `xml:"genre"`
	Author     []FB2Author     `xml:"author"`
	BookTitle  []string        `xml:"book-title"`
	Annotation []FB2Annotation `xml:"annotation"`
	Keywords   []string        `xml:"keywords"`
	Date       []string        `xml:"date"`
	Coverpage  []FB2Cover      `xml:"coverpage"`
	Lang       []string        `xml:"lang"`
	SrcLang    []string        `xml:"src-lang"`
	Translator []FB2Author     `xml:"translator"`
	Sequence   []FB2Sequence   `xml:"sequence"`
}

FB2TitleInfo struct of fb2 title info. http://www.fictionbook.org/index.php/Элемент_title-info

func NewFB2TitleInfo

func NewFB2TitleInfo(
	tokenName string, reader xml.TokenReader, rules []HandlingRule,
) (res FB2TitleInfo, err error)

NewFB2TitleInfo factory for FB2TitleInfo.

type HandlingRule added in v0.10.0

type HandlingRule func(next TokenHandler) TokenHandler

HandlingRule middleware for TokenHandler.

type TokenHandler added in v0.10.0

type TokenHandler func(section interface{}, node xml.StartElement, r xml.TokenReader) error

TokenHandler handler for fb2 tokens.

Jump to

Keyboard shortcuts

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