profile

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package profile loads and validates profiles (PROFILE_FORMAT.md): JSON files that map the elements of one XML schema to the shared model. A loaded profile is immutable and safe for concurrent use.

Index

Constants

View Source
const (
	MaxProfileBytes = 4 << 20
	MaxRules        = 10000
	MaxCells        = 64
	MaxHeaderBytes  = 256
)

MaxProfileBytes bounds the size of a profile file; MaxRules its rules. MaxCells bounds the entries of a row's "cells" and a table's "header"; MaxHeaderBytes the text of one header cell.

View Source
const (
	MediaHTML  = "text/html"
	MediaXHTML = "application/xhtml+xml"
	// MediaOctetStream is the media type of an attachment that says none.
	MediaOctetStream = "application/octet-stream"
)

Media types a "document" rule may hand over.

View Source
const (
	OpExists = iota // [@a]
	OpEquals        // [@a='v']
	OpToken         // [@a~='t']
)

Test operators of an attribute test.

View Source
const (
	MaxSelectorBytes = 4096
	MaxSteps         = 64
)

MaxSelectorBytes bounds a selector's text, MaxSteps its steps.

Variables

This section is empty.

Functions

func HasToken

func HasToken(v, t string) bool

HasToken reports whether t is one of the white-space separated tokens of v, without allocating.

func Tokens

func Tokens(v string) map[string]bool

Tokens returns the set of white-space separated tokens of v.

Types

type As

type As uint8

As is a rule kind.

const (
	AsSection As = iota + 1
	AsHeading
	AsBlock
	AsList
	AsItem
	AsTable
	AsRow
	AsCell
	AsInline
	AsLabel
	AsImage
	AsNote
	AsMetadata
	AsPart
	AsStored
	AsTransparent
	AsSkip
	AsBreak
	AsDocument
)

Rule kinds.

func (As) String

func (a As) String() string

type AttrTest

type AttrTest struct {
	Name  QName
	Op    int
	Value string
}

AttrTest is one attribute test of a step.

type Declarer added in v0.2.0

type Declarer interface {
	Declares(uri string) bool
}

Declarer is a root path that can tell which namespaces its element declares.

type Display

type Display struct {
	Attr   QName
	In     []string
	Reason gofastcore.Display
}

Display is a declared switch.

type Error

type Error struct {
	Rule int // 1-based, 0 for the profile as a whole
	Msg  string
}

Error is a profile that does not load.

func (*Error) Error

func (e *Error) Error() string

type KV

type KV struct {
	Key   string
	Value *Value
}

KV is a key and its value source.

type Level

type Level struct {
	Fixed   int
	Nesting bool
	Attr    *QName
}

Level of a heading.

type Match

type Match struct {
	Root          QName
	DoctypePublic string
	Attr          []AttrTest
	// Declares: the root element declares this namespace URI, with any
	// prefix or as the default ("declares").
	Declares string
}

Match is one entry of "match".

type Path

type Path interface {
	Len() int
	Name(i int) QName
	Attr(i int, name QName) (string, bool)
}

Path is the element being matched and its ancestors: index 0 is the element, 1 its parent, and so on. Attr of an ancestor need answer only for the names KeptAttrs lists.

type Profile

type Profile struct {
	Name        string
	Title       string
	HeadingBase int
	Matches     []Match
	Rules       []*Rule
	// contains filtered or unexported fields
}

Profile is a loaded profile.

func Load

func Load(r io.Reader, builtin bool) (*Profile, error)

Load reads and validates a profile. builtin refuses "skip" rules.

func (*Profile) Anchored

func (p *Profile) Anchored() bool

Anchored reports whether any selector is anchored at the root.

func (*Profile) ElementNamespace

func (p *Profile) ElementNamespace(uri string) string

ElementNamespace returns the namespace an element of namespace uri is matched in: the target of "namespace_aliases", or uri itself.

func (*Profile) KeptAttrs

func (p *Profile) KeptAttrs() []QName

KeptAttrs lists the attributes whose values a reader keeps on open elements: those that selectors test and those read from ancestors.

func (*Profile) Lookup

func (p *Profile) Lookup(path Path) *Rule

Lookup returns the rule for the element at index 0 of path, or nil.

func (*Profile) MatchesRoot

func (p *Profile) MatchesRoot(path Path, doctypePublic string) bool

MatchesRoot reports whether the profile applies to a document whose root is root with attributes attrs (index 0 of path) and DOCTYPE public identifier doctypePublic.

func (*Profile) MaxSteps

func (p *Profile) MaxSteps() int

MaxSteps returns the most steps of any selector.

func (*Profile) OfReads

func (p *Profile) OfReads(name QName) []QName

OfReads lists the attributes rules read from an ancestor of this name.

func (*Profile) ReadsChildAttrs

func (p *Profile) ReadsChildAttrs() bool

ReadsChildAttrs reports whether a rule reads an attribute of a child element ({"attr": ..., "child": ...}).

func (*Profile) TestedAttrs

func (p *Profile) TestedAttrs() []QName

TestedAttrs lists every attribute a selector tests.

type QName

type QName struct {
	URI, Local string
}

QName is a namespace URI and a local name.

func ResolveName

func ResolveName(name string, ns map[string]string, defaultNS string) (QName, error)

ResolveName resolves a prefixed or unprefixed name.

func (QName) String

func (q QName) String() string

type RestTable added in v0.3.0

type RestTable struct {
	Names  string   `json:"names"`
	ID     int      `json:"id"`
	Header []string `json:"header"`
}

RestTable is a "rest_table": the rows of names and values a row holds beyond its column cells, written as a table after the table. Names: "path" (relative to the row's element) or "local". ID: a column of the row whose text is the first cell of each of its rows (the line's id), 0 for none. Header: the table's header row, the profile's text.

type Rule

type Rule struct {
	Index    int
	Selector *Selector
	As       As

	Kind        gofastcore.Kind // block
	PartKind    gofastcore.PartKind
	StoredKind  string
	Language    string
	Notation    string
	NoteType    string
	Style       gofastcore.Style
	Model       string // rows or cals
	Field       string
	Key         string
	Join        string
	AlsoHeading int
	DuplicateOf string
	OutOfLine   bool // a block or note met inside an open block is a block of its own
	// Split: met inside an open paragraph-like block, the element ends
	// that block, is read on its own, and text after it starts a new
	// block ("split"), as a table or image does.
	Split bool
	// Space: the element's text is set off by white space ("space",
	// inline): a space is held before and after it, where the source has
	// none, as between the fields of a name or a citation.
	Space bool

	Level   Level
	Ordered *Test
	Header  *Test
	Colspan *QName
	Rowspan *QName
	Display *Display

	Marker, Link, Title, ID, Alt, Name, Value, Anchor *Value
	Attrs, Mark, Extra                                []KV

	// Cells are the cells of a row made from its element's attributes
	// ("cells", row). HeaderText is a header row of fixed text ("header",
	// table).
	Cells      []*Value
	HeaderText []string
	// Heading: a section's heading from its own attributes, the first
	// present and not empty ("heading", section: XLIFF file@original).
	Heading []*Value
	// HeadingAnchor: the child the section's anchor is read from is also
	// its heading, where the child stands ("heading": "anchor": a PubMed
	// record's PMID).
	HeadingAnchor bool
	// SectionTable: the section is also a table whose header row is
	// HeaderText ("table_header", section: an XLIFF 2 file, whose rows
	// have no table element of their own); its table block comes where
	// the section ends.
	SectionTable bool
	// Column: a cell placed in this column (1-based) of the row it stands
	// in, at any depth inside the row element ("column", cell). Rest: the
	// column that takes the row's content no column cell takes ("rest",
	// row). With: attributes of the element's own written after its text,
	// each set off by a space ("with": unitCode, currencyID, schemeID).
	// OneRow: the table's element is also its one row, filled by column
	// cells ("one_row", table: CII's monetary summation).
	Column int
	Rest   int
	// ColumnKey: the cell's column is chosen by this attribute's value
	// ("column_key", cell with "column"): each value as written has a
	// column of its own, from Column on, in order of first appearance in
	// the table, and its header cell is the value (TMX tuv@xml:lang).
	ColumnKey *Value
	With      []*Value
	OneRow    bool
	// Pairs: the table's element is read as names and values, a row for
	// each element with text inside it and each of their attributes
	// ("pairs", table: "path", "parent" (the path with the name of the
	// table's parent element before it) or "local"). RestTable: what a row (or a
	// one_row table) holds that no column cell takes is a table of names
	// and values after the table ("rest_table", row and table).
	Pairs string
	// PairsEmpty: in a "pairs" table an element with no content and no
	// attributes is a row of its name and an empty value ("pairs_empty").
	PairsEmpty bool
	RestTable  *RestTable

	// A "document" rule: the media type handed over, a constant
	// (MediaType) or chosen by an attribute (MediaAttr, MediaMap: a value
	// not in the map, or no attribute, and the rule does not apply).
	// WhenMarkup: the rule applies only when the element's text holds
	// markup; otherwise its fallback paragraph is made.
	MediaType  string
	MediaAttr  *QName
	MediaMap   map[string]string
	WhenMarkup bool
	// Base64: "encoding": "base64", the element's text is base64, decoded
	// before it is handed over (an attachment); with it, MediaAttr may
	// have no map: the attribute's value is the media type as written.
	// NameAttr: "name": {"attr": ...}, the attribute that names the
	// child (a file name).
	Base64   bool
	NameAttr *QName

	// Reads lists the attributes of the element the rule reads.
	Reads []QName
	// Claims lists the value sources read from children.
	Claims []*Value
}

Rule is a compiled rule.

func (*Rule) Media added in v0.2.0

func (r *Rule) Media(path Path) (string, bool)

Media returns the media type a "document" rule hands over for the element at index 0 of path, and whether the rule applies; other rules give ("", true).

type Selector

type Selector struct {
	Source   string
	Anchored bool
	Steps    []Step
	// contains filtered or unexported fields
}

Selector is a compiled selector. Steps go from the outermost element to the element matched.

func ParseSelector

func ParseSelector(s string, ns map[string]string, defaultNS string) (*Selector, error)

ParseSelector compiles a selector. Prefixes are resolved with ns; an unprefixed element name is in defaultNS, an unprefixed attribute name in no namespace.

func (*Selector) LastName

func (s *Selector) LastName() (QName, bool)

LastName returns the name of the last step, and whether it is "*".

func (*Selector) Match

func (s *Selector) Match(p Path) bool

Match reports whether the selector matches the element at index 0 of p.

func (*Selector) MatchFrom

func (s *Selector) MatchFrom(p Path, base int) bool

MatchFrom reports whether the selector, read relative to the element at index base of p, matches the element at index 0: the path from below base to 0 has exactly the selector's steps.

func (*Selector) Tests

func (s *Selector) Tests() int

Tests returns the number of attribute tests of all steps.

type Step

type Step struct {
	Name  QName
	Any   bool
	Tests []AttrTest
}

Step is one step of a selector: a name, or any name, with attribute tests.

type Test

type Test struct {
	Const bool
	Attr  *QName
	In    []string
}

Test is a boolean that is constant or an attribute test.

type TokenPath

type TokenPath interface {
	Path
	HasToken(i int, name QName, token string) bool
}

TokenPath is a Path that answers token tests itself, splitting each value once per element rather than once per test.

type Value

type Value struct {
	Literal   string
	IsLiteral bool
	Attr      QName  // {"attr": ...}
	Of        *QName // {"attr": ..., "of": ...}
	Child     *Selector
	Structure *Selector
	// AttrChild: {"attr": ..., "child": ...}, the attribute Attr of the
	// first matching child, which is still read by its own rule.
	AttrChild *Selector
	// Codepoint: {"attr": ..., "codepoint": "hex"}, the character whose
	// code point the attribute holds in hexadecimal (Formex QUOT.START).
	Codepoint bool
	// AnchorOf: {"anchor_of": ...}, in "anchor" only: the anchor of the
	// nearest enclosing element of that name, as its rule's "anchor" gave
	// it (PubMed: a record's heading takes the record's PMID).
	AnchorOf *QName
}

Value is a value source (A4).

func (*Value) Claims

func (v *Value) Claims() *Selector

Claims reports whether the value is read from a child element.

Jump to

Keyboard shortcuts

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