Documentation
¶
Overview ¶
Package csshtml combines CSS instructions and HTML text to a DOM with style attributes at the selected nodes.
This package can be used to construct a DOM tree from a set of CSS files or strings and applied to HTML files or texts.
Index ¶
- Variables
- func GetAttributes(attrs []html.Attribute) map[string]string
- func PapersizeWidthHeight(spec string) (string, string)
- func ResolveAttributes(attrs []html.Attribute) (resolved map[string]string, newAttributes []html.Attribute)
- type CSS
- func (c *CSS) AddCSSText(fragment string) error
- func (c *CSS) ApplyCSS(doc *goquery.Document) (*goquery.Document, error)
- func (c *CSS) PopDir()
- func (c *CSS) ProcessHTMLChunk(htmltext string) (*goquery.Document, error)
- func (c *CSS) ProcessHTMLFile(filename string) (*goquery.Document, error)
- func (c *CSS) PushDir(dir string)
- type ContentToken
- type ContentTokenType
- type FontFace
- type FontSource
- type Page
Constants ¶
This section is empty.
Variables ¶
var CSSdefaults = `` /* 2089-byte string literal not displayed */
CSSdefaults contains browser-like styling of some elements.
Functions ¶
func GetAttributes ¶
GetAttributes returns a map from all HTML attributes.
func PapersizeWidthHeight ¶
PapersizeWidthHeight converts the spec to the width and height. The parameter can be a known paper size (such as A4 or letter) or a one or two parameter string such as 20cm 20cm. The return values are in one of the units 'mm' or 'in'. 'mm' if the parameter spec is one of a5, a4, a3, b5, b4, jis-b5 or jis-b4 and 'in' if the parameter spec is one of letter, legal or ledger.
func ResolveAttributes ¶
func ResolveAttributes(attrs []html.Attribute) (resolved map[string]string, newAttributes []html.Attribute)
ResolveAttributes returns the resolved styles and the attributes of the node. The argument attrs is unchanged. This function transforms rules such as "margin: 1cm;" into "margin-left: 1cm; margin-right: 1cm; ...". Attributes without a ! prefix are not resolved, just copied to the newAttributes return value. All other attributes are copied to the newAttributes prefixed with a * and all the resolved attributes prefixed with an exclamation mark. The resolved map contains only the resolved attributes and values without a prefix.
Types ¶
type CSS ¶
type CSS struct {
Pages map[string]Page
FileFinder func(string) (string, error)
FontFaces []FontFace
// contains filtered or unexported fields
}
CSS is the main structure that contains cascading style sheet information. Multiple stylesheets can be added to the CSS structure and then applied to HTML.
func NewCSSParserWithDefaults ¶
func NewCSSParserWithDefaults() *CSS
NewCSSParserWithDefaults returns a new CSS object with the default stylesheet included. This is a convenience function which adds the CSSdefaults to the returned CSS struct.
func (*CSS) AddCSSText ¶
AddCSSText parses CSS text and appends the rules to the previously read rules. If the fragment contains relative links to other files (fonts or other stylesheets for example), the dir stack must be set in advance.
func (*CSS) ApplyCSS ¶
ApplyCSS resolves CSS rules in the DOM. Each CSS rule is added to the selection as an attribute (prefixed with a !). Pseudo elements are prefixed with ::.
func (*CSS) ProcessHTMLChunk ¶
ProcessHTMLChunk reads the HTML text. If there are linked style sheets (<link href=...) these are also read. After reading, the CSS is applied to the HTML DOM which is returned.
func (*CSS) ProcessHTMLFile ¶
ProcessHTMLFile opens an HTML file, reads linked stylesheets, applies the CSS rules and returns the DOM structure.
type ContentToken ¶ added in v0.0.3
type ContentToken struct {
Type ContentTokenType
Value string // string literal or counter name ("page", "pages")
}
ContentToken represents a single parsed piece of a CSS content property value.
type ContentTokenType ¶ added in v0.0.3
type ContentTokenType int
ContentTokenType distinguishes different kinds of CSS content tokens.
const ( // ContentString is a literal string from a CSS content value. ContentString ContentTokenType = iota // ContentCounter is a counter() function call. ContentCounter // ContentLeader is a leader() function call (CSS GCPM). ContentLeader // ContentURL is a url() reference to an image or other resource. ContentURL )
type FontFace ¶
type FontFace struct {
Weight int
Style string
Family string
Source []FontSource
Features []string
VariationSettings map[string]float64 // axis tag -> value (e.g., "wght" -> 700)
SizeAdjust float64
}
FontFace contains information from a @font-face rule.
type FontSource ¶
FontSource has information from the src attribute.
type Page ¶
type Page struct {
PageArea map[string]map[string]string // key value pairs for the page areas
PageAreaContent map[string][]ContentToken // parsed content tokens per area
Attributes []html.Attribute
Papersize string
MarginLeft string
MarginRight string
MarginTop string
MarginBottom string
// contains filtered or unexported fields
}
Page defines a page.