style

Mess around with SVG styling

svgis.style.add_style(svgfile, style, replace=False)

Add to or replace the CSS style in an SVG file.

Parameters:
  • svgfile (str) – Path to an SVG file or an SVG string.
  • newstyle (str) – CSS string, or path to CSS file.
  • replace (bool) – If true, replace the existing CSS with newstyle (default: False)
svgis.style.construct_classes(classes, properties)

Build a class string for an element using the properties. Class names take the form myclass_value. If a given class isn’t found in properties, the class name is added (e.g. myclass).

Parameters:
  • classes (Sequence) – Column names to include in the class list
  • properties (dict) – A single feature’s properties.
Returns:

(list) class names

svgis.style.construct_datas(fields, properties)

Build a data- attribute string for an element using the properties. Attributes take the form data_FIELD=PROPERTY.

Parameters:
  • datas (Sequence) – Column names to include in the class list
  • properties (dict) – A single feature’s properties.
Returns:

(dict) attribute dictionary

svgis.style.inline(svg)

Inline the CSS rules in an SVG. This is a very rough operation, and full css precedence rules won’t be respected. May ignore sibling operators (~, +), pseudo-selectors (e.g. :first-child), and attribute selectors (e.g. .foo[name=bar]). Works best with rules like:

  • .class
  • tag
  • tag.class
  • #layer .class
  • #layer tag
Parameters:
  • svg (string) – An SVG document.
  • style (string) – CSS to use, instead of the CSS in the <defs> element of the SVG.
svgis.style.pick(style)

Fetch a CSS string.

Parameters:style (str) – Either a CSS string or the path of a css file.
svgis.style.replace_comments(css)

Replace one-line non-standard comments with body-style comments:

// non-standard comment
/* body-style comment */
svgis.style.rescale(svgfile, factor)

Add a scale() operation to an entire svg file.

svgis.style.sanitize(string)

Make input safe of use in an svg ID or class field. Replaces blocks of whitespace with an underscore (_), deletes periods, number signs and double-quotes (.#"). If the first character isn’t an ascii letter, dash (-) or underscore (_), an underscore is added to the beginning.

Parameters:string (mixed) – Input to sanitize
Returns:str

Utilities for manipulating the DOM and applying styles to same

svgis.dom.ampencode(value)

Escape an ampersand that isn’t already url encoded

svgis.dom.apply_rule(doc, rule, nsmap)

Apply a tinycss2 rule to an etree.Element :param doc: The svg document to scan. :type doc: ElementTree.Element :param rule: tinycss2 rule :type rule: QualifiedRule :param nsmap: namespace map :type nsmap: dict

svgis.dom.apply_rules(doc, rules, nsmap=None)

Apply tinycss2 rules to an etree.Element (only tested on documents created by SVGIS).

Parameters:
  • doc (ElementTree.Element) – The svg document to scan.
  • rules (list) – List of tinycss2 Rules to apply.
  • nsmap (dict) – namespace map. Default: { "svg": "http://www.w3.org/2000/svg" }
svgis.dom.rule_content(rule)

Except the content from CSS rules.

svgis.dom.serialize_prelude(rule)

Convert the selector section of a CSS rule to string.

svgis.dom.serialize_token(token, previous_token=None)

Convert a tinycss2 selector to string in preparation for use with cssselect. This involves prepending ‘svg|’ to element selectors.

Parameters:token (tinycss2.ast.Node) – The selector to serialize.