Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] EPUB: ハイライトのエスケープ #1256

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2002-2018 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2019 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -225,8 +225,8 @@ def blankline
puts ''
end

def compile_inline(s)
@compiler.text(s)
def compile_inline(s, esc_array = nil)
@compiler.text(s, esc_array)
end

def inline_chapref(id)
Expand Down Expand Up @@ -632,5 +632,9 @@ def escape(str)
def unescape(str)
str
end

def revert_escape(s, esc_array)
s.gsub("\x01") { esc_array.shift }
end
end
end # module ReVIEW
16 changes: 12 additions & 4 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2009-2018 Minero Aoki, Kenshi Muto
# Copyright (c) 2009-2019 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2007 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -445,7 +445,10 @@ def compile_paragraph(f)
def read_command(f)
line = f.gets
name = line.slice(/[a-z]+/).to_sym
ignore_inline = (name == :embed)
ignore_inline = nil
if %i[embed list emlist source cmd listnum emlistnum].include?(name)
ignore_inline = true
end
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 Down Expand Up @@ -550,7 +553,7 @@ def revert_replace_fence(str)
str.gsub("\x01", '@').gsub("\x02", '\\').gsub("\x03", '{').gsub("\x04", '}')
end

def text(str)
def text(str, esc_array = nil)
return '' if str.empty?
words = replace_fence(str).split(/(@<\w+>\{(?:[^\}\\]|\\.)*?\})/, -1)
words.each do |w|
Expand All @@ -560,7 +563,12 @@ def text(str)
end
result = @strategy.nofunc_text(revert_replace_fence(words.shift))
until words.empty?
result << compile_inline(revert_replace_fence(words.shift.gsub(/\\\}/, '}').gsub(/\\\\/, '\\')))
if esc_array
esc_array << compile_inline(revert_replace_fence(words.shift.gsub(/\\\}/, '}').gsub(/\\\\/, '\\')))
result << "\x01"
else
result << compile_inline(revert_replace_fence(words.shift.gsub(/\\\}/, '}').gsub(/\\\\/, '\\')))
end
result << @strategy.nofunc_text(revert_replace_fence(words.shift))
end
result
Expand Down
46 changes: 32 additions & 14 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ def list_body(_id, lines, lang)
class_names.push("language-#{lexer}") unless lexer.blank?
class_names.push('highlight') if highlight?
print %Q(<pre class="#{class_names.join(' ')}">)
body = lines.inject('') { |i, j| i + detab(j) + "\n" }
puts highlight(body: body, lexer: lexer, format: 'html')
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(j) }
puts revert_escape(highlight(body: body, lexer: lexer, format: 'html'), esc_array)
puts '</pre>'
end

Expand All @@ -417,9 +419,11 @@ def source_header(caption)

def source_body(_id, lines, lang)
print %Q(<pre class="source">)
body = lines.inject('') { |i, j| i + detab(j) + "\n" }
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(j) }
lexer = lang
puts highlight(body: body, lexer: lexer, format: 'html')
puts revert_escape(highlight(body: body, lexer: lexer, format: 'html'), esc_array)
puts '</pre>'
end

Expand All @@ -435,19 +439,21 @@ def listnum(lines, id, caption, lang = nil)
end

def listnum_body(lines, lang)
body = lines.inject('') { |i, j| i + detab(j) + "\n" }
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(j) }
lexer = lang
first_line_number = line_num
hs = highlight(body: body, lexer: lexer, format: 'html', linenum: true,
options: { linenostart: first_line_number })

if highlight?
puts hs
puts revert_escape(hs, esc_array)
else
class_names = ['list']
class_names.push("language-#{lang}") unless lang.blank?
print %Q(<pre class="#{class_names.join(' ')}">)
hs.split("\n").each_with_index do |line, i|
revert_escape(hs, esc_array).split("\n").each_with_index do |line, i|
puts detab((i + first_line_number).to_s.rjust(2) + ': ' + line)
end
puts '</pre>'
Expand All @@ -463,9 +469,11 @@ def emlist(lines, caption = nil, lang = nil)
class_names.push("language-#{lang}") unless lang.blank?
class_names.push('highlight') if highlight?
print %Q(<pre class="#{class_names.join(' ')}">)
body = lines.inject('') { |i, j| i + detab(j) + "\n" }
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(j) }
lexer = lang
puts highlight(body: body, lexer: lexer, format: 'html')
puts revert_escape(highlight(body: body, lexer: lexer, format: 'html'), esc_array)
puts '</pre>'
puts '</div>'
end
Expand All @@ -476,19 +484,21 @@ def emlistnum(lines, caption = nil, lang = nil)
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end

body = lines.inject('') { |i, j| i + detab(j) + "\n" }
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(j) }
lexer = lang
first_line_number = line_num
hs = highlight(body: body, lexer: lexer, format: 'html', linenum: true,
options: { linenostart: first_line_number })
if highlight?
puts hs
puts revert_escape(hs, esc_array)
else
class_names = ['emlist']
class_names.push("language-#{lang}") unless lang.blank?
class_names.push('highlight') if highlight?
print %Q(<pre class="#{class_names.join(' ')}">)
hs.split("\n").each_with_index do |line, i|
revert_escape(hs, esc_array).split("\n").each_with_index do |line, i|
puts detab((i + first_line_number).to_s.rjust(2) + ': ' + line)
end
puts '</pre>'
Expand All @@ -503,9 +513,11 @@ def cmd(lines, caption = nil)
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end
print %Q(<pre class="cmd">)
body = lines.inject('') { |i, j| i + detab(j) + "\n" }
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(j) }
lexer = 'shell-session'
puts highlight(body: body, lexer: lexer, format: 'html')
puts revert_escape(highlight(body: body, lexer: lexer, format: 'html'), esc_array)
puts '</pre>'
puts '</div>'
end
Expand Down Expand Up @@ -1219,6 +1231,12 @@ def olnum(num)
@ol_num = num.to_i
end

def revert_escape(s, esc_array)
s.gsub("<span class=\"err\">\x01</span>", "\x01").
gsub("<span style=\"border: 1px solid #FF0000\">\x01</span>", "\x01").
gsub("\x01") { esc_array.shift }
end

def defer_math_image(str, path, key)
# for Re:VIEW >3
File.open(File.join(File.dirname(path), '__IMGMATH_BODY__.tex'), 'a+') do |f|
Expand Down
38 changes: 28 additions & 10 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2018 Minero Aoki, Kenshi Muto
# Copyright (c) 2008-2019 Minero Aoki, Kenshi Muto
# 2002-2007 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -279,15 +279,16 @@ def list_header(id, caption, _lang)

def codelines_body(lines)
no = 1
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
lines.each do |line|
if @book.config['listinfo']
print %Q(<listinfo line="#{no}")
print %Q( begin="1") if no == 1
print %Q( end="#{no}") if no == lines.size
print '>'
end
print detab(line)
print "\n"
print revert_escape(detab(line), esc_array)
print '</listinfo>' if @book.config['listinfo']
no += 1
end
Expand All @@ -304,27 +305,43 @@ def emlist(lines, caption = nil, _lang = nil)
end

def emlistnum(lines, caption = nil, _lang = nil)
lines2 = []
print %Q(<list type='emlistnum'>)
puts "<caption aid:pstyle='emlistnum-title'>#{compile_inline(caption)}</caption>" if caption.present?
print '<pre>'
no = 1
first_line_num = line_num
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
lines.each_with_index do |line, i|
lines2 << detab(%Q(<span type='lineno'>) + (i + first_line_num).to_s.rjust(2) + ': </span>' + line)
if @book.config['listinfo']
print %Q(<listinfo line="#{no}")
print %Q( begin="1") if no == 1
print %Q( end="#{no}") if no == lines.size
print '>'
end
print revert_escape(detab(%Q(<span type='lineno'>) + (i + first_line_num).to_s.rjust(2) + ': </span>' + line), esc_array)

print '</listinfo>' if @book.config['listinfo']
no += 1
end
quotedlist lines2, 'emlistnum', caption
puts '</pre></list>'
end

def listnum_body(lines, _lang)
print '<pre>'
no = 1
first_line_num = line_num
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
lines.each_with_index do |line, i|
if @book.config['listinfo']
print %Q(<listinfo line="#{no}")
print %Q( begin="1") if no == 1
print %Q( end="#{no}") if no == lines.size
print '>'
end
print detab(%Q(<span type='lineno'>) + (i + first_line_num).to_s.rjust(2) + ': </span>' + line)
print "\n"
print revert_escape(detab(%Q(<span type='lineno'>) + (i + first_line_num).to_s.rjust(2) + ': </span>' + line), esc_array)

print '</listinfo>' if @book.config['listinfo']
no += 1
end
Expand All @@ -340,15 +357,16 @@ def quotedlist(lines, css_class, caption)
puts "<caption aid:pstyle='#{css_class}-title'>#{compile_inline(caption)}</caption>" if caption.present?
print '<pre>'
no = 1
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
lines.each do |line|
if @book.config['listinfo']
print %Q(<listinfo line="#{no}")
print %Q( begin="1") if no == 1
print %Q( end="#{no}") if no == lines.size
print '>'
end
print detab(line)
print "\n"
print revert_escape(detab(line), esc_array)
print '</listinfo>' if @book.config['listinfo']
no += 1
end
Expand Down
24 changes: 15 additions & 9 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def emlist(lines, caption = nil, lang = nil)
if highlight_listings?
common_code_block_lst(nil, lines, 'reviewemlistlst', 'title', caption, lang)
else
common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, _idx| detab(line) + "\n" }
common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, _idx| detab(line) }
end
end

Expand All @@ -344,7 +344,7 @@ def emlistnum(lines, caption = nil, lang = nil)
if highlight_listings?
common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang, first_line_num: first_line_num)
else
common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, idx| detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) + "\n" }
common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, idx| detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) }
end
end

Expand All @@ -353,7 +353,7 @@ def list(lines, id, caption, lang = nil)
if highlight_listings?
common_code_block_lst(id, lines, 'reviewlistlst', 'caption', caption, lang)
else
common_code_block(id, lines, 'reviewlist', caption, lang) { |line, _idx| detab(line) + "\n" }
common_code_block(id, lines, 'reviewlist', caption, lang) { |line, _idx| detab(line) }
end
end

Expand All @@ -363,7 +363,7 @@ def listnum(lines, id, caption, lang = nil)
if highlight_listings?
common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang, first_line_num: first_line_num)
else
common_code_block(id, lines, 'reviewlist', caption, lang) { |line, idx| detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) + "\n" }
common_code_block(id, lines, 'reviewlist', caption, lang) { |line, idx| detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) }
end
end

Expand All @@ -372,7 +372,7 @@ def cmd(lines, caption = nil, lang = nil)
common_code_block_lst(nil, lines, 'reviewcmdlst', 'title', caption, lang)
else
blank
common_code_block(nil, lines, 'reviewcmd', caption, lang) { |line, _idx| detab(line) + "\n" }
common_code_block(nil, lines, 'reviewcmd', caption, lang) { |line, _idx| detab(line) }
end
end

Expand All @@ -398,11 +398,15 @@ def common_code_block(id, lines, command, caption, _lang)
end
@doc_status[:caption] = nil
body = ''

esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }

lines.each_with_index do |line, idx|
body.concat(yield(line, idx))
end
puts macro('begin', command)
print body
print revert_escape(body, esc_array)
puts macro('end', command)
unless @book.config.check_version('2', exception: false)
puts '\\end{reviewlistblock}'
Expand All @@ -414,10 +418,12 @@ def common_code_block_lst(_id, lines, command, title, caption, lang, first_line_
if title == 'title' && caption.blank? && @book.config.check_version('2', exception: false)
print '\vspace{-1.5em}'
end
body = lines.inject('') { |i, j| i + detab(unescape(j)) + "\n" }
esc_array = []
lines.map! { |l| compile_inline(l, esc_array) }
body = lines.inject('') { |i, j| i + detab(unescape(j)) }
args = make_code_block_args(title, caption, lang, first_line_num: first_line_num)
puts %Q(\\begin{#{command}}[#{args}])
print body
print revert_escape(body, esc_array)
puts %Q(\\end{#{command}})
blank
end
Expand Down Expand Up @@ -449,7 +455,7 @@ def source(lines, caption = nil, lang = nil)
if highlight_listings?
common_code_block_lst(nil, lines, 'reviewsourcelst', 'title', caption, lang)
else
common_code_block(nil, lines, 'reviewsource', caption, lang) { |line, _idx| detab(line) + "\n" }
common_code_block(nil, lines, 'reviewsource', caption, lang) { |line, _idx| detab(line) }
end
end

Expand Down
Loading