Skip to content

Commit

Permalink
Merge pull request #961 from kmuto/rescue_keyerror
Browse files Browse the repository at this point in the history
インライン参照系で、見つからないIDが示されたときのメッセージを妥当なものにする
  • Loading branch information
kmuto authored Feb 26, 2018
2 parents f2cbdaf + 5499297 commit 39b69c6
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 21 deletions.
11 changes: 1 addition & 10 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,35 +209,30 @@ def inline_chapref(id)
compile_inline @book.chapter_index.display_string(id)
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_chap(id)
@book.chapter_index.number(id)
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_title(id)
compile_inline @book.chapter_index.title(id)
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_list(id)
"#{I18n.t('list')}#{@chapter.list(id).number}"
rescue KeyError
error "unknown list: #{id}"
nofunc_text("[UnknownList:#{id}]")
end

def inline_img(id)
"#{I18n.t('image')}#{@chapter.image(id).number}"
rescue KeyError
error "unknown image: #{id}"
nofunc_text("[UnknownImage:#{id}]")
end

def inline_imgref(id)
Expand All @@ -254,14 +249,12 @@ def inline_table(id)
"#{I18n.t('table')}#{@chapter.table(id).number}"
rescue KeyError
error "unknown table: #{id}"
nofunc_text("[UnknownTable:#{id}]")
end

def inline_fn(id)
@chapter.footnote(id).content
rescue KeyError
error "unknown footnote: #{id}"
nofunc_text("[UnknownFootnote:#{id}]")
end

def inline_bou(str)
Expand Down Expand Up @@ -317,8 +310,7 @@ def inline_hd(id)
inline_hd_chap(@chapter, id)
end
rescue KeyError
error "unknown hd: #{id}"
nofunc_text("[UnknownHeader:#{id}]")
error "unknown headline: #{id}"
end

def inline_column(id)
Expand All @@ -333,7 +325,6 @@ def inline_column(id)
end
rescue KeyError
error "unknown column: #{id}"
nofunc_text("[UnknownColumn:#{id}]")
end

def inline_column_chap(chapter, id)
Expand Down
14 changes: 8 additions & 6 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,6 @@ def inline_chapref(id)
end
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_chap(id)
Expand All @@ -810,7 +809,6 @@ def inline_chap(id)
end
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_title(id)
Expand All @@ -822,7 +820,6 @@ def inline_title(id)
end
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_fn(id)
Expand All @@ -831,6 +828,8 @@ def inline_fn(id)
else
%Q(<a id="fnb-#{normalize_id(id)}" href="#fn-#{normalize_id(id)}" class="noteref">*#{@chapter.footnote(id).number}</a>)
end
rescue KeyError
error "unknown footnote: #{id}"
end

def compile_ruby(base, ruby)
Expand Down Expand Up @@ -949,6 +948,8 @@ def bibpaper_bibpaper(_id, _caption, lines)

def inline_bib(id)
%Q(<a href="#{@book.bib_file.gsub(/\.re\Z/, ".#{@book.config['htmlext']}")}#bib-#{normalize_id(id)}">[#{@chapter.bibpaper(id).number}]</a>)
rescue KeyError
error "unknown bib: #{id}"
end

def inline_hd_chap(chap, id)
Expand All @@ -964,6 +965,8 @@ def inline_hd_chap(chap, id)
else
str
end
rescue KeyError
error "unknown headline: #{id}"
end

def column_label(id, chapter = @chapter)
Expand All @@ -978,6 +981,8 @@ def inline_column_chap(chapter, id)
else
I18n.t('column', compile_inline(chapter.column(id).caption))
end
rescue KeyError
error "unknown column: #{id}"
end

def inline_list(id)
Expand All @@ -995,7 +1000,6 @@ def inline_list(id)
end
rescue KeyError
error "unknown list: #{id}"
nofunc_text("[UnknownList:#{id}]")
end

def inline_table(id)
Expand All @@ -1013,7 +1017,6 @@ def inline_table(id)
end
rescue KeyError
error "unknown table: #{id}"
nofunc_text("[UnknownTable:#{id}]")
end

def inline_img(id)
Expand All @@ -1031,7 +1034,6 @@ def inline_img(id)
end
rescue KeyError
error "unknown image: #{id}"
nofunc_text("[UnknownImage:#{id}]")
end

def inline_asis(str, tag)
Expand Down
17 changes: 14 additions & 3 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ def inline_column_chap(chapter, id)
else
I18n.t('column', compile_inline(chapter.column(id).caption))
end
rescue KeyError
error "unknown column: #{id}"
end

def inline_list(id)
Expand All @@ -270,6 +272,8 @@ def inline_list(id)
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}"
end

def list_header(id, caption, _lang)
Expand Down Expand Up @@ -373,6 +377,8 @@ def inline_table(id)
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}"
end

def inline_img(id)
Expand All @@ -382,6 +388,8 @@ def inline_img(id)
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}"
end

def inline_imgref(id)
Expand Down Expand Up @@ -600,6 +608,8 @@ def footnote(id, str)

def inline_fn(id)
%Q(<footnote>#{compile_inline(@chapter.footnote(id).content.strip)}</footnote>)
rescue KeyError
error "unknown footnote: #{id}"
end

def compile_ruby(base, ruby)
Expand Down Expand Up @@ -1063,7 +1073,6 @@ def inline_chapref(id)
end
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_chap(id)
Expand All @@ -1074,7 +1083,6 @@ def inline_chap(id)
end
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def inline_title(id)
Expand All @@ -1086,7 +1094,6 @@ def inline_title(id)
end
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def source_header(caption)
Expand Down Expand Up @@ -1117,6 +1124,8 @@ def bibpaper_bibpaper(_id, _caption, lines)

def inline_bib(id)
%Q(<span type='bibref' idref='#{id}'>[#{@chapter.bibpaper(id).number}]</span>)
rescue KeyError
error "unknown bib: #{id}"
end

def inline_hd_chap(chap, id)
Expand All @@ -1127,6 +1136,8 @@ def inline_hd_chap(chap, id)
end
end
I18n.t('chapter_quote', compile_inline(chap.headline(id).caption))
rescue KeyError
error "unknown headline: #{id}"
end

def inline_recipe(id)
Expand Down
10 changes: 10 additions & 0 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,8 @@ def inline_list(id)
else
macro('reviewlistref', I18n.t('format_number', [get_chap(chapter), chapter.list(id).number]))
end
rescue KeyError
error "unknown list: #{id}"
end

def inline_table(id)
Expand All @@ -791,6 +793,8 @@ def inline_table(id)
else
macro('reviewtableref', I18n.t('format_number', [get_chap(chapter), chapter.table(id).number]), table_label(id, chapter))
end
rescue KeyError
error "unknown table: #{id}"
end

def inline_img(id)
Expand All @@ -800,6 +804,8 @@ def inline_img(id)
else
macro('reviewimageref', I18n.t('format_number', [get_chap(chapter), chapter.image(id).number]), image_label(id, chapter))
end
rescue KeyError
error "unknown image: #{id}"
end

def footnote(id, content)
Expand All @@ -817,6 +823,8 @@ def inline_fn(id)
else
macro('footnote', compile_inline(@chapter.footnote(id).content.strip))
end
rescue KeyError
error "unknown footnote: #{id}"
end

BOUTEN = '・'.freeze
Expand Down Expand Up @@ -917,6 +925,8 @@ def inline_column_chap(chapter, id)
macro('reviewcolumnref',
I18n.t('chapter_quote', compile_inline(chapter.column(id).caption)),
column_label(id, chapter))
rescue KeyError
error "unknown column: #{id}"
end

def inline_raw(str)
Expand Down
1 change: 0 additions & 1 deletion lib/review/markdownbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ def inline_img(id)
"#{I18n.t('image')}#{@chapter.image(id).number}"
rescue KeyError
error "unknown image: #{id}"
nofunc_text("[UnknownImage:#{id}]")
end

def indepimage(_lines, id, caption = '', _metric = nil)
Expand Down
13 changes: 12 additions & 1 deletion lib/review/plaintextbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def inline_list(id)
else
%Q(#{I18n.t('list')}#{I18n.t('format_number_without_chapter', [chapter.list(id).number])})
end
rescue KeyError
error "unknown list: #{id}"
end

def list_header(id, caption, _lang)
Expand Down Expand Up @@ -212,6 +214,8 @@ def inline_table(id)
else
"#{I18n.t('table')}#{I18n.t('format_number_without_chapter', [chapter.table(id).number])}"
end
rescue KeyError
error "unknown table: #{id}"
end

def inline_img(id)
Expand All @@ -221,6 +225,8 @@ def inline_img(id)
else
"#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [chapter.image(id).number])}"
end
rescue KeyError
error "unknown image: #{id}"
end

def image(_lines, id, caption, _metric = nil)
Expand Down Expand Up @@ -313,6 +319,8 @@ def footnote(id, str)

def inline_fn(id)
" 注#{@chapter.footnote(id).number} "
rescue KeyError
error "unknown footnote: #{id}"
end

def compile_ruby(base, _ruby)
Expand Down Expand Up @@ -375,6 +383,8 @@ def bibpaper_bibpaper(_id, _caption, lines)

def inline_bib(id)
%Q(#{@chapter.bibpaper(id).number} )
rescue KeyError
error "unknown bib: #{id}"
end

def inline_hd_chap(chap, id)
Expand All @@ -383,6 +393,8 @@ def inline_hd_chap(chap, id)
return I18n.t('chapter_quote', "#{n} #{compile_inline(chap.headline(id).caption)}") if @book.config['secnolevel'] >= n.split('.').size
end
I18n.t('chapter_quote', compile_inline(chap.headline(id).caption))
rescue KeyError
error "unknown headline: #{id}"
end

def noindent
Expand Down Expand Up @@ -618,7 +630,6 @@ def inline_chapref(id)
"#{chs[0]}#{@book.chapter_index.number(id)}#{chs[1]}#{@book.chapter_index.title(id)}#{chs[2]}"
rescue KeyError
error "unknown chapter: #{id}"
nofunc_text("[UnknownChapter:#{id}]")
end

def source(lines, caption = nil, _lang = nil)
Expand Down
4 changes: 4 additions & 0 deletions lib/review/topbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def footnote(id, str)

def inline_fn(id)
"【注#{@chapter.footnote(id).number}】"
rescue KeyError
error "unknown footnote: #{id}"
end

def compile_ruby(base, ruby)
Expand Down Expand Up @@ -359,6 +361,8 @@ def bibpaper_header(id, caption)

def inline_bib(id)
%Q([#{@chapter.bibpaper(id).number}])
rescue KeyError
error "unknown bib: #{id}"
end

def noindent
Expand Down
17 changes: 17 additions & 0 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1691,4 +1691,21 @@ def test_inline_fence
actual = compile_inline('test @<code>|@<code>{$サンプル$}|')
assert_equal 'test <code class="inline-code tt">@&lt;code&gt;{$サンプル$}</code>', actual
end

def test_inline_unknown
e = assert_raises(ReVIEW::ApplicationError) { compile_block "@<img>{n}\n" }
assert_equal ':1: error: unknown image: n', e.message
e = assert_raises(ReVIEW::ApplicationError) { compile_block "@<fn>{n}\n" }
assert_equal ':1: error: unknown footnote: n', e.message
e = assert_raises(ReVIEW::ApplicationError) { compile_block "@<hd>{n}\n" }
assert_equal ':1: error: unknown headline: n', e.message
%w[list table column].each do |name|
e = assert_raises(ReVIEW::ApplicationError) { compile_block "@<#{name}>{n}\n" }
assert_equal ":1: error: unknown #{name}: n", e.message
end
%w[chap chapref title].each do |name|
e = assert_raises(ReVIEW::ApplicationError) { compile_block "@<#{name}>{n}\n" }
assert_equal ':1: error: key not found: "n"', e.message
end
end
end
Loading

0 comments on commit 39b69c6

Please sign in to comment.