19.2.1 Customization of CSS Rules, Imports and Selectors

Information on static CSS data used in conversion and more direct control over rules, CSS imports and selectors is available through functions. The information is about CSS rules lines and CSS import lines obtained from parsing --css-include=file files, as described in HTML CSS in Texinfo, and CSS style rules associated with HTML elements and class attributes used in the conversion to HTML. The CSS style rules selectors are, classically, element.class strings with element an HTML element and class an attribute class associated with that element.

The function used are css_get_info and css_get_selector_style to get information and css_add_info and css_set_selector_style to modify:

Function: $converter->css_get_info ($specification)
Function: $converter->css_get_selector_style ($selector)
Function: $converter->css_add_info ($specification, $css_info)
Function: $converter->css_set_selector_style ($selector, $css_style)

Those functions can only be used on a converter $converter, from functions registered and called with a converter. $specification is 'rules' to get information on or set information for CSS rules lines and 'imports' to get information on or set information for CSS import lines. Any other value for $specification corresponds to HTML elements and class attributes selectors, and can be used to get the list of selectors.

With css_get_info, array references corresponding to $specification are returned.

css_get_selector_style returns the CSS style corresponding to the HTML element and class attribute selector $selector, or undef if not found.

With css_add_info, $css_info is an additional entry added to CSS rules lines if $specification is set to 'rules' or an additional entry added to CSS import lines if $specification is set to 'imports'.

With css_set_selector_style, $selector is a CSS rule selector and the associated style rule is set to $css_style.

Some examples of use:

my @all_included_rules = $converter->css_get_info('rules');
my $all_default_selector_styles = $converter->css_get_info('styles');
my $titlefont_header_style = $converter->css_get_selector_style('h1.titlefont');

$converter->css_set_selector_style('h1.titlefont', 'text-align:center');
$converter->css_add_info('imports', "\@import \"special.css\";\n");

Note that the CSS selectors and associated style rules that can be accessed and modified will not necessarily end up in the HTML output. They are output only if the HTML element and class corresponding to a selector is seen in the document. See Customizing the CSS Lines.

The simplest way to modify CSS rules would be to use a function registered for the ‘structure’ stage:

sub my_function_set_some_css {
  my $converter = shift;

  $converter->css_set_selector_style('h1.titlefont',
                                     'text-align:center');
  # ... calls to  $converter->css_add_info();
}

texinfo_register_handler('structure', \&my_function_set_some_css);