asciidoctor-csand 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,174 @@
1
+ require "asciidoctor"
2
+ require "asciidoctor/csand/version"
3
+ require "asciidoctor/csand/csandconvert"
4
+ require "asciidoctor/iso/converter"
5
+
6
+ module Asciidoctor
7
+ module Csand
8
+ CSAND_NAMESPACE = "https://open.ribose.com/standards/csand"
9
+
10
+ # A {Converter} implementation that generates CSAND output, and a document
11
+ # schema encapsulation of the document for validation
12
+ class Converter < ISO::Converter
13
+
14
+ register_for "csand"
15
+
16
+ def metadata_author(node, xml)
17
+ xml.contributor do |c|
18
+ c.role **{ type: "author" }
19
+ c.organization do |a|
20
+ a.name "Ribose"
21
+ end
22
+ end
23
+ end
24
+
25
+ def metadata_publisher(node, xml)
26
+ xml.contributor do |c|
27
+ c.role **{ type: "publisher" }
28
+ c.organization do |a|
29
+ a.name "Ribose"
30
+ end
31
+ end
32
+ end
33
+
34
+ def metadata_committee(node, xml)
35
+ xml.editorialgroup do |a|
36
+ a.technical_committee node.attr("technical-committee"),
37
+ **attr_code(type: node.attr("technical-committee-type"))
38
+ end
39
+ end
40
+
41
+ def title(node, xml)
42
+ ["en"].each do |lang|
43
+ xml.title **{ language: lang, format: "plain" } do |t|
44
+ t << asciidoc_sub(node.attr("title"))
45
+ end
46
+ end
47
+ end
48
+
49
+ def metadata_status(node, xml)
50
+ xml.status **{ format: "plain" } { |s| s << node.attr("status") }
51
+ end
52
+
53
+ def metadata_id(node, xml)
54
+ xml.docidentifier { |i| i << node.attr("docnumber") }
55
+ end
56
+
57
+ def metadata_copyright(node, xml)
58
+ from = node.attr("copyright-year") || Date.today.year
59
+ xml.copyright do |c|
60
+ c.from from
61
+ c.owner do |owner|
62
+ owner.organization do |o|
63
+ o.name "Ribose"
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ def title_validate(root)
70
+ nil
71
+ end
72
+
73
+ def makexml(node)
74
+ result = ["<?xml version='1.0' encoding='UTF-8'?>\n<csand-standard>"]
75
+ @draft = node.attributes.has_key?("draft")
76
+ result << noko { |ixml| front node, ixml }
77
+ result << noko { |ixml| middle node, ixml }
78
+ result << "</csand-standard>"
79
+ result = textcleanup(result.flatten * "\n")
80
+ ret1 = cleanup(Nokogiri::XML(result))
81
+ validate(ret1)
82
+ ret1.root.add_namespace(nil, CSAND_NAMESPACE)
83
+ ret1
84
+ end
85
+
86
+ def document(node)
87
+ init(node)
88
+ ret1 = makexml(node)
89
+ ret = ret1.to_xml(indent: 2)
90
+ filename = node.attr("docfile").gsub(/\.adoc/, ".xml").
91
+ gsub(%r{^.*/}, "")
92
+ File.open(filename, "w") { |f| f.write(ret) }
93
+ html_converter(node).convert filename unless node.attr("nodoc")
94
+ @files_to_delete.each { |f| system "rm #{f}" }
95
+ ret
96
+ end
97
+
98
+ def validate(doc)
99
+ content_validate(doc)
100
+ schema_validate(formattedstr_strip(doc.dup),
101
+ File.join(File.dirname(__FILE__), "csand.rng"))
102
+ end
103
+
104
+ def html_doc_path(file)
105
+ File.join(File.dirname(__FILE__), File.join("html", file))
106
+ end
107
+
108
+ def literal(node)
109
+ noko do |xml|
110
+ xml.figure **id_attr(node) do |f|
111
+ figure_title(node, f)
112
+ f.pre node.lines.join("\n")
113
+ end
114
+ end
115
+ end
116
+
117
+ def sections_cleanup(x)
118
+ super
119
+ x.xpath("//*[@inline-header]").each do |h|
120
+ h.delete("inline-header")
121
+ end
122
+ end
123
+
124
+ def style(n, t)
125
+ return
126
+ end
127
+
128
+ def html_converter(_node)
129
+ CsandConvert.new(
130
+ htmlstylesheet: generate_css(html_doc_path("htmlstyle.scss"), true),
131
+ standardstylesheet: generate_css(html_doc_path("csand.scss"), true),
132
+ htmlcoverpage: html_doc_path("html_csand_titlepage.html"),
133
+ htmlintropage: html_doc_path("html_csand_intro.html"),
134
+ scripts: html_doc_path("scripts.html"),
135
+ )
136
+ end
137
+
138
+ def default_fonts(node)
139
+ b = node.attr("body-font") ||
140
+ (node.attr("script") == "Hans" ? '"SimSun",serif' :
141
+ '"Overpass",sans-serif')
142
+ h = node.attr("header-font") ||
143
+ (node.attr("script") == "Hans" ? '"SimHei",sans-serif' :
144
+ '"Overpass",sans-serif')
145
+ m = node.attr("monospace-font") || '"Space Mono",monospace'
146
+ "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
147
+ end
148
+
149
+ def inline_quoted(node)
150
+ noko do |xml|
151
+ case node.type
152
+ when :emphasis then xml.em node.text
153
+ when :strong then xml.strong node.text
154
+ when :monospaced then xml.tt node.text
155
+ when :double then xml << "\"#{node.text}\""
156
+ when :single then xml << "'#{node.text}'"
157
+ when :superscript then xml.sup node.text
158
+ when :subscript then xml.sub node.text
159
+ when :asciimath then stem_parse(node.text, xml)
160
+ else
161
+ case node.role
162
+ when "strike" then xml.strike node.text
163
+ when "smallcap" then xml.smallcap node.text
164
+ when "keyword" then xml.keyword node.text
165
+ else
166
+ xml << node.text
167
+ end
168
+ end
169
+ end.join
170
+ end
171
+
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,134 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
3
+ <!--
4
+ default namespace = "http://riboseinc.com/csand"
5
+ Currently we inherit from a namespaced grammar, isostandard. Until we inherit from isodoc,
6
+ we cannot have a new default namespace: we will end up with a grammar with two different
7
+ namespaces, one for isostandard and one for csand additions. And we do not want that.
8
+ -->
9
+ <include href="isostandard.rng">
10
+ <start>
11
+ <ref name="csand-standard"/>
12
+ </start>
13
+ <define name="language">
14
+ <element name="language">
15
+ <value>en</value>
16
+ </element>
17
+ </define>
18
+ <define name="docidentifier">
19
+ <element name="docidentifier">
20
+ <text/>
21
+ </element>
22
+ </define>
23
+ <define name="btitle">
24
+ <element name="title">
25
+ <ref name="FormattedString"/>
26
+ </element>
27
+ </define>
28
+ <define name="status">
29
+ <element name="status">
30
+ <choice>
31
+ <value>proposal</value>
32
+ <value>working-draft</value>
33
+ <value>committee-draft</value>
34
+ <value>draft-standard</value>
35
+ <value>final-draft</value>
36
+ <value>published</value>
37
+ <value>withdrawn</value>
38
+ </choice>
39
+ </element>
40
+ </define>
41
+ <define name="figure">
42
+ <element name="figure">
43
+ <attribute name="id">
44
+ <data type="ID"/>
45
+ </attribute>
46
+ <optional>
47
+ <ref name="source"/>
48
+ </optional>
49
+ <optional>
50
+ <ref name="tname"/>
51
+ </optional>
52
+ <choice>
53
+ <oneOrMore>
54
+ <ref name="figure"/>
55
+ </oneOrMore>
56
+ <group>
57
+ <choice>
58
+ <zeroOrMore>
59
+ <ref name="TextElement"/>
60
+ </zeroOrMore>
61
+ <ref name="pre"/>
62
+ </choice>
63
+ <zeroOrMore>
64
+ <ref name="note"/>
65
+ </zeroOrMore>
66
+ <optional>
67
+ <ref name="dl"/>
68
+ </optional>
69
+ </group>
70
+ </choice>
71
+ </element>
72
+ </define>
73
+ <!-- TextElement |= keyword -->
74
+ <define name="TextElement">
75
+ <choice>
76
+ <text/>
77
+ <ref name="em"/>
78
+ <ref name="eref"/>
79
+ <ref name="strong"/>
80
+ <ref name="stem"/>
81
+ <ref name="sub"/>
82
+ <ref name="sup"/>
83
+ <ref name="tt"/>
84
+ <ref name="strike"/>
85
+ <ref name="smallcap"/>
86
+ <ref name="xref"/>
87
+ <ref name="br"/>
88
+ <ref name="hyperlink"/>
89
+ <ref name="hr"/>
90
+ <ref name="pagebreak"/>
91
+ <ref name="bookmark"/>
92
+ <ref name="keyword"/>
93
+ </choice>
94
+ </define>
95
+ </include>
96
+ <define name="BibItemType" combine="choice">
97
+ <choice>
98
+ <value>code</value>
99
+ <value>presentation</value>
100
+ <value>proposal</value>
101
+ <value>standard</value>
102
+ <value>report</value>
103
+ </choice>
104
+ </define>
105
+ <define name="pre">
106
+ <element name="pre">
107
+ <text/>
108
+ </element>
109
+ </define>
110
+ <define name="keyword">
111
+ <element name="keyword">
112
+ <text/>
113
+ </element>
114
+ </define>
115
+ <define name="csand-standard">
116
+ <element name="csand-standard">
117
+ <ref name="bibdata"/>
118
+ <optional>
119
+ <ref name="version"/>
120
+ </optional>
121
+ <zeroOrMore>
122
+ <ref name="termdocsource"/>
123
+ </zeroOrMore>
124
+ <ref name="preface"/>
125
+ <oneOrMore>
126
+ <ref name="sections"/>
127
+ </oneOrMore>
128
+ <zeroOrMore>
129
+ <ref name="annex"/>
130
+ </zeroOrMore>
131
+ <ref name="bibliography"/>
132
+ </element>
133
+ </define>
134
+ </grammar>
@@ -0,0 +1,135 @@
1
+ require "isodoc"
2
+
3
+ module Asciidoctor
4
+ module Csand
5
+ # A {Converter} implementation that generates CSAND output, and a document
6
+ # schema encapsulation of the document for validation
7
+ class CsandConvert < IsoDoc::Convert
8
+ def initialize(options)
9
+ super
10
+ set_metadata(:status, "XXX")
11
+ end
12
+
13
+ def init_metadata
14
+ super
15
+ end
16
+
17
+ def title(isoxml, _out)
18
+ main = isoxml&.at(ns("//title[@language='en']"))&.text
19
+ set_metadata(:doctitle, main)
20
+ end
21
+
22
+ def subtitle(_isoxml, _out)
23
+ nil
24
+ end
25
+
26
+ def author(isoxml, _out)
27
+ set_metadata(:tc, "XXXX")
28
+ tc = isoxml.at(ns("//editorialgroup/technical-committee"))
29
+ set_metadata(:tc, tc.text) if tc
30
+ end
31
+
32
+
33
+ def docid(isoxml, _out)
34
+ docnumber = isoxml.at(ns("//bibdata/docidentifier"))
35
+ docstatus = isoxml.at(ns("//bibdata/status"))
36
+ dn = docnumber&.text
37
+ if docstatus
38
+ set_metadata(:status, status_print(docstatus.text))
39
+ abbr = status_abbr(docstatus.text)
40
+ dn = "#{dn}(#{abbr})" unless abbr.empty?
41
+ end
42
+ set_metadata(:docnumber, dn)
43
+ end
44
+
45
+ def status_print(status)
46
+ status.split(/-/).map{ |w| w.capitalize }.join(" ")
47
+ end
48
+
49
+ def status_abbr(status)
50
+ case status
51
+ when "working-draft" then "wd"
52
+ when "committee-draft" then "cd"
53
+ when "draft-standard" then "d"
54
+ else
55
+ ""
56
+ end
57
+ end
58
+
59
+ def pre_parse(node, out)
60
+ out.pre node.text # content.gsub(/</, "&lt;").gsub(/>/, "&gt;")
61
+ end
62
+
63
+ def term_defs_boilerplate(div, source, term)
64
+ if source.empty? && term.nil?
65
+ div << @no_terms_boilerplate
66
+ else
67
+ div << term_defs_boilerplate_cont(source, term)
68
+ end
69
+ end
70
+
71
+ def i18n_init(lang, script)
72
+ super
73
+ @annex_lbl = "Appendix"
74
+ end
75
+
76
+ def error_parse(node, out)
77
+ # catch elements not defined in ISO
78
+ case node.name
79
+ when "pre"
80
+ pre_parse(node, out)
81
+ when "keyword"
82
+ out.span node.text, **{ class: "keyword" }
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ HEAD = <<~HEAD.freeze
89
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
90
+
91
+ <!--TOC script import-->
92
+ <script type="text/javascript" src="https://cdn.rawgit.com/jgallen23/toc/0.3.2/dist/toc.min.js"></script>
93
+
94
+ <!--Google fonts-->
95
+ <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i|Space+Mono:400,700" rel="stylesheet">
96
+ <link href="https://fonts.googleapis.com/css?family=Overpass:300,300i,600,900" rel="stylesheet">
97
+ <!--Font awesome import for the link icon-->
98
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/solid.css" integrity="sha384-v2Tw72dyUXeU3y4aM2Y0tBJQkGfplr39mxZqlTBDUZAb9BGoC40+rdFCG0m10lXk" crossorigin="anonymous">
99
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/fontawesome.css" integrity="sha384-q3jl8XQu1OpdLgGFvNRnPdj5VIlCvgsDQTQB6owSOHWlAurxul7f+JpUOVdAiJ5P" crossorigin="anonymous">
100
+ HEAD
101
+
102
+ BUTTON = '<button onclick="topFunction()" id="myBtn" '\
103
+ 'title="Go to top">Top</button>'.freeze
104
+
105
+
106
+ def html_main(docxml)
107
+ d = docxml.at('//div[@class="WordSection3"]')
108
+ s = d.replace("<main></main>")
109
+ s.first.children = d
110
+ s.first.children.first.previous = BUTTON
111
+ end
112
+
113
+ def html_preface(docxml)
114
+ super
115
+ docxml.at("//head").add_child(HEAD)
116
+ html_main(docxml)
117
+ docxml
118
+ end
119
+
120
+ def make_body(xml, docxml)
121
+ body_attr = { lang: "EN-US", link: "blue", vlink: "#954F72", "xml:lang": "EN-US", class: "container" }
122
+ xml.body **body_attr do |body|
123
+ make_body1(body, docxml)
124
+ make_body2(body, docxml)
125
+ make_body3(body, docxml)
126
+ end
127
+ end
128
+
129
+ def html_toc(docxml)
130
+ docxml
131
+ end
132
+ end
133
+ end
134
+ end
135
+