= How can I adapt the StanDoc toolchain for my own publications?
[TIP]
====
The easiest way to adopt StanDoc is to use the metanorma-acme gem: https://github.com/riboseinc/metanorma-acme, supplying your own stylesheets and HTML files for styling.
If you wish to create a custom gem, in order to customise behaviour further:
* Clone the metanorma-sample gem: https://github.com/riboseinc/metanorma-sample.
* Change the namespace for RSD documents (`RSD_NAMESPACE = "https://open.ribose.com/standards/rsd"`) to a namespace specific to your organisation's document standard.
* Change any references to `sample` or `Sample` in the gem to your organisation's document standard.
* Change the styling of the document outputs (`.../lib/isodoc/XXX/html`).
====
The tool chains currently available proceed in two steps: map an input markup language (currently Asciidoctor only) into Standoc XML, and map Standoc XML into various output formats (currently Word doc, HTML, PDF via HTML). Running the metanorma tool involves a third step, of exposing the capabilities available in the first two in a consistent format. These two steps are represented as three separate modules, which are included in the same gem; for the Sample gem, they are `Asciidoctor::Sample`, `Isodoc::Sample`, and `Metanorma::Sample`. (In the case of Metanorma-ISO, almost all the content of `Isodoc::ISO` is in the isodoc gem, so the base classes of the two steps are in separate gems.)
Your adaptation of the toolchain will need to instantiate these three modules. The connection between the two first steps is taken care of in the toolchain, and metanorma explicitly invokes the two steps, feeding the XML output of the first step as input into the second. The metanorma-sample gem outputs both Word and HTML; you can choose to output only Word (as is done in metanorma-m3d), or only HTML (as is done in metanorma-csand), and you can choose to generate PDF from HTML as well (as is done in metanorma-csd).
The modules involve classes which rely on inheritance from other classes; the current gems use `Asciidoctor::{Standoc, ISO}::Converter`, `Isodoc::{Metadata, HtmlConvert, WordConvert}`, and `Metanorma::Processor` as their base classes. This allows the standards-specific classes to be quite succinct, as most of their behaviour is inherited from other classes; but it also means that you need to be familiar with the underlying gems, in order to do most customisation.
In the case of `Asciidoctor::X` classes, the changes you will need to make involve the intermediate XML representation of your document, which is built up through Nokogiri Builder; e.g. adding different enums, or adding new elements. The adaptations in `Asciidoctor::Sample::Converter` are limited, and most projects can take them across as is.
The customisations needed for `Metanorma::Sample::Processor` are minor, and involve invoking methods specific to the gem for document generation.
The customisations needed for `Isodoc::Sample` are more extensive. Three base classes are involved:
* `Isodoc::Metadata` processes the metadata about the document stored in `//bibdata`. This information typically ends up in the document title page, as opposed to the document body. For that reason, metadata is extracted into a hash, which is passed to document output (title page, Word header) via the https://shopify.github.io/liquid/[Liquid template language].
* `Isodoc::HtmlConvert` converts Standoc XML to HTML.
* `Isodoc::WordConvert` converts Standoc XML to Word HTML; the https://github.com/riboseinc/html2doc[html2doc] gem then converts this to a .doc document.
The `Isodoc::HtmlConvert` and `Isodoc::WordConvert` overlap substantially, as both use variants of HTML. However there is no reason not to make substantially different rendering choices in the HTML and Word branches of the code.
=== Asciidoctor::Standoc customisation in metanorma-sample
Examples from `Asciidoctor::Sample` in metanorma-sample. In the following snippets, the parameter `node` represents the current node of the Asciidoctor document, and `xml` represents the Nokogiri Builder node of the XML output.
* The boilerplate representation of the document's author, publisher and copyright holder names Acme instead of ISO as the responsible organisation.
[source,ruby]
--
def metadata_author(node, xml)
xml.contributor do |c|
c.role **{ type: "author" }
c.organization do |a|
a.name "Acme"
end
end
end
--
* The editorial committees are represented as a single element, as opposed to ISO's name plus number. (`node.attr()` recovers Asciidoctor document attribute values.)
[source,ruby]
--
def metadata_committee(node, xml)
xml.editorialgroup do |a|
a.committee node.attr("committee"),
**attr_code(type: node.attr("committee-type"))
end
end
--
* The document title is monolingual, not bilingual. (`asciidoc_sub()` is already defined to apply Asciidoctor text substitutions to its contents, such as smart quotes and em-dashes.)
[source,ruby]
--
def title(node, xml)
["en"].each do |lang|
xml.title **{ language: lang, format: "plain" } do |t|
t << asciidoc_sub(node.attr("title"))
end
end
end
--
* The document status is a single element, as opposed to ISO's two-part code.
[source,ruby]
--
def metadata_status(node, xml)
xml.status(**{ format: "plain" }) { |s| s << node.attr("status") }
end
--
* The document identifier is a single element.
[source,ruby]
--
def metadata_id(node, xml)
xml.docidentifier { |i| i << node.attr("docnumber") }
end
--
* A security element is added to the document metadata.
[source,ruby]
--
def metadata_security(node, xml)
security = node.attr("security") || return
xml.security security
end
def metadata(node, xml)
super
metadata_security(node, xml)
end
--
* Title validation and style validation is disabled.
[source,ruby]
--
def title_validate(root)
nil
end
--
* The root element of the document is changed from `iso-standard` to `sample-standard`.
[source,ruby]
--
def makexml(node)
result = ["\n