Documentation
¶
Overview ¶
Package xmlprint pretty-prints XML data.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AllBlankLinesPolicy = []BlankLinesPolicy{ CollapseBlankLines, PreserveBlankLines, RemoveBlankLines, }
These shouldn't be reordered, as their index positions are used by checked-in fuzz inputs.
var AllLineBreaksPolicy = []LineBreaksPolicy{ InferLineBreaks, LFLineBreaks, CRLFLineBreaks, }
var AllNonASCIIWhitespacePolicy = []NonASCIIWhitespacePolicy{ PreserveNonASCIIWhitespace, EscapeNonASCIIWhitespaceHex, EscapeNonASCIIWhitespaceDecimal, }
var AllQuotesPolicy = []QuotesPolicy{ DoubleQuotes, SingleQuotes, PreferDoubleQuotes, PreferSingleQuotes, }
var AllWhitespacePolicy = []WhitespacePolicy{ TrimWhitespace, CollapseWhitespace, PreserveWhitespace, }
These shouldn't be reordered, as their index positions are used by checked-in fuzz inputs.
Functions ¶
Types ¶
type BlankLinesPolicy ¶
type BlankLinesPolicy string
BlankLinesPolicy specifies how newlines in character data are handled.
const ( // CollapseBlankLines indicates that one or more blank lines will be collapsed to a single blank line. CollapseBlankLines BlankLinesPolicy = "collapse" // PreserveBlankLines indicates that all blank lines should be preserved. PreserveBlankLines BlankLinesPolicy = "preserve" // RemoveBlankLines indicates that all blank lines will be removed. RemoveBlankLines BlankLinesPolicy = "remove" )
type Config ¶
type Config struct { // PrintWidth is the desired maximum line length. PrintWidth int `json:"printWidth"` // IndentWidth is the number of spaces to use for each indentation level. // If UseTabs is true, this sets the number of characters that a tab will be considered to use. IndentWidth int `json:"indentWidth"` // TagWrapWidth is the number of spaces to use when wrapping a tag. // If UseTabs is true, TagWrapWidth is divided by IndentWidth to determine the number of tabs to use. TagWrapWidth int `json:"tagWrapWidth"` // CommentWrapWidth is the number of spaces to use when wrapping a comment. // If UseTabs is true, CommentWrapWidth is divided by IndentWidth to determine the number of // tabs to use. CommentWrapWidth int `json:"commentWrapWidth"` // UseTabs indicates that a tab character should be used for indenting (rather than spaces). UseTabs bool `json:"useTabs"` // LineBreaks specifies how lines should be terminated. LineBreaks LineBreaksPolicy `json:"lineBreaks"` // TrailingLineBreak indicates that a trailing line break should be written at the end of the // file. TrailingLineBreak bool `json:"trailingLineBreak"` // PackDepth specifies the maximum depth at which an element and its children will be printed on // a single line if all will fit. With a PackDepth of 2, "<a><b><c/></b></a>" (depth 2) would be // printed on a single line but "<a><b><c>foo</c></b></a>" (depth 3) would not. A negative value // specifies no limit and 0 specifies that elements should not be packed. PackDepth int `json:"packDepth"` // SelfClosing indicates that self-closing tags should be printed, e.g. "<element />". // Otherwise, they're written as separate tags, e.g. "<element></element>". SelfClosing bool `json:"selfClosing"` // SelfClosingSpace indicates that a space should be placed before the slash at the end of a // self-closing tag, e.g. "<element />" rather than "<element/>". SelfClosingSpace bool `json:"selfClosingSpace"` // ClosingBracketSameLine indicates that a multiline tag's closing bracket should be printed on // the same line as the final attribute. Otherwise, it's printed on a new line at the same // indentation level as the opening bracket. ClosingBracketSameLine bool `json:"closingBracketSameLine"` // Whitespace specifies the extent to which whitespace in character data should be preserved. Whitespace WhitespacePolicy `json:"whitespace"` // IgnoreXMLSpace indicates that "xml:space" attributes should be ignored. // If false, "xml:space" attributes with a value of "preserve" indicate that whitespace // should be preserved within all inner character data. IgnoreXMLSpace bool `json:"ignoreXMLSpace"` // BlankLines determines how blank lines in character data will be handled. BlankLines BlankLinesPolicy `json:"blankLines"` // NonASCIIWhitespace specifies how non-ASCII whitespace should be printed. NonASCIIWhitespace NonASCIIWhitespacePolicy `json:"nonASCIIWhitespace"` // RewriteCDATA indicates that "<![CDATA[...]]>" sections should be rewritten as escaped // character data. Otherwise, they are preserved (usually: large CDATA sections may be rewritten // even when this is false, with "large" currently defined as approximately 8 KiB). RewriteCDATA bool `json:"rewriteCDATA"` // FormatComments indicates that comments should be word-wrapped. // Otherwise, the original line breaks are preserved. // // Note that even with a false value, comment lines are still trimmed and indented. // To leave a comment completely unmodified (e.g. because it contains multiline ASCII art), // add an ignore directive immediately before it (see IgnoreCommentDirective). FormatComments bool `json:"formatComments"` // SingleAttributePerLine indicates that each attribute will be printed on its own line if the // entire tag doesn't fit on a single line. Otherwise, multiple attributes may be printed on a // single line if they fit. SingleAttributePerLine bool `json:"singleAttributePerLine"` // Quotes describes how attribute values should be quoted. Quotes QuotesPolicy `json:"quotes"` // SortAttributes describes whether a tag's attributes should be sorted lexicographically by // name, with "xmlns" attributes prioritized. If false, the original attribute order is preserved. SortAttributes bool `json:"sortAttributes"` // PrioritizeAttributes contains ordered names of attributes to prioritize when SortAttributes // is true. Names can be prefixed or unprefixed names (e.g. "ns:attr" or "attr") or namespaces // (to prioritize all attributes in that namespace). PrioritizeAttributes []string `json:"prioritizeAttributes"` // IgnoreCommentDirective specifies a string that can be placed in a comment (e.g. "xmlprint-ignore") // to indicate that the original formatting of the next node should be preserved. // "-start" and "-end" (e.g. "xmlprint-ignore-start" and "xmlprint-ignore-end") can be appended to // preserve all formatting between the two comments. IgnoreCommentDirective string `json:"ignoreCommentDirective"` // Ignore indicates that the file should not be formatted. // This is intended to be set in an override, not at the top level of a configuration. Ignore bool `json:"ignore"` // Overrides contains additional configuration to apply to specific files. Overrides []struct { // Files is a glob or array of globs matching XML files whose configuration should be // overriden. The glob syntax at https://pkg.go.dev/path/filepath#Match is used. // Globs are interpreted relative to the configuration file's directory. // Globs can also match one of the XML file's ancestor directories. // If the glob doesn't contain a slash, it will additionally be tested against the // base filename (e.g. "*bar.xml" will match "/blah/foobar.xml"). Files stringOrArray `json:"files"` // Options is a Config struct. Options json.RawMessage `json:"options"` } `json:"overrides"` // DebugChecks enables (possibly-slow) runtime assertions. DebugChecks bool `json:"-"` // DumpNodes indicates that nodes should be dumped instead of pretty-printing the XML data. DumpNodes bool `json:"-"` // DumpTokens indicates that the raw tokens returned by the decoder should be dumped instead // of pretty-printing the XML data. DumpTokens bool `json:"-"` // Iterations indicates that the number of times that the input should be formatted. // It's useful for debugging unstable output. Iterations int `json:"-"` // contains filtered or unexported fields }
Config specifies how XML data should be pretty-printed.
Names and values are chosen for consistency with https://github.com/g-plane/markup_fmt (https://markup-fmt.netlify.app/config/) and https://github.com/prettier/plugin-xml when there's no compelling reason to be different.
This struct should not be mutated after it has been initialized as it caches values. It is not safe to be used concurrently from multiple goroutines, and CloneWithoutOverrides should be used to make copies (and must be updated if new slice/map/etc. fields are added).
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config initialized to default values.
func (*Config) CloneWithoutOverrides ¶
Clone returns a deep copy of cfg. The Overrides field and various cached fields are cleared.
type InputError ¶
InputError wraps an error that occurred in the input and includes information about where it occurred.
func (InputError) Error ¶
func (ie InputError) Error() string
func (InputError) Unwrap ¶
func (ie InputError) Unwrap() error
type LineBreaksPolicy ¶
type LineBreaksPolicy string
LineBreaksPolicy specifies how lines should be terminated.
const ( // InferLineBreaks indicates that "\r\n" should be used if at least one "\r\n" // sequence occurs near the beginning of the input file, with "\n" used // otherwise. InferLineBreaks LineBreaksPolicy = "infer" // LFLineBreaks indicates that lines should be terminated with "\n". LFLineBreaks LineBreaksPolicy = "lf" // CRLFLineBreaks indicates that lines should be terminated with "\r\n". CRLFLineBreaks LineBreaksPolicy = "crlf" )
type NonASCIIWhitespacePolicy ¶
type NonASCIIWhitespacePolicy string
NonASCIIWhitespacePolicy indicates how non-ASCII whitespace should be printed within character data.
const ( // PreserveNonASCIIWhitespace indicates that non-ASCII whitespace should be printed unchanged. PreserveNonASCIIWhitespace NonASCIIWhitespacePolicy = "preserve" // EscapeNonASCIIWhitespaceHex indicates that non-ASCII whitespace should be printed as // hexadecimal numeric character entities, e.g. +U+00A0 becomes " ". EscapeNonASCIIWhitespaceHex NonASCIIWhitespacePolicy = "escapeHex" // EscapeNonASCIIWhitespaceDecimal indicates that non-ASCII whitespace should be printed as // decimal numeric character entities, e.g. +U+00A0 becomes " ". EscapeNonASCIIWhitespaceDecimal NonASCIIWhitespacePolicy = "escapeDecimal" )
type QuotesPolicy ¶
type QuotesPolicy string
QuotesPolicy specifies how attribute values are quoted.
const ( // DoubleQuotes indicates that double quotes should always be used. DoubleQuotes QuotesPolicy = "double" // SingleQuotes indicates that single quotes should always be used. SingleQuotes QuotesPolicy = "single" // PreferDoubleQuotes indicates that double quotes should be used unless the value // contains one or more double quotes and zero single quotes. PreferDoubleQuotes QuotesPolicy = "preferDouble" // PreferSingleQuotes indicates that single quotes should be used unless the value // contains one or more single quotes and zero double quotes. PreferSingleQuotes QuotesPolicy = "preferSingle" )
type WhitespacePolicy ¶
type WhitespacePolicy string
WhitespacePolicy specifies how whitespace in character data is handled.
const ( // TrimWhitespace attempts to intelligently handle whitespace in character data: // - Data consisting only of newlines and other whitespace will be replaced with // zero or more blank lines per BlankLinePolicy. // - Data containing at least one non-whitespace character is trimmed to remove leading // and trailing whitespace on each line, but all other internal whitespace is preserved. TrimWhitespace WhitespacePolicy = "trim" // CollapseWhitespace is like "trim" but additionally replaces sequences of // multiple whitespace characters with single spaces and wraps text after splitting it // into words. CollapseWhitespace WhitespacePolicy = "collapse" // PreserveWhitespace preserves all whitespace within elements containing non-whitespace // character data. // // As an example, given: // <element> // text 1 // <child>text 2</child> // </element> // the "\n text 1\n " and "\n " character data will be preserved, with no // additional whitespace inserted. // // Otherwise, it acts like "trim". // // Note that this policy can be substantially slower and use much more memory than other // policies when processing large files, since earlier character data often cannot be // processed until later character data has been seen. PreserveWhitespace WhitespacePolicy = "preserve" )