Skip to content

Commit

Permalink
implement equation numbering. Closes #1167
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Oct 23, 2018
1 parent f07760d commit 6d1d3b0
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 97 deletions.
1 change: 1 addition & 0 deletions lib/review/book/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def initialize(book, number, name, path, io = nil)
end
@list_index = nil
@table_index = nil
@equation_index = nil
@footnote_index = nil
@image_index = nil
@icon_index = nil
Expand Down
9 changes: 9 additions & 0 deletions lib/review/book/compilable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def table_index
@table_index
end

def equation(id)
equation_index[id]
end

def equation_index
@equation_index ||= EquationIndex.parse(lines)
@equation_index
end

def footnote(id)
footnote_index[id]
end
Expand Down
8 changes: 7 additions & 1 deletion lib/review/book/index.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2017 Minero Aoki, Kenshi Muto
# Copyright (c) 2008-2018 Minero Aoki, Kenshi Muto
# 2002-2007 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -128,6 +128,12 @@ def self.item_type
end
end

class EquationIndex < Index
def self.item_type
'(texequation)'
end
end

class FootnoteIndex < Index
Item = Struct.new(:id, :number, :content)

Expand Down
32 changes: 29 additions & 3 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,23 @@ def inline_title(id)
end

def inline_list(id)
"#{I18n.t('list')}#{@chapter.list(id).number}"
chapter, id = extract_chapter_id(id)
if get_chap(chapter)
%Q(#{I18n.t('list')}#{I18n.t('format_number', [get_chap(chapter), chapter.list(id).number])})
else
%Q(#{I18n.t('list')}#{I18n.t('format_number_without_chapter', [chapter.list(id).number])})
end
rescue KeyError
error "unknown list: #{id}"
end

def inline_img(id)
"#{I18n.t('image')}#{@chapter.image(id).number}"
chapter, id = extract_chapter_id(id)
if get_chap(chapter)
%Q(#{I18n.t('image')}#{I18n.t('format_number', [get_chap(chapter), chapter.image(id).number])})
else
%Q(#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [chapter.image(id).number])})
end
rescue KeyError
error "unknown image: #{id}"
end
Expand All @@ -270,11 +280,27 @@ def inline_imgref(id)
end

def inline_table(id)
"#{I18n.t('table')}#{@chapter.table(id).number}"
chapter, id = extract_chapter_id(id)
if get_chap(chapter)
%Q(#{I18n.t('table')}#{I18n.t('format_number', [get_chap(chapter), chapter.table(id).number])})
else
%Q(#{I18n.t('table')}#{I18n.t('format_number_without_chapter', [chapter.table(id).number])})
end
rescue KeyError
error "unknown table: #{id}"
end

def inline_eq(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter)
%Q(#{I18n.t('equation')}#{I18n.t('format_number', [get_chap(chapter), chapter.equation(id).number])})
else
%Q(#{I18n.t('equation')}#{I18n.t('format_number_without_chapter', [chapter.equation(id).number])})
end
rescue KeyError
error "unknown equation: #{id}"
end

def inline_fn(id)
@chapter.footnote(id).content
rescue KeyError
Expand Down
3 changes: 2 additions & 1 deletion lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def inline_defined?(name)
defblock :bibpaper, 2..3, true
defblock :doorquote, 1
defblock :talk, 0
defblock :texequation, 0
defblock :texequation, 0..2
defblock :graph, 1..3
defblock :indepimage, 1..3, true
defblock :numberlessimage, 1..3, true
Expand Down Expand Up @@ -184,6 +184,7 @@ def inline_defined?(name)
definline :icon
definline :list
definline :table
definline :eq
definline :fn
definline :kw
definline :ruby
Expand Down
64 changes: 37 additions & 27 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,28 @@ def talk(lines)
puts '</div>'
end

def texequation(lines)
def texequation(lines, id = nil, caption = nil)
if id
texequation_header id, caption
end

texequation_body(lines)

if id
puts '</div>'
end
end

def texequation_header(id, caption)
puts %Q(<div id="#{normalize_id(id)}" class="caption-equation")
if get_chap
puts %Q(<p class="caption">#{I18n.t('equation')}#{I18n.t('format_number_header', [get_chap, @chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}</p>)
else
puts %Q(<p class="caption">#{I18n.t('equation')}#{I18n.t('format_number_header_without_chapter', [@chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}</p>)
end
end

def texequation_body(lines)
puts %Q(<div class="equation">)
if @book.config['mathml']
require 'math_ml'
Expand Down Expand Up @@ -1008,54 +1029,43 @@ def inline_column_chap(chapter, id)
end

def inline_list(id)
str = super(id)
chapter, id = extract_chapter_id(id)
str =
if get_chap(chapter)
"#{I18n.t('list')}#{I18n.t('format_number', [get_chap(chapter), chapter.list(id).number])}"
else
"#{I18n.t('list')}#{I18n.t('format_number_without_chapter', [chapter.list(id).number])}"
end
if @book.config['chapterlink']
%Q(<span class="listref"><a href="./#{chapter.id}#{extname}##{id}">#{str}</a></span>)
%Q(<span class="listref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
else
%Q(<span class="listref">#{str}</span>)
end
rescue KeyError
error "unknown list: #{id}"
end

def inline_table(id)
str = super(id)
chapter, id = extract_chapter_id(id)
str =
if get_chap(chapter)
"#{I18n.t('table')}#{I18n.t('format_number', [get_chap(chapter), chapter.table(id).number])}"
else
"#{I18n.t('table')}#{I18n.t('format_number_without_chapter', [chapter.table(id).number])}"
end
if @book.config['chapterlink']
%Q(<span class="tableref"><a href="./#{chapter.id}#{extname}##{id}">#{str}</a></span>)
%Q(<span class="tableref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
else
%Q(<span class="tableref">#{str}</span>)
end
rescue KeyError
error "unknown table: #{id}"
end

def inline_img(id)
str = super(id)
chapter, id = extract_chapter_id(id)
str =
if get_chap(chapter)
"#{I18n.t('image')}#{I18n.t('format_number', [get_chap(chapter), chapter.image(id).number])}"
else
"#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [chapter.image(id).number])}"
end
if @book.config['chapterlink']
%Q(<span class="imgref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
else
%Q(<span class="imgref">#{str}</span>)
end
rescue KeyError
error "unknown image: #{id}"
end

def inline_eq(id)
str = super(id)
chapter, id = extract_chapter_id(id)
if @book.config['chapterlink']
%Q(<span class="eqref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
else
%Q(<span class="eqref">#{str}</span>)
end
end

def inline_asis(str, tag)
Expand Down
5 changes: 4 additions & 1 deletion lib/review/i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ja:
image:
table:
list: リスト
equation:
column: "コラム「%s」"
columnname: "コラム"
column_head: "■コラム"
Expand Down Expand Up @@ -67,6 +68,7 @@ en:
image: "Figure "
table: "Table "
list: "List "
equation: "Equation "
column: "Column %s"
columnname: "Column"
column_head: "Column"
Expand Down Expand Up @@ -131,7 +133,8 @@ en:
zh-TW:
image:
table:
list: List
list: "List "
equation: "Equation "
column: "Column %s"
columnname: "Column"
column_head: "Column"
Expand Down
46 changes: 21 additions & 25 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,7 @@ def inline_column_chap(chapter, id)
end

def inline_list(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter).nil?
"<span type='list'>#{I18n.t('list')}#{I18n.t('format_number_without_chapter', [chapter.list(id).number])}</span>"
else
"<span type='list'>#{I18n.t('list')}#{I18n.t('format_number', [get_chap(chapter), chapter.list(id).number])}</span>"
end
rescue KeyError
error "unknown list: #{id}"
"<span type='list'>#{super(id)}</span>"
end

def list_header(id, caption, _lang)
Expand Down Expand Up @@ -369,25 +362,15 @@ def quote(lines)
end

def inline_table(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter).nil?
"<span type='table'>#{I18n.t('table')}#{I18n.t('format_number_without_chapter', [chapter.table(id).number])}</span>"
else
"<span type='table'>#{I18n.t('table')}#{I18n.t('format_number', [get_chap(chapter), chapter.table(id).number])}</span>"
end
rescue KeyError
error "unknown table: #{id}"
"<span type='table'>#{super(id)}</span>"
end

def inline_img(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter).nil?
"<span type='image'>#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [chapter.image(id).number])}</span>"
else
"<span type='image'>#{I18n.t('image')}#{I18n.t('format_number', [get_chap(chapter), chapter.image(id).number])}</span>"
end
rescue KeyError
error "unknown image: #{id}"
"<span type='image'>#{super(id)}</span>"
end

def inline_eq(id)
"<span type='eq'>#{super(id)}</span>"
end

def inline_imgref(id)
Expand Down Expand Up @@ -440,13 +423,26 @@ def image_header(id, caption)
end
end

def texequation(lines)
def texequation(lines, id = nil, caption = nil)
@texblockequation += 1
if id
puts '<equationblock>'
if get_chap.nil?
puts %Q(<caption>#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [@chapter.image(id).number])}#{I18n.t('caption_prefix_idgxml')}#{compile_inline(caption)}</caption>)
else
puts %Q(<caption>#{I18n.t('image')}#{I18n.t('format_number', [get_chap, @chapter.image(id).number])}#{I18n.t('caption_prefix_idgxml')}#{compile_inline(caption)}</caption>)
end
end

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

if id
puts '</equationblock>'
end
end

def table(lines, id = nil, caption = nil)
Expand Down
28 changes: 27 additions & 1 deletion lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,28 @@ def flushright(lines)
latex_block 'flushright', lines
end

def texequation(lines)
def texequation(lines, id = nil, caption = nil)
blank

if id
puts macro('begin', 'reviewequationblock')
if get_chap.nil?
puts macro('reviewequationcaption', "#{I18n.t('equation')}#{I18n.t('format_number_header_without_chapter', [@chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
else
puts macro('reviewequationcaption', "#{I18n.t('equation')}#{I18n.t('format_number_header', [get_chap, @chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
end
end

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

if id
puts macro('end', 'reviewequationblock')
end

blank
end

Expand Down Expand Up @@ -863,6 +878,17 @@ def inline_img(id)
error "unknown image: #{id}"
end

def inline_equation(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter).nil?
macro('reviewequationref', I18n.t('format_number_without_chapter', [chapter.equation(id).number]))
else
macro('reviewequationref', I18n.t('format_number', [get_chap(chapter), chapter.equation(id).number]))
end
rescue KeyError
error "unknown equation: #{id}"
end

def footnote(id, content)
if @book.config['footnotetext'] || @foottext[id]
puts macro("footnotetext[#{@chapter.footnote(id).number}]", compile_inline(content.strip))
Expand Down
Loading

0 comments on commit 6d1d3b0

Please sign in to comment.