csshtml

package module
v0.0.0-...-f214983 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: BSD-3-Clause Imports: 14 Imported by: 2

README

Apply CSS to a HTML DOM tree.

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

Constants

This section is empty.

Variables

View Source
var CSSdefaults = `` /* 2070-byte string literal not displayed */

CSSdefaults contains browser-like styling of some elements.

Functions

func GetAttributes

func GetAttributes(attrs []html.Attribute) map[string]string

GetAttributes returns a map from all HTML attributes.

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 NewCSSParser

func NewCSSParser() *CSS

NewCSSParser returns a new CSS object

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

func (c *CSS) AddCSSText(fragment string) error

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

func (c *CSS) ApplyCSS(doc *goquery.Document) (*goquery.Document, error)

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) PopDir

func (c *CSS) PopDir()

PopDir removes the last entry from the dir stack.

func (*CSS) ProcessHTMLChunk

func (c *CSS) ProcessHTMLChunk(htmltext string) (*goquery.Document, error)

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

func (c *CSS) ProcessHTMLFile(filename string) (*goquery.Document, error)

ProcessHTMLFile opens an HTML file, reads linked stylesheets, applies the CSS rules and returns the DOM structure.

func (*CSS) PushDir

func (c *CSS) PushDir(dir string)

PushDir adds a directory to the dir stack. When a file is opened, all new Open calls are relative to this directory. ProcessHTMLFile uses the dir stack internally when it reads a cascade of CSS files.

type FontFace

type FontFace struct {
	Weight     int
	Style      string
	Family     string
	Source     []FontSource
	Features   []string
	SizeAdjust float64
}

FontFace contains information from a @font-face rule.

type FontSource

type FontSource struct {
	Local  string
	URI    string
	Format string
	Tech   string
}

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
	Attributes   []html.Attribute
	Papersize    string
	MarginLeft   string
	MarginRight  string
	MarginTop    string
	MarginBottom string
	// contains filtered or unexported fields
}

Page defines a page.

Jump to

Keyboard shortcuts

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