metanorma-iso 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ = How can I adopt the StanDoc specification for my own publications?
2
+
3
+ TIP: Copy the RSD schema from https://github.com/riboseinc/metanorma-iso/blob/master/grammars/rsd.rng. You may need to adapt some of the enums in the model, or in the ISO Standards model that it inherits; but in the first instance, you can just ignore the differences—and ignore the validation feedback that the toolset gives.
4
+
5
+ The Standoc specification is expressed in http://www.relaxng.org[RelaxNG schema for XML], and is intended to be customisable for different types of publication. The customisation of Standoc relies on inheritance, with the following schemas embedded hierarchically:
6
+
7
+ * https://github.com/riboseinc/bib-models[Relaton]: bibliography
8
+ * https://github.com/riboseinc/basicdoc-models[BasicDoc]: block-level and inline formatting
9
+ * https://github.com/riboseinc/metanorma-standoc[StanDoc]: organisation of sections for a generic standards document
10
+ * Models specific to standards
11
+
12
+ Specialisation of a model consists of:
13
+
14
+ * Adding classes to a base model.
15
+ * Changing attributes of a base model class. This is not restricted to adding attributes, as is the case in typical entity subclassing; it can also include removing attributes from a class, changing their obligation and cardinality, and changing their type, including changing enumerations. Attributes can be overruled at any level; for example, standards-specific models routinely enhance the bibliographic model at the base of the hierarchy.
16
+ * For reasons of clarity, renaming classes and attributes is avoided in specialisation.
17
+
18
+ To adapt the schema for your publication set,
19
+
20
+ * Get familiar with the Standoc set of models, and identify any elements that you would want to represent differently for your documents (different types, different enums), or enhance for your documents (additional element attributes, additional elements)
21
+ * Create a grammar inheriting from StanDoc or from a specific standard, which expresses what is distinctive about your grammar.
22
+ ** We recommend starting your modelling in UML, as an effective communication tool; compare the UML models for Standoc standards at https://github.com/riboseinc/metanorma-model-iso
23
+ ** The tool suite expects to validate against a set of schemas expressed in RelaxNG. We have been authoring grammars in RelaxNG Compact, as a more human-readable format, then compiling those grammars to RelaxNG using https://github.com/relaxng/jing-trang[jing-trang]. You can choose to use a different schema language, but you will need to customise the tool chain to validate against that form of schema instead.
24
+ ** In order to make schema inheritance easier, we have avoided using namespaces for the individual schemas; a namespace is added to the standards-specific schema at the very end of the inheritance chain.
25
+
26
+ === Schema customisation
27
+
28
+ For example, the `rsd.rnc` schema, expressed in RelaxNG Compact, is specific to Ribose Standard documents: it inherits from the ISO Standards model, modifying 10 elements, and adding five. As an example of modifying elements, RSD permits preformatted text (Ascii art) in figures, which ISO does not:
29
+
30
+ .ISO
31
+ [source,asciidoctor]
32
+ ----
33
+ figure =
34
+ element figure {
35
+ attribute id { xsd:ID },
36
+ tname?,
37
+ ( image | subfigure+ ),
38
+ fn*, dl?, note*
39
+ }
40
+ ----
41
+
42
+ .RSD
43
+ [source,asciidoctor]
44
+ ----
45
+ figure =
46
+ element figure {
47
+ attribute id { xsd:ID },
48
+ tname?,
49
+ ( image | pre | subfigure+ ),
50
+ fn*, dl?, note*
51
+ }
52
+ ----
53
+
54
+ The preformatted text tag `pre` is an addition to the RSD specification, which is why it lies outside the `include "isostandard.rnc {}"` container:
55
+
56
+ .RSD
57
+ [source,asciidoctor]
58
+ ----
59
+ pre = element pre { text }
60
+ ----
61
+
62
+ As another instance of customisation, the `BibItemType` enumeration of permissible bibliographical item types is extended in RSD to include the document types specific to RSD:
63
+
64
+ .RSD
65
+ [source,asciidoctor]
66
+ ----
67
+ BibItemType |=
68
+ "policy-and-procedures" | "best-practices" | "supporting-document" | "report" | "legal" | "directives" | "proposal" |
69
+ "standard"
70
+ ----
71
+
72
+
@@ -0,0 +1,389 @@
1
+ = How can I adapt the StanDoc toolchain for my own publications?
2
+
3
+ [TIP]
4
+ ====
5
+ 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.
6
+
7
+ If you wish to create a custom gem, in order to customise behaviour further:
8
+
9
+ * Clone the metanorma-sample gem: https://github.com/riboseinc/metanorma-sample.
10
+ * Change the namespace for RSD documents (`RSD_NAMESPACE = "https://open.ribose.com/standards/rsd"`) to a namespace specific to your organisation's document standard.
11
+ * Change any references to `sample` or `Sample` in the gem to your organisation's document standard.
12
+ * Change the styling of the document outputs (`.../lib/isodoc/XXX/html`).
13
+ ====
14
+
15
+ 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.)
16
+
17
+ 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).
18
+
19
+ 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.
20
+
21
+ 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.
22
+
23
+ The customisations needed for Metanorma::Sample::Processor are minor, and involve invoking methods specific to the gem for document generation.
24
+
25
+ The customisations needed for Isodoc::Sample are more extensive. Three base classes are involved:
26
+
27
+ * `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].
28
+ * `Isodoc::HtmlConvert` converts Standoc XML to HTML.
29
+ * `Isodoc::WordConvert` converts Standoc XML to Word HTML; the https://github.com/riboseinc/html2doc[html2doc] gem then converts this to a .doc document.
30
+
31
+ 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.
32
+
33
+ === Asciidoctor::Standoc customisation in metanorma-sample
34
+
35
+ 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.
36
+
37
+ * The boilerplate representation of the document's author, publisher and copyright holder names Acme instead of ISO as the responsible organisation.
38
+
39
+ [source,ruby]
40
+ --
41
+ def metadata_author(node, xml)
42
+ xml.contributor do |c|
43
+ c.role **{ type: "author" }
44
+ c.organization do |a|
45
+ a.name "Acme"
46
+ end
47
+ end
48
+ end
49
+ --
50
+
51
+ * The editorial committees are represented as a single element, as opposed to ISO's name plus number. (`node.attr()` recovers Asciidoctor document attribute values.)
52
+
53
+ [source,ruby]
54
+ --
55
+ def metadata_committee(node, xml)
56
+ xml.editorialgroup do |a|
57
+ a.committee node.attr("committee"),
58
+ **attr_code(type: node.attr("committee-type"))
59
+ end
60
+ end
61
+ --
62
+
63
+ * 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.)
64
+
65
+ [source,ruby]
66
+ --
67
+ def title(node, xml)
68
+ ["en"].each do |lang|
69
+ xml.title **{ language: lang, format: "plain" } do |t|
70
+ t << asciidoc_sub(node.attr("title"))
71
+ end
72
+ end
73
+ end
74
+ --
75
+
76
+ * The document status is a single element, as opposed to ISO's two-part code.
77
+
78
+ [source,ruby]
79
+ --
80
+ def metadata_status(node, xml)
81
+ xml.status(**{ format: "plain" }) { |s| s << node.attr("status") }
82
+ end
83
+ --
84
+
85
+ * The document identifier is a single element.
86
+
87
+ [source,ruby]
88
+ --
89
+ def metadata_id(node, xml)
90
+ xml.docidentifier { |i| i << node.attr("docnumber") }
91
+ end
92
+ --
93
+
94
+ * A security element is added to the document metadata.
95
+
96
+ [source,ruby]
97
+ --
98
+ def metadata_security(node, xml)
99
+ security = node.attr("security") || return
100
+ xml.security security
101
+ end
102
+
103
+ def metadata(node, xml)
104
+ super
105
+ metadata_security(node, xml)
106
+ end
107
+ --
108
+
109
+ * Title validation and style validation is disabled.
110
+
111
+ [source,ruby]
112
+ --
113
+ def title_validate(root)
114
+ nil
115
+ end
116
+ --
117
+
118
+ * The root element of the document is changed from `iso-standard` to `sample-standard`.
119
+
120
+ [source,ruby]
121
+ --
122
+ def makexml(node)
123
+ result = ["<?xml version='1.0' encoding='UTF-8'?>\n<sample-standard>"]
124
+ @draft = node.attributes.has_key?("draft")
125
+ result << noko { |ixml| front node, ixml }
126
+ result << noko { |ixml| middle node, ixml }
127
+ result << "</sample-standard>"
128
+ ....
129
+ end
130
+ --
131
+
132
+ * The document type attribute is restricted to a prescribed set of options.
133
+
134
+ [source,ruby]
135
+ --
136
+ def doctype(node)
137
+ d = node.attr("doctype")
138
+ unless %w{policy-and-procedures best-practices
139
+ supporting-document report legal directives proposal
140
+ standard}.include? d
141
+ warn "#{d} is not a legal document type: reverting to 'standard'"
142
+ d = "standard"
143
+ end
144
+ d
145
+ end
146
+ --
147
+
148
+ * The `literal` asciidoctor block is processed as a preformatted tag (`pre`).
149
+ (The code uses the built-in Asciidoctor `literal()` method, and embeds `pre` within a `figure` tag.)
150
+
151
+ [source,ruby]
152
+ --
153
+ def literal(node)
154
+ noko do |xml|
155
+ xml.figure **id_attr(node) do |f|
156
+ figure_title(node, f)
157
+ f.pre node.lines.join("\n")
158
+ end
159
+ end
160
+ end
161
+ --
162
+
163
+ * A `keyword` element is added. (The keyword is encoded through the role attribute of Asciidoc: `[.keyword]#text#`)
164
+
165
+ [source,ruby]
166
+ --
167
+ def inline_quoted(node)
168
+ noko do |xml|
169
+ case node.type
170
+ ...
171
+ else
172
+ case node.role
173
+ ...
174
+ when "keyword" then xml.keyword node.text
175
+ else
176
+ xml << node.text
177
+ end
178
+ end
179
+ end.join
180
+ end
181
+ --
182
+
183
+ * The inline headers of ISO are ignored.
184
+
185
+ [source,ruby]
186
+ --
187
+ def sections_cleanup(x)
188
+ super
189
+ x.xpath("//*[@inline-header]").each do |h|
190
+ h.delete("inline-header")
191
+ end
192
+ end
193
+ --
194
+
195
+ === Metanorma::Processor customisation in metanorma-sample
196
+
197
+ * `initialize` names the token by which Asciidoctor registers the standard
198
+
199
+ [source,ruby]
200
+ --
201
+ def initialize
202
+ @short = :sample
203
+ @input_format = :asciidoc
204
+ @asciidoctor_backend = :sample
205
+ end
206
+ --
207
+
208
+ * `output_formats` names the available output formats (including XML, which is inherited from the parent class)
209
+
210
+ [source,ruby]
211
+ --
212
+ def output_formats
213
+ super.merge(
214
+ html: "html",
215
+ doc: "doc",
216
+ pdf: "pdf"
217
+ )
218
+ end
219
+ --
220
+
221
+ * `version` gives the current version string for the gem
222
+
223
+ [source,ruby]
224
+ --
225
+ def version
226
+ "Asciidoctor::Sample #{Asciidoctor::Sample::VERSION}"
227
+ end
228
+ --
229
+
230
+ * `input_to_isodoc` is the call which converts Asciidoctor input into IsoDoc XML
231
+
232
+ [source,ruby]
233
+ --
234
+ def input_to_isodoc(file, filename)
235
+ Metanorma::Input::Asciidoc.new.process(file, filename, @asciidoctor_backend)
236
+ end
237
+ --
238
+
239
+ * `output` is the call which converts IsoDoc XML into various nominated output formats
240
+
241
+ [source,ruby]
242
+ --
243
+ def output(isodoc_node, outname, format, options={})
244
+ case format
245
+ when :html
246
+ IsoDoc::Sample::HtmlConvert.new(options).convert(outname, isodoc_node)
247
+ when :doc
248
+ IsoDoc::Sample::WordConvert.new(options).convert(outname, isodoc_node)
249
+ when :pdf
250
+ IsoDoc::Sample::PdfConvert.new(options).convert(outname, isodoc_node)
251
+ else
252
+ super
253
+ end
254
+ end
255
+ --
256
+
257
+ === Isodoc::Standoc customisation in metanorma-sample
258
+
259
+ * Initialise the HTML Converter:
260
+ ** Set `@libdir`, the current directory of the HTML converter, and the basis of the `html_doc_path()` method for accessing HTML assets (the `html` subdirectory of the current directory).
261
+ ** Copy the logo JPG from the HTML asset directory to the working directory, so that it can be access by the HTML template; flag the copy for deletion at the end of processing.
262
+
263
+ [source,ruby]
264
+ --
265
+ def initialize(options)
266
+ @libdir = File.dirname(__FILE__)
267
+ super
268
+ FileUtils.cp html_doc_path('logo.jpg'), "logo.jpg"
269
+ @files_to_delete << "logo.jpg"
270
+ end
271
+ --
272
+
273
+ * Set the default fonts for the HTML rendering, which will be used to populate the HTML CSS stylesheet.
274
+
275
+ [source,ruby]
276
+ --
277
+ class HtmlConvert < IsoDoc::HtmlConvert
278
+ def default_fonts(options)
279
+ {
280
+ bodyfont: (options[:script] == "Hans" ? '"SimSun",serif' : '"Overpass",sans-serif'),
281
+ headerfont: (options[:script] == "Hans" ? '"SimHei",sans-serif' : '"Overpass",sans-serif'),
282
+ monospacefont: '"Space Mono",monospace'
283
+ }
284
+ end
285
+ --
286
+
287
+ * Set the default HTML assets for the HTML rendering.
288
+
289
+ [source,ruby]
290
+ --
291
+ class HtmlConvert < IsoDoc::HtmlConvert
292
+ def default_file_locations(_options)
293
+ {
294
+ htmlstylesheet: html_doc_path("htmlstyle.scss"),
295
+ htmlcoverpage: html_doc_path("html_sample_titlepage.html"),
296
+ htmlintropage: html_doc_path("html_sample_intro.html"),
297
+ scripts: html_doc_path("scripts.html"),
298
+ }
299
+ end
300
+ --
301
+
302
+ * Set distinct default fonts and HTML assets for the Word rendering.
303
+
304
+ [source,ruby]
305
+ --
306
+ class WordConvert < IsoDoc::WordConvert
307
+ def default_fonts(options)
308
+ {
309
+ bodyfont: (options[:script] == "Hans" ? '"SimSun",serif' : '"Arial",sans-serif'),
310
+ headerfont: (options[:script] == "Hans" ? '"SimHei",sans-serif' : '"Arial",sans-serif'),
311
+ monospacefont: '"Courier New",monospace'
312
+ }
313
+ end
314
+
315
+ def default_file_locations(_options)
316
+ {
317
+ wordstylesheet: html_doc_path("wordstyle.scss"),
318
+ standardstylesheet: html_doc_path("sample.scss"),
319
+ header: html_doc_path("header.html"),
320
+ wordcoverpage: html_doc_path("word_sample_titlepage.html"),
321
+ wordintropage: html_doc_path("word_sample_intro.html"),
322
+ ulstyle: "l3",
323
+ olstyle: "l2",
324
+ }
325
+ end
326
+ --
327
+
328
+
329
+ * Set the content of the HTML head, other than the CSS stylesheets. Note that the head title is given as a Liquid Template reference to metadata ()`{{ doctitle }}`, which we have seen populated above.)
330
+
331
+ [source,ruby]
332
+ --
333
+ def html_head
334
+ <<~HEAD.freeze
335
+ <title>{{ doctitle }}</title>
336
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
337
+ <!--TOC script import-->
338
+ <script type="text/javascript" src="https://cdn.rawgit.com/jgallen23/toc/0.3.2/dist/toc.min.js"></script>
339
+ <!--Google fonts-->
340
+ ....
341
+ <style class="anchorjs"></style>
342
+ HEAD
343
+ end
344
+ --
345
+
346
+ * Change the default label for annexes from "Annex" to "Appendix".
347
+
348
+ [source,ruby]
349
+ --
350
+ def i18n_init(lang, script)
351
+ super
352
+ @annex_lbl = "Appendix"
353
+ end
354
+ --
355
+
356
+ * Define rendering for the `pre` and `keyword` preformatted text tags.
357
+
358
+ [source,ruby]
359
+ --
360
+ def pre_parse(node, out)
361
+ out.pre node.text # content.gsub(/</, "&lt;").gsub(/>/, "&gt;")
362
+ end
363
+
364
+ def error_parse(node, out)
365
+ # catch elements not defined in ISO
366
+ case node.name
367
+ when "pre"
368
+ pre_parse(node, out)
369
+ when "keyword"
370
+ out.span node.text, **{ class: "keyword" }
371
+ else
372
+ super
373
+ end
374
+ end
375
+ --
376
+
377
+ * Render term headings in the same paragraph as the term heading number
378
+
379
+ [source,ruby]
380
+ --
381
+ def term_cleanup(docxml)
382
+ docxml.xpath("//p[@class = 'Terms']").each do |d|
383
+ h2 = d.at("./preceding-sibling::*[@class = 'TermNum'][1]")
384
+ h2.add_child("&nbsp;")
385
+ h2.add_child(d.remove)
386
+ end
387
+ docxml
388
+ end
389
+ --