Skip to content

Commit

Permalink
Merge pull request #1371 from kmuto/fix-texequation
Browse files Browse the repository at this point in the history
 refactor //texequation, //embed and //graph
  • Loading branch information
takahashim authored Aug 20, 2019
2 parents 37f81b3 + 89f5e10 commit d9ed919
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
21 changes: 11 additions & 10 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,12 @@ def inline_w(s)
end

def inline_wb(s)
inline_b(unescape(inline_w(s)))
translated = @dictionary[s]
if translated
inline_b(translated)
else
inline_b("[missing word: #{s}]")
end
end

def raw(str)
Expand All @@ -433,9 +438,9 @@ def embed(lines, arg = nil)
if arg
builders = arg.gsub(/^\s*\|/, '').gsub(/\|\s*$/, '').gsub(/\s/, '').split(',')
c = target_name
print lines.join if builders.include?(c)
print lines.join("\n") + "\n" if builders.include?(c)
else
print lines.join
print lines.join("\n") + "\n"
end
end

Expand Down Expand Up @@ -514,13 +519,13 @@ def graph(lines, id, command, caption = '')
file = "#{id}.#{image_ext}"
file_path = File.join(dir, file)

line = self.unescape(lines.join("\n"))
content = lines.join("\n") + "\n"

tf = Tempfile.new('review_graph')
tf.puts line
tf.puts content
tf.close
begin
file_path = send("graph_#{command}".to_sym, id, file_path, line, tf.path)
file_path = send("graph_#{command}".to_sym, id, file_path, content, tf.path)
ensure
tf.unlink
end
Expand Down Expand Up @@ -644,9 +649,5 @@ def detab(str, num = nil)
def escape(str)
str
end

def unescape(str)
str
end
end
end # module ReVIEW
7 changes: 5 additions & 2 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module ReVIEW
class Compiler
def initialize(strategy)
@strategy = strategy

## commands which do not parse block lines in compiler
@non_parsed_commands = %i[embed texequation graph]
end

attr_reader :strategy
Expand Down Expand Up @@ -422,7 +425,7 @@ def compile_paragraph(f)
def read_command(f)
line = f.gets
name = line.slice(/[a-z]+/).to_sym
ignore_inline = (name == :embed)
ignore_inline = @non_parsed_commands.include?(name)
args = parse_args(line.sub(%r{\A//[a-z]+}, '').rstrip.chomp('{'), name)
@strategy.doc_status[name] = true
lines = block_open?(line) ? read_block(f, ignore_inline) : nil
Expand All @@ -439,7 +442,7 @@ def read_block(f, ignore_inline)
buf = []
f.until_match(%r{\A//\}}) do |line|
if ignore_inline
buf.push(line)
buf.push(line.chomp)
elsif line !~ /\A\#@/
buf.push(text(line.rstrip))
end
Expand Down
4 changes: 2 additions & 2 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@ def texequation_body(lines)
require 'math_ml'
require 'math_ml/symbol/character_reference'
p = MathML::LaTeX::Parser.new(symbol: MathML::Symbol::CharacterReference)
puts p.parse(unescape(lines.join("\n")), true)
print p.parse(lines.join("\n") + "\n", true)
elsif @book.config['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#{unescape(lines.join("\n"))}\n\\end{equation*}\n"
math_str = "\\begin{equation*}\n\\fontsize{#{fontsize}}{#{lineheight}}\\selectfont\n#{lines.join("\n")}\n\\end{equation*}\n"
key = Digest::SHA256.hexdigest(math_str)
math_dir = File.join(@book.config['imagedir'], '_review_math')
Dir.mkdir(math_dir) unless Dir.exist?(math_dir)
Expand Down
2 changes: 1 addition & 1 deletion lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def texequation(lines, id = nil, caption = '')

puts %Q(<replace idref="texblock-#{@texblockequation}">)
puts '<pre>'
puts lines.join("\n")
print lines.join("\n")
puts '</pre>'
puts '</replace>'

Expand Down
2 changes: 1 addition & 1 deletion lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ def texequation(lines, id = nil, caption = '')

puts macro('begin', 'equation*')
lines.each do |line|
puts unescape(line)
puts line
end
puts macro('end', 'equation*')

Expand Down
8 changes: 4 additions & 4 deletions lib/review/rstbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def headline(level, label, caption)
blank
end
puts '=' * caption.size * 2
when 2 then
when 2
p = '='
when 3 then
when 3
p = '-'
when 4 then
when 4
p = '`'
when 5 then
when 5
p = '~'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/review/topbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def texequation(lines, id = nil, caption = '')
if @book.config['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#{unescape(lines.join("\n"))}\n\\end{equation*}\n"
math_str = "\\begin{equation*}\n\\fontsize{#{fontsize}}{#{lineheight}}\\selectfont\n#{lines.join("\n")}\n\\end{equation*}\n"
key = Digest::SHA256.hexdigest(math_str)
math_dir = File.join(@book.config['imagedir'], '_review_math_text')
Dir.mkdir(math_dir) unless Dir.exist?(math_dir)
Expand Down

0 comments on commit d9ed919

Please sign in to comment.