Skip to content

Commit

Permalink
Merge pull request #1829 from kn1cht/imgmath-for-idgxml
Browse files Browse the repository at this point in the history
Implement imgmath for IDGXML maker
  • Loading branch information
kmuto authored Jul 10, 2022
2 parents fbcd983 + b728924 commit caf998d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
30 changes: 24 additions & 6 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,22 @@ def texequation(lines, id = nil, caption = '')
puts caption_str if caption_top?('equation')
end

puts %Q(<replace idref="texblock-#{@texblockequation}">)
puts '<pre>'
print lines.join("\n")
puts '</pre>'
puts '</replace>'
if @book.config['math_format'] == 'imgmath'
fontsize = @book.config['imgmath_options']['fontsize'].to_f
lineheight = @book.config['imgmath_options']['lineheight'].to_f
math_str = "\\begin{equation*}\n\\fontsize{#{fontsize}}{#{lineheight}}\\selectfont\n#{lines.join("\n")}\n\\end{equation*}\n"
key = Digest::SHA256.hexdigest(math_str)
img_path = @img_math.defer_math_image(math_str, key)
puts '<equationimage>'
puts %Q(<Image href="file://#{img_path}" />)
puts '</equationimage>'
else
puts %Q(<replace idref="texblock-#{@texblockequation}">)
puts '<pre>'
puts lines.join("\n")
puts '</pre>'
puts '</replace>'
end

if id
puts caption_str unless caption_top?('equation')
Expand Down Expand Up @@ -835,7 +846,14 @@ def inline_uchar(str)

def inline_m(str)
@texinlineequation += 1
%Q(<replace idref="texinline-#{@texinlineequation}"><pre>#{escape(str)}</pre></replace>)
if @book.config['math_format'] == 'imgmath'
math_str = '$' + str + '$'
key = Digest::SHA256.hexdigest(str)
img_path = @img_math.defer_math_image(math_str, key)
%Q(<inlineequation><Image href="file://#{img_path}" type="inline" /></inlineequation>)
else
%Q(<replace idref="texinline-#{@texinlineequation}"><pre>#{escape(str)}</pre></replace>)
end
end

def noindent
Expand Down
12 changes: 11 additions & 1 deletion lib/review/idgxmlmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'review/idgxmlbuilder'
require 'review/version'
require 'review/makerhelper'
require 'review/img_math'
require 'review/loggable'

module ReVIEW
Expand All @@ -28,6 +29,7 @@ class IDGXMLMaker
def initialize
@basedir = nil
@logger = ReVIEW.logger
@img_math = nil
@plaintext = nil
@compile_errors = nil
end
Expand Down Expand Up @@ -67,6 +69,7 @@ def build_path
end

def remove_old_files(path)
@img_math.cleanup_mathimg
FileUtils.rm_rf(path)
end

Expand All @@ -81,6 +84,9 @@ def execute(*args)
rescue ReVIEW::ConfigError => e
error! e.message
end

@img_math = ReVIEW::ImgMath.new(@config)

I18n.setup(@config['language'])
begin
generate_idgxml_files(yamlfile)
Expand All @@ -90,6 +96,10 @@ def execute(*args)

error! e.message
end

if @config['math_format'] == 'imgmath'
@img_math.make_math_images
end
end

def generate_idgxml_files(yamlfile)
Expand Down Expand Up @@ -130,7 +140,7 @@ def apply_filter(xmlfile)

def build_body(basetmpdir, _yamlfile)
base_path = Pathname.new(@basedir)
@converter = ReVIEW::Converter.new(@book, ReVIEW::IDGXMLBuilder.new)
@converter = ReVIEW::Converter.new(@book, ReVIEW::IDGXMLBuilder.new(img_math: @img_math))
@book.parts.each do |part|
if part.name.present?
if part.file?
Expand Down
39 changes: 39 additions & 0 deletions test/test_idgxmlbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'test_helper'
require 'book_test_helper'
require 'review/compiler'
require 'review/book'
require 'review/idgxmlbuilder'
require 'review/i18n'

class IDGXMLBuidlerTest < Test::Unit::TestCase
include ReVIEW
include BookTestHelper

def setup
@builder = IDGXMLBuilder.new
Expand Down Expand Up @@ -261,6 +263,12 @@ def test_inline_m
assert_equal %Q(<replace idref="texinline-1"><pre>\\sin</pre></replace> <replace idref="texinline-2"><pre>\\frac{1}{2}</pre></replace>), actual
end

def test_inline_m_imgmath
@config['math_format'] = 'imgmath'
actual = compile_inline('@<m>{\\sin} @<m>{\\frac{1\\}{2\\}}')
assert_equal %Q(<inlineequation><Image href="file://images/_review_math/_gen_5fded382aa33f0f0652092d41e05c743f7453c26ca1433038a4883234975a9b0.png" type="inline" /></inlineequation> <inlineequation><Image href="file://images/_review_math/_gen_e7e9536310cdba7ff948771f791cefe32f99b73c608778c9660db79e4926e9f9.png" type="inline" /></inlineequation>), actual
end

def test_dlist_beforeulol
actual = compile_block(" : foo\n foo.\n\npara\n\n : foo\n foo.\n\n 1. bar\n\n : foo\n foo.\n\n * bar\n")
assert_equal %Q(<dl><dt>foo</dt><dd>foo.</dd></dl><p>para</p><dl><dt>foo</dt><dd>foo.</dd></dl><ol><li aid:pstyle="ol-item" olnum="1" num="1">bar</li></ol><dl><dt>foo</dt><dd>foo.</dd></dl><ul><li aid:pstyle="ul-item">bar</li></ul>), actual
Expand Down Expand Up @@ -1286,6 +1294,37 @@ def test_texequation_with_caption
assert_equal expected, actual
end

def test_texequation_imgmath
@config['math_format'] = 'imgmath'
src = <<-EOS
//texequation{
p \\land \\bm{P} q
//}
EOS
expected = %Q(<equationimage><Image href="file://images/_review_math/_gen_84291054a12d278ea05694c20fbbc8e974ec66fc13be801c01dca764faeecccb.png" /></equationimage>)
actual = compile_block(src)
assert_equal expected, actual
end

def test_texequation_with_caption_imgmath
@config['math_format'] = 'imgmath'
src = <<-EOS
@<eq>{emc2}
//texequation[emc2][The Equivalence of Mass @<i>{and} Energy]{
e=mc^2
//}
EOS
expected = %Q(<p><span type='eq'>式1.1</span></p><equationblock><caption>式1.1 The Equivalence of Mass <i>and</i> Energy</caption><equationimage><Image href="file://images/_review_math/_gen_882e99d99b276a2118a3894895b6da815a03261f4150148c99b932bec5355f25.png" /></equationimage></equationblock>)
actual = compile_block(src)
assert_equal expected, actual

@config['caption_position']['equation'] = 'bottom'
expected = %Q(<p><span type='eq'>式1.1</span></p><equationblock><equationimage><Image href="file://images/_review_math/_gen_882e99d99b276a2118a3894895b6da815a03261f4150148c99b932bec5355f25.png" /></equationimage><caption>式1.1 The Equivalence of Mass <i>and</i> Energy</caption></equationblock>)
actual = compile_block(src)
assert_equal expected, actual
end

def test_nest_error_close1
src = <<-EOS
//beginchild
Expand Down

0 comments on commit caf998d

Please sign in to comment.