asciidoctor-mathematical 0.2.2 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1c9eceb9149c84c9d4de3fcd30ec5db93a90ca95
4
- data.tar.gz: 4374551bae0d9848f18cf32dfef01c23dca8cb1a
2
+ SHA256:
3
+ metadata.gz: 437008600aa4b5218559e2572897d96af79213dfccadf5182d21d66211eb457d
4
+ data.tar.gz: 1d3c1222d9a7668176d1b3e3470f26d11d5b50b3e6c2682c44a8c66a5de250d6
5
5
  SHA512:
6
- metadata.gz: 543213a341d317439b96f453ef3ef0233f1d4b4e49f3998d58601bc1e35f02f10e2a7557ce643686de95f352ecfed27f38bcc891c02a1dfe23ebe4de5c11123c
7
- data.tar.gz: 5994fba652c78d3f5a7358f266e5aabfd467e0c402ada7864833ab22cc7353d2311950695189f07f210cb8f2ee8a73fe00c55a4ba2250c79071d6039979b479f
6
+ metadata.gz: e75cc6ce59b6ae680333e7ea97e3cc45b46f735fde6d6f3ca0eb0bb9489e8f74fe78c48872e3fc4f56aacc0c8d73e2b8325042b8e40389163b66ee125f975ccb
7
+ data.tar.gz: 433c46fe8a04eef3792c522a660b8fc733b036a514ade4f8a52e12152fb4f54d4b75e6749d1a9e59c3489ddaab7df6ff2998509929187dd1426931ff87b69279
@@ -10,107 +10,173 @@ class MathematicalTreeprocessor < Asciidoctor::Extensions::Treeprocessor
10
10
  LatexmathInlineMacroRx = /\\?latexmath:([a-z,]*)\[(.*?[^\\])\]/m
11
11
 
12
12
  def process document
13
- to_html = document.basebackend? 'html'
14
13
  format = ((document.attr 'mathematical-format') || 'png').to_sym
15
14
  if format != :png and format != :svg
16
15
  warn %(Unknown format '#{format}', retreat to 'png')
17
16
  format = :png
18
17
  end
19
- image_ext = %(.#{format})
20
18
  ppi = ((document.attr 'mathematical-ppi') || '300.0').to_f
21
19
  ppi = format == :png ? ppi : 72.0
20
+ inline = document.attr 'mathematical-inline'
21
+ if inline and format == :png
22
+ warn 'Can\'t use mathematical-inline together with mathematical-format=png'
23
+ end
22
24
  # The no-args constructor defaults to SVG and standard delimiters ($..$ for inline, $$..$$ for block)
23
25
  mathematical = ::Mathematical.new format: format, ppi: ppi
24
- image_output_dir, image_target_dir = image_output_and_target_dir document
25
-
26
- unless (stem_blocks = document.find_by context: :stem).nil_or_empty?
26
+ unless inline
27
+ image_output_dir, image_target_dir = image_output_and_target_dir document
27
28
  ::Asciidoctor::Helpers.mkdir_p image_output_dir unless ::File.directory? image_output_dir
29
+ end
28
30
 
31
+ unless (stem_blocks = document.find_by context: :stem).nil_or_empty?
29
32
  stem_blocks.each do |stem|
30
- equation_type = stem.style.to_sym
31
- next unless equation_type == :latexmath
32
- equation_data = %($$#{stem.content}$$)
33
+ handle_stem_block stem, mathematical, image_output_dir, image_target_dir, format, inline
34
+ end
35
+ end
36
+
37
+ unless (prose_blocks = document.find_by {|b|
38
+ (b.content_model == :simple && (b.subs.include? :macros)) || b.context == :list_item
39
+ }).nil_or_empty?
40
+ prose_blocks.each do |prose|
41
+ handle_prose_block prose, mathematical, image_output_dir, image_target_dir, format, inline
42
+ end
43
+ end
33
44
 
34
- # FIXME auto-generate id if one is not provided
35
- unless (stem_id = stem.id)
36
- stem_id = %(stem-#{::Digest::MD5.hexdigest stem.content})
45
+ # handle table cells of the "asciidoc" type, as suggested by mojavelinux
46
+ # at asciidoctor/asciidoctor-mathematical#20.
47
+ unless (table_blocks = document.find_by context: :table).nil_or_empty?
48
+ table_blocks.each do |table|
49
+ (table.rows[:body] + table.rows[:foot]).each do |row|
50
+ row.each do |cell|
51
+ if cell.style == :asciidoc
52
+ process cell.inner_document
53
+ elsif cell.style != :literal
54
+ handle_nonasciidoc_table_cell cell, mathematical, image_output_dir, image_target_dir, format, inline
55
+ end
56
+ end
37
57
  end
58
+ end
59
+ end
38
60
 
39
- alt_text = stem.attr 'alt', equation_data
61
+ unless (sect_blocks = document.find_by content: :section).nil_or_empty?
62
+ sect_blocks.each do |sect|
63
+ handle_section_title sect, mathematical, image_output_dir, image_target_dir, format, inline
64
+ end
65
+ end
40
66
 
41
- image_target = %(#{stem_id}#{image_ext})
42
- image_file = ::File.join image_output_dir, image_target
43
- image_target = ::File.join image_target_dir, image_target unless image_target_dir == '.'
67
+ nil
68
+ end
44
69
 
45
- # TODO check for error
46
- result = mathematical.parse equation_data
47
- ::IO.write image_file, result[:data]
70
+ def handle_stem_block(stem, mathematical, image_output_dir, image_target_dir, format, inline)
71
+ equation_type = stem.style.to_sym
72
+ return unless equation_type == :latexmath
48
73
 
49
- attrs = { 'target' => image_target, 'alt' => alt_text, 'align' => 'center' }
50
- if format == :png
51
- attrs['width'] = %(#{result[:width]})
52
- attrs['height'] = %(#{result[:height]})
53
- end
54
- parent = stem.parent
55
- stem_image = create_image_block parent, attrs
56
- stem_image.id = stem.id if stem.id
57
- if (title = stem.attributes['title'])
58
- stem_image.title = title
59
- end
60
- parent.blocks[parent.blocks.index stem] = stem_image
74
+ img_target, img_width, img_height = make_equ_image stem.content, stem.id, false, mathematical, image_output_dir, image_target_dir, format, inline
75
+
76
+ parent = stem.parent
77
+ if inline
78
+ stem_image = create_pass_block parent, %{<div class="stemblock"> #{img_target} </div>}, {}
79
+ parent.blocks[parent.blocks.index stem] = stem_image
80
+ else
81
+ alt_text = stem.attr 'alt', %($$#{stem.content}$$)
82
+ attrs = {'target' => img_target, 'alt' => alt_text, 'align' => 'center'}
83
+ # NOTE: The following setups the *intended width and height in pixel* for png images, which can be different that that of the generated image when PPIs larger than 72.0 is used.
84
+ if format == :png
85
+ attrs['width'] = %(#{img_width})
86
+ attrs['height'] = %(#{img_height})
61
87
  end
88
+ parent = stem.parent
89
+ stem_image = create_image_block parent, attrs
90
+ stem_image.id = stem.id if stem.id
91
+ if (title = stem.attributes['title'])
92
+ stem_image.title = title
93
+ end
94
+ parent.blocks[parent.blocks.index stem] = stem_image
62
95
  end
96
+ end
63
97
 
64
- unless (prose_blocks = document.find_by {|b|
65
- (b.content_model == :simple && (b.subs.include? :macros)) || b.context == :list_item
66
- }).nil_or_empty?
67
- support_stem_prefix = document.attr? 'stem', 'latexmath'
68
- ::Asciidoctor::Helpers.mkdir_p image_output_dir unless ::File.directory? image_output_dir
69
- stem_rx = support_stem_prefix ? StemInlineMacroRx : LatexmathInlineMacroRx
70
-
71
- prose_blocks.each do |block|
72
- source_modified = false
73
- source = block.context == :list_item ? (block.instance_variable_get :@text) : (block.lines * LineFeed)
74
- # TODO skip passthroughs in the source (e.g., +stem:[x^2]+)
75
- source.gsub!(stem_rx) {
76
- if (m = $~)[0].start_with? '\\'
77
- next m[0][1..-1]
78
- end
98
+ def handle_prose_block(prose, mathematical, image_output_dir, image_target_dir, format, inline)
99
+ text = prose.context == :list_item ? (prose.instance_variable_get :@text) : (prose.lines * LineFeed)
100
+ text, source_modified = handle_inline_stem prose, text, mathematical, image_output_dir, image_target_dir, format, inline
101
+ if source_modified
102
+ if prose.context == :list_item
103
+ prose.instance_variable_set :@text, text
104
+ else
105
+ prose.lines = text.split LineFeed
106
+ end
107
+ end
108
+ end
79
109
 
80
- if (eq_data = m[2].rstrip).empty?
81
- next
82
- else
83
- source_modified = true
84
- end
110
+ def handle_nonasciidoc_table_cell(cell, mathematical, image_output_dir, image_target_dir, format, inline)
111
+ text = cell.instance_variable_get :@text
112
+ text, source_modified = handle_inline_stem cell, text, mathematical, image_output_dir, image_target_dir, format, inline
113
+ if source_modified
114
+ cell.instance_variable_set :@text, text
115
+ end
116
+ end
117
+
118
+ def handle_section_title(sect, mathematical, image_output_dir, image_target_dir, format, inline)
119
+ text = sect.instance_variable_get :@title
120
+ text, source_modified = handle_inline_stem sect, text, mathematical, image_output_dir, image_target_dir, format, inline
121
+ if source_modified
122
+ sect.instance_variable_set :@title, text
123
+ sect.remove_instance_variable :@subbed_title
124
+ end
125
+ end
85
126
 
86
- eq_data.gsub! '\]', ']'
87
- subs = m[1].nil_or_empty? ? (to_html ? [:specialcharacters] : []) : (block.resolve_pass_subs m[1])
88
- eq_data = block.apply_subs eq_data, subs unless subs.empty?
127
+ def handle_inline_stem(node, text, mathematical, image_output_dir, image_target_dir, format, inline)
128
+ document = node.document
129
+ to_html = document.basebackend? 'html'
130
+ support_stem_prefix = document.attr? 'stem', 'latexmath'
131
+ stem_rx = support_stem_prefix ? StemInlineMacroRx : LatexmathInlineMacroRx
132
+
133
+ source_modified = false
134
+ # TODO skip passthroughs in the source (e.g., +stem:[x^2]+)
135
+ text.gsub!(stem_rx) {
136
+ if (m = $~)[0].start_with? '\\'
137
+ next m[0][1..-1]
138
+ end
89
139
 
90
- eq_id = %(stem-#{::Digest::MD5.hexdigest eq_data})
91
- eq_input = %($#{eq_data}$)
140
+ if (eq_data = m[2].rstrip).empty?
141
+ next
142
+ else
143
+ source_modified = true
144
+ end
92
145
 
93
- img_target = %(#{eq_id}#{image_ext})
94
- img_file = ::File.join image_output_dir, img_target
95
- img_target = ::File.join image_target_dir, img_target unless image_target_dir == '.'
146
+ eq_data.gsub! '\]', ']'
147
+ subs = m[1].nil_or_empty? ? (to_html ? [:specialcharacters] : []) : (node.resolve_pass_subs m[1])
148
+ eq_data = node.apply_subs eq_data, subs unless subs.empty?
149
+ img_target, img_width, img_height = make_equ_image eq_data, nil, true, mathematical, image_output_dir, image_target_dir, format, inline
150
+ if inline
151
+ %(pass:[<span class="steminline"> #{img_target} </span>])
152
+ else
153
+ %(image:#{img_target}[width=#{img_width},height=#{img_height}])
154
+ end
155
+ } if (text != nil) && (text.include? ':') && ((support_stem_prefix && (text.include? 'stem:')) || (text.include? 'latexmath:'))
96
156
 
97
- eq_result = mathematical.parse eq_input
157
+ [text, source_modified]
158
+ end
98
159
 
99
- ::IO.write img_file, eq_result[:data]
100
- %(image:#{img_target}[width=#{eq_result[:width]},height=#{eq_result[:height]}])
101
- } if (source != nil) && (source.include? ':') && ((support_stem_prefix && (source.include? 'stem:')) || (source.include? 'latexmath:'))
160
+ def make_equ_image(equ_data, equ_id, equ_inline, mathematical, image_output_dir, image_target_dir, format, inline)
161
+ input = equ_inline ? %($#{equ_data}$) : %($$#{equ_data}$$)
102
162
 
103
- if source_modified
104
- if block.context == :list_item
105
- block.instance_variable_set :@text, source
106
- else
107
- block.lines = source.split LineFeed
108
- end
109
- end
163
+ # TODO: Handle exceptions.
164
+ result = mathematical.parse input
165
+ if inline
166
+ result[:data]
167
+ else
168
+ unless equ_id
169
+ equ_id = %(stem-#{::Digest::MD5.hexdigest input})
110
170
  end
111
- end
171
+ image_ext = %(.#{format})
172
+ img_target = %(#{equ_id}#{image_ext})
173
+ img_file = ::File.join image_output_dir, img_target
112
174
 
113
- nil
175
+ ::IO.write img_file, result[:data]
176
+
177
+ img_target = ::File.join image_target_dir, img_target unless image_target_dir == '.'
178
+ [img_target, result[:width], result[:height]]
179
+ end
114
180
  end
115
181
 
116
182
  def image_output_and_target_dir(parent)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-mathematical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Stumm
@@ -50,22 +50,36 @@ dependencies:
50
50
  name: asciidoctor
51
51
  requirement: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
54
- - !ruby/object:Gem::Version
55
- version: '1.5'
56
53
  - - ">="
57
54
  - !ruby/object:Gem::Version
58
- version: 1.5.0
55
+ version: 2.0.0
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '2.0'
59
59
  type: :runtime
60
60
  prerelease: false
61
61
  version_requirements: !ruby/object:Gem::Requirement
62
62
  requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.0.0
63
66
  - - "~>"
64
67
  - !ruby/object:Gem::Version
65
- version: '1.5'
66
- - - ">="
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 12.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: 1.5.0
82
+ version: 12.3.0
69
83
  description: An Asciidoctor extension to converts latexmath equations to SVG or PNGs
70
84
  email: tstumm@users.noreply.github.com
71
85
  executables: []
@@ -93,8 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
107
  - !ruby/object:Gem::Version
94
108
  version: '0'
95
109
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.5.1
110
+ rubygems_version: 3.0.3
98
111
  signing_key:
99
112
  specification_version: 4
100
113
  summary: Asciidoctor STEM processor based on Mathematical