11. unparse-styles

unparse-styles(css-uri?, custom-styles-url-or-file?)

The inverse command of parse-styles:

  1. If the document being edited has “interned” CSS rules, adds an html/head/style element containing these CSS rules. If the document already has a style element, this style element is replaced by a new one containing the interned CSS rules.

  2. If an element has “interned” CSS classes, adds to the element a class attribute containing these CSS classes. If the element already has a class attribute, interned CSS classes are prepended to the content of the class attribute.

  3. If an element has “interned” CSS style properties, adds to the element a style attribute containing these CSS style properties. If the element already has a style attribute, interned CSS style properties are prepended to the content of the style attribute.

Argument css-uri defaults to the empty string "". If argument css-uri specifies an absolute or relative "file:" URI, “interned” CSS rules, if any, are saved to this (UTF-8 encoded, text) file. A relative "file:" URI is relative to the URI of the document being edited.

Note that when css-uri has been specified, an html/head/style element is not added to the document being edited.

Excerpts from xed/finish-styles.xed, which consists essentially in a call to unparse-styles:

unparse-styles($cssURI, $customStylesLocation);

if $cssURI != "" {
   insert-into(<link href="{$cssURI}" rel="stylesheet" type="text/css" />, 
               false(), /html/head);
}

Invoking unparse-styles removes all s:rules, s:class, s:style application-level properties. Therefore after that, it is no longer possible to use the following XPath extension functions: find-rule, font-size, get-class, get-rule, get-style, lookup-length, lookup-style, style-count, or the following editing commands: add-class, add-rule, remove-class, remove-rule, set-rule, set-style.

Customizing the automatically generated CSS styles

Argument custom-styles-url-or-file makes it easy customizing the automatically generated CSS styles. The custom CSS styles found in specified file are simply appended to the automatically generated CSS styles. This argument defaults to the empty string "" (no custom styles). Its value may be an absolute URL or a filename. A relative filename is relative to the current working directory.

Example 1:

w2x -pu edit.finish-styles.custom-styles-url-or-file customize/custom.css \
  manual.docx out/manual_restyled.html

where customize/custom.css contains:

body {
    font-family: sans-serif;
}

.p-Heading1,
.p-Heading2,
.p-Heading3,
.p-Heading4,
.p-Heading5,
.p-Heading6 {
    font-family: serif;
    color: #17365D;
    padding: 1pt;
    border-bottom: 1pt solid #4F81BD;
    margin-bottom: 10pt;
    margin-left: 0pt;
    text-indent: 0pt;
}

.p-Heading1 {
    border-bottom-width: 2pt;
}

...

.c-FootnoteReference,
.c-EndnoteReference {
    font-size: smaller;
}

Example 2: all CSS styles, whether automatically generated or custom, are saved to a separate file manual_restyled_css/manual.css:

w2x -p edit.finish-styles.css-uri manual_restyled_css/manual.css \
  -pu edit.finish-styles.custom-styles-url-or-file customize/custom.css \
  manual.docx out/manual_restyled.html