asciidoctor-mathematical 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7314ce3eb4a07303f58434c05e7b40d00b532ecb
4
+ data.tar.gz: 769f69516c2d5a113ad67cbbd8720098f860c00f
5
+ SHA512:
6
+ metadata.gz: baa9e15e5cdaeda7b3048470cd813c7647825ca35adb8816d9691b64743b357fc7e8b36017febefc147aa45ad3c0efb48751c1fb2c5964b7c5651891321c144b
7
+ data.tar.gz: fbb01d534e647733b86b756f45d6598013c28560635a7d9aaa1cc26f46ced269687476d6053a618f63d9eb7a8494a07f18ee6ae1d08741801de07aee3c22389e
@@ -0,0 +1,6 @@
1
+ RUBY_ENGINE == 'opal' ? (require 'asciidoctor-mathematical/extension') : (require_relative 'asciidoctor-mathematical/extension')
2
+
3
+ Extensions.register do
4
+ preprocessor MathematicalPreprocessor
5
+ treeprocessor MathematicalTreeprocessor
6
+ end
@@ -0,0 +1,161 @@
1
+ require 'asciidoctor'
2
+ require 'asciidoctor/extensions'
3
+ require 'mathematical'
4
+
5
+ autoload :Digest, 'digest'
6
+
7
+ include ::Asciidoctor
8
+
9
+ class MathematicalTreeprocessor < Extensions::Treeprocessor
10
+ def process document
11
+ if (stem_blocks = document.find_by context: :stem)
12
+ format = :png
13
+ if document.attributes['mathematical-format']
14
+ format_str = document.attributes['mathematical-format']
15
+ if format_str == 'png'
16
+ format = :png
17
+ elsif format_str == 'svg'
18
+ format = :svg
19
+ end
20
+ end
21
+ image_postfix = ".#{format}"
22
+ scale = 1.0
23
+ if format == :png
24
+ scale = 72.0/300.0
25
+ end
26
+ ppi = 72.0
27
+ if format == :png
28
+ ppi = 300.0
29
+ end
30
+ # The no-args constructor defaults to SVG and standard delimiters ($..$ for inline, $$..$$ for block)
31
+ mathematical = ::Mathematical.new({ :format => format, :ppi => ppi })
32
+ image_output_dir = resolve_image_output_dir document
33
+ image_target_dir = document.attr 'imagesoutdir', (document.attr 'imagesdir')
34
+ image_target_dir = '.' if image_target_dir.nil_or_empty?
35
+ ::FileUtils.mkdir_p image_output_dir unless ::File.directory? image_output_dir
36
+
37
+ stem_blocks.each do |stem|
38
+ equation_data = %($$#{stem.content}$$)
39
+ equation_type = stem.style.to_sym
40
+ next unless equation_type == :latexmath
41
+
42
+ # FIXME auto-generate id if one is not provided
43
+ unless (stem_id = stem.id)
44
+ stem_id = %(stem-#{::Digest::MD5.hexdigest stem.content})
45
+ end
46
+
47
+ alt_text = stem.attr 'alt', equation_data
48
+
49
+ image_target = %(#{stem_id}#{image_postfix})
50
+ image_file = ::File.join image_output_dir, image_target
51
+ image_target = ::File.join image_target_dir, image_target unless image_target_dir == '.'
52
+
53
+ # TODO check for error
54
+ result = mathematical.parse equation_data
55
+ ::IO.write image_file, result[:data]
56
+
57
+ attrs = { 'target' => image_target, 'alt' => alt_text, 'align' => 'center' }
58
+ if format == :png
59
+ attrs['width'] = "#{result[:width]}pt"
60
+ attrs['height'] = "#{result[:height]}pt"
61
+ end
62
+ parent = stem.parent
63
+ stem_image = create_image_block parent, attrs
64
+ stem_image.id = stem.id if stem.id
65
+ if (title = stem.attributes['title'])
66
+ stem_image.title = title
67
+ end
68
+ parent.blocks[parent.blocks.index stem] = stem_image
69
+ end
70
+ end
71
+ nil
72
+ end
73
+
74
+ def resolve_image_output_dir doc
75
+ if (images_dir = doc.attr 'imagesoutdir')
76
+ base_dir = nil
77
+ else
78
+ base_dir = (doc.attr 'outdir') || ((doc.respond_to? :options) && doc.options[:to_dir])
79
+ images_dir = doc.attr 'imagesdir'
80
+ end
81
+
82
+ doc.normalize_system_path images_dir, base_dir
83
+ end
84
+ end
85
+
86
+ class MathematicalPreprocessor < Extensions::Preprocessor
87
+ LATEXMATH_PTN = /latexmath:\[([^\]]+)\]/
88
+
89
+ SPECIAL_CHARS = {
90
+ '&' => '&amp;',
91
+ '<' => '&lt;',
92
+ '>' => '&gt;'
93
+ }
94
+
95
+ SPECIAL_CHARS_PATTERN = /[#{SPECIAL_CHARS.keys.join}]/
96
+
97
+ # FIXME: I don't understand why I can not simply use sub_specialchars from
98
+ # Asciidoctor. It just crashes with 'unknown method name'. So I copy the
99
+ # code here.
100
+ def sub_specialchars text
101
+ text.gsub(SPECIAL_CHARS_PATTERN, SPECIAL_CHARS)
102
+ end
103
+
104
+ def process document, reader
105
+ # Setup image format information
106
+ format = :png
107
+ if document.attributes['mathematical-format']
108
+ format_str = document.attributes['mathematical-format']
109
+ if format_str == 'png'
110
+ format = :png
111
+ elsif format_str == 'svg'
112
+ format = :svg
113
+ end
114
+ end
115
+ image_postfix = ".#{format}"
116
+ scale = 1.0
117
+ if format == :png
118
+ scale = 72.0/300.0
119
+ end
120
+ ppi = 72.0
121
+ if format == :png
122
+ ppi = 300.0
123
+ end
124
+
125
+ # Since at preprocessing stage, we have no document attribute avaliable,
126
+ # so fix the image output dir to be simple.
127
+ image_output_dir = './images'
128
+ image_target_dir = './images'
129
+ ::FileUtils.mkdir_p image_output_dir unless ::File.directory? image_output_dir
130
+
131
+ mathematical = ::Mathematical.new({ :format => format, :ppi => ppi })
132
+
133
+ lines = reader.readlines
134
+ lines.each do |line|
135
+ md = LATEXMATH_PTN.match line
136
+ while md
137
+ stem_content = md[1]
138
+ # NOTE: It seems that we need to escape '<>&' to make mathematical
139
+ # work. This is weired but verified. So we escape them here.
140
+ stem_content = sub_specialchars stem_content
141
+ equation_data = %($#{stem_content}$)
142
+ stem_id = %(stem-#{::Digest::MD5.hexdigest stem_content})
143
+
144
+ image_target = %(#{stem_id}#{image_postfix})
145
+ image_file = ::File.join image_output_dir, image_target
146
+ image_target = ::File.join image_target_dir, image_target unless image_target_dir == '.'
147
+
148
+ # TODO check for error
149
+ result = mathematical.parse equation_data
150
+ ::IO.write image_file, result[:data]
151
+
152
+ subst = %(image:#{image_target}[width=#{result[:width]}pt])
153
+ line.gsub! md[0], subst
154
+ md = LATEXMATH_PTN.match md.post_match
155
+ end
156
+ end
157
+
158
+ reader.unshift_lines lines
159
+ reader
160
+ end
161
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoctor-mathematical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Stumm
8
+ - Zhang Yang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-09-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-enum
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.4'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: mathematical
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.5'
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 1.5.8
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "~>"
43
+ - !ruby/object:Gem::Version
44
+ version: '1.5'
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.8
48
+ - !ruby/object:Gem::Dependency
49
+ name: asciidoctor
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 1.5.0
58
+ type: :runtime
59
+ prerelease: false
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '1.5'
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 1.5.0
68
+ description: Converts latexmath equations in Asciidoctor to SVG
69
+ email: tstumm@users.noreply.github.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - lib/asciidoctor-mathematical.rb
75
+ - lib/asciidoctor-mathematical/extension.rb
76
+ homepage: https://github.com/tstumm/asciidoctor-mathematical
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Wrapper for Mathematical
100
+ test_files: []