asciidoctor-mathml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 30d4154c08818f8383a3b1785f9e12f54ba14471ec4cdd4358d8f0660a1149f1
4
+ data.tar.gz: 8a2ba6d4b7ff57381614fa4f9dd6484d1779f4009b7917b1114c41f687d8d15c
5
+ SHA512:
6
+ metadata.gz: 6a1ca7b78faef47dfe8cdec9a5a71478f678edd0314fca674684aa5b3e38c2ef807c0c870cd3ce5a636cb6af2674fa6138fc348316bfd6d105e7785cd60461a1
7
+ data.tar.gz: ac9c789f2693df2041b9a136d841727307980a68ff6eef1c1ebf6c7168febdb165740c4743401002e9c7f0e3b6826d645e3704f4e13c3908b49daaa864511246
@@ -0,0 +1,93 @@
1
+ require 'asciimath'
2
+
3
+ module Asciidoctor
4
+ module Mathml
5
+ # Inspired by https://github.com/asciidoctor/asciidoctor-mathematical/blob/6316a270ec6e58a2735f626b089372c5c169aac7/lib/asciidoctor-mathematical/extension.rb
6
+ class StemTreeProcessor < Asciidoctor::Extensions::TreeProcessor
7
+ def process(document)
8
+ if document.backend=='html5'
9
+ default_stem = ['asciimath', nil].include?(document.attr('stem')) ? 'asciimath' : document.attr('stem')
10
+
11
+ document.find_by(context: :stem, traverse_documents: true).each do |node|
12
+ handle_asciimath_stem_block(node, default_stem)
13
+ end
14
+
15
+ document.find_by(traverse_documents: true) do |node|
16
+ node.subs && node.subs.include?(:macros)
17
+ end.each do |node|
18
+ handle_prose_block(node, default_stem)
19
+ end
20
+
21
+ document.find_by(context: :section) do |node|
22
+ handle_section_title(node, default_stem)
23
+ end
24
+
25
+ # TODO: Remove the need for the condition by converting latexmath to MathML.
26
+ if default_stem=='asciimath'
27
+ # Ensure that the default html5 backend does not add MathJax.
28
+ document.remove_attr('stem')
29
+ document.instance_variable_get(:@header_attributes).delete('stem')
30
+ end
31
+ end
32
+ end
33
+
34
+ def handle_asciimath_stem_block(node, default_stem)
35
+ if node.attributes[1]=='asciimath' || (node.attributes[1]=='stem' && default_stem=='asciimath')
36
+ node.parent.blocks[node.parent.blocks.index(node)] = create_pass_block(node.parent, <<~HTML, {})
37
+ <div#{node.id ? %Q{ id="#{node.id}"} : ''}#{node.role ? %Q{ class="#{node.role}"} : ''}>
38
+ #{convert_asciimath_to_mathml(node.content)}
39
+ </div>
40
+ HTML
41
+ end
42
+ end
43
+
44
+ def handle_prose_block(node, default_stem)
45
+ if [:list_item, :table_cell].include?(node.context)
46
+ convert_inline_stem(node.instance_variable_get(:@text), default_stem).tap do |converted_text|
47
+ if !converted_text.nil?
48
+ node.text = converted_text
49
+ end
50
+ end
51
+ else
52
+ convert_inline_stem(node.lines.join("\n"), default_stem).tap do |converted_lines|
53
+ if !converted_lines.nil?
54
+ node.lines = converted_lines.split("\n")
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def handle_section_title(node, default_stem)
61
+ convert_inline_stem(node.instance_variable_get(:@title), default_stem).tap do |converted_title|
62
+ if !converted_title.nil?
63
+ node.title = converted_title
64
+ end
65
+ end
66
+ end
67
+
68
+ def convert_inline_stem(text, default_stem)
69
+ if text
70
+ text.gsub(Asciidoctor::InlineStemMacroRx) do
71
+ match = $~
72
+
73
+ if match[0].start_with?('\\') # Do not convert the match if the stem macro is escaped.
74
+ match[0][1..-1]
75
+ elsif match[1]!='asciimath' && (match[1]=='stem' && default_stem!='asciimath') # Do not convert the match if it is not designated as asciimath.
76
+ match[0]
77
+ elsif match[3].rstrip.empty?
78
+ ''
79
+ else
80
+ %Q{pass:[#{convert_asciimath_to_mathml(match[3].gsub('\\]', ']').rstrip).gsub(']', '\\]')}]}
81
+ end
82
+ end
83
+ else
84
+ nil # No conversion required.
85
+ end
86
+ end
87
+
88
+ def convert_asciimath_to_mathml(text)
89
+ AsciiMath.parse(CGI.unescape_html(text)).to_mathml
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ module Asciidoctor
2
+ module Mathml
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'asciidoctor'
2
+
3
+ require_relative 'mathml/stem_tree_processor'
4
+ require_relative 'mathml/version'
5
+
6
+ module Asciidoctor
7
+ module Mathml
8
+ Asciidoctor::Extensions.register do
9
+ treeprocessor Asciidoctor::Mathml::StemTreeProcessor
10
+ end
11
+ end
12
+ end
@@ -0,0 +1 @@
1
+ require_relative 'asciidoctor/mathml'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoctor-mathml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - expeehaa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: asciidoctor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: asciimath
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - expeehaa@outlook.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/asciidoctor-mathml.rb
49
+ - lib/asciidoctor/mathml.rb
50
+ - lib/asciidoctor/mathml/stem_tree_processor.rb
51
+ - lib/asciidoctor/mathml/version.rb
52
+ homepage: https://github.com/expeehaa/asciidoctor-mathml
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ allowed_push_host: https://rubygems.org
57
+ homepage_uri: https://github.com/expeehaa/asciidoctor-mathml
58
+ source_code_uri: https://github.com/expeehaa/asciidoctor-mathml
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.4.10
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: AsciiDoctor extension to convert STEM to MathML
78
+ test_files: []