From aaee0f730ccf659bcd0e7f5b87180c4ab6fb2ecc Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 14 May 2018 00:14:01 +0900 Subject: [PATCH 1/5] add @w, @wb. Closes #1007 --- lib/review/builder.rb | 25 +++++++++++++++++++++++++ lib/review/compiler.rb | 2 ++ 2 files changed, 27 insertions(+) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index e87062612..25f2ceeeb 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -13,6 +13,7 @@ require 'stringio' require 'cgi' require 'fileutils' +require 'csv' module ReVIEW class Builder @@ -35,6 +36,8 @@ def initialize(strict = false, *args) @output = nil @logger = ReVIEW.logger @doc_status = {} + @dictionary = {} + load_csv('words.csv') # FIXME builder_init(*args) end @@ -81,6 +84,14 @@ def target_name self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase end + def load_csv(file) + if File.exist?(file) + CSV.foreach(file) do |row| + @dictionary[row[0]] = row[1] + end + end + end + def headline_prefix(level) @sec_counter.inc(level) anchor = @sec_counter.anchor(level) @@ -345,6 +356,20 @@ def inline_tcy(arg) "#{arg}[rotate 90 degree]" end + def inline_w(s) + translated = @dictionary[s] + if translated + escape(translated) + else + @logger.info("missing word: #{s}") + escape(s) + end + end + + def inline_wb(s) + inline_b(unescape(inline_w(s))) + end + def raw(str) if matched = str.match(/\|(.*?)\|(.*)/) builders = matched[1].split(',').map { |i| i.gsub(/\s/, '') } diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index b3476c5a0..faa88429c 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -231,6 +231,8 @@ def inline_defined?(name) definline :tcy definline :embed definline :pageref + definline :w + definline :wb private From 3947eca0c9a8c8ded6fed92435c9d4c09ebd1fa3 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 14 May 2018 08:15:09 +0900 Subject: [PATCH 2/5] use warn & inline message for missing word --- lib/review/builder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 25f2ceeeb..6287cdd56 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -361,8 +361,8 @@ def inline_w(s) if translated escape(translated) else - @logger.info("missing word: #{s}") - escape(s) + warn "word not bound: #{s}" + escape("[missing word: #{s}]") end end From af6b6cc703ee857ae91db113417b0bef2cf97708 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Thu, 17 May 2018 22:07:05 +0900 Subject: [PATCH 3/5] introduce wordcsvfile parameter to specify csv file path --- doc/config.yml.sample | 3 +++ lib/review/builder.rb | 10 +++++++--- lib/review/configure.rb | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/config.yml.sample b/doc/config.yml.sample index f69ff86bc..500bfedab 100644 --- a/doc/config.yml.sample +++ b/doc/config.yml.sample @@ -183,6 +183,9 @@ secnolevel: 2 # reファイルを格納するディレクトリ。省略した場合は以下 (. はカレントディレクトリを示す) # contentdir: . +# @命令で使用するCSV単語ファイルのパス +# wordcsvfile: words.csv + # 1ページの行数文字数と1kbごとのページ数を用紙サイズで指定する(A5 or B5)。 # page_metric: A5 # diff --git a/lib/review/builder.rb b/lib/review/builder.rb index 6287cdd56..e2b02bca9 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -37,7 +37,6 @@ def initialize(strict = false, *args) @logger = ReVIEW.logger @doc_status = {} @dictionary = {} - load_csv('words.csv') # FIXME builder_init(*args) end @@ -55,8 +54,13 @@ def bind(compiler, chapter, location) end @tabwidth = nil @tsize = nil - if @book && @book.config && @book.config['tabwidth'] - @tabwidth = @book.config['tabwidth'] + if @book && @book.config + if @book.config['wordcsvfile'] + load_csv(@book.config['wordcsvfile']) + end + if @book.config['tabwidth'] + @tabwidth = @book.config['tabwidth'] + end end builder_init_file end diff --git a/lib/review/configure.rb b/lib/review/configure.rb index f5b6bf3cc..626ddf1cc 100644 --- a/lib/review/configure.rb +++ b/lib/review/configure.rb @@ -62,6 +62,7 @@ def self.values 'ext' => '.re', 'image_types' => %w[.ai .psd .eps .pdf .tif .tiff .png .bmp .jpg .jpeg .gif .svg], 'bib_file' => 'bib.re', + 'wordcsvfile' => nil, 'colophon_order' => %w[aut csl trl dsr ill cov edt pbl contact prt], 'externallink' => true, # for IDGXML From e6186d3fe332c914ad8ba4ca5f588eaf077d210f Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Fri, 18 May 2018 15:12:17 +0900 Subject: [PATCH 4/5] use words_file for the name --- doc/config.yml.sample | 4 ++-- lib/review/builder.rb | 12 +++++++----- lib/review/configure.rb | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/doc/config.yml.sample b/doc/config.yml.sample index 500bfedab..df0e3bc7d 100644 --- a/doc/config.yml.sample +++ b/doc/config.yml.sample @@ -183,8 +183,8 @@ secnolevel: 2 # reファイルを格納するディレクトリ。省略した場合は以下 (. はカレントディレクトリを示す) # contentdir: . -# @命令で使用するCSV単語ファイルのパス -# wordcsvfile: words.csv +# @命令で使用する単語ファイルのパス +# words_file: words.csv # 1ページの行数文字数と1kbごとのページ数を用紙サイズで指定する(A5 or B5)。 # page_metric: A5 diff --git a/lib/review/builder.rb b/lib/review/builder.rb index e6302cc01..540651eb8 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -56,8 +56,8 @@ def bind(compiler, chapter, location) @tabwidth = nil @tsize = nil if @book && @book.config - if @book.config['wordcsvfile'] - load_csv(@book.config['wordcsvfile']) + if @book.config['words_file'] + load_words(@book.config['words_file']) end if @book.config['tabwidth'] @tabwidth = @book.config['tabwidth'] @@ -89,10 +89,12 @@ def target_name self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase end - def load_csv(file) + def load_words(file) if File.exist?(file) - CSV.foreach(file) do |row| - @dictionary[row[0]] = row[1] + if file =~ /\.csv\Z/i + CSV.foreach(file) do |row| + @dictionary[row[0]] = row[1] + end end end end diff --git a/lib/review/configure.rb b/lib/review/configure.rb index 626ddf1cc..744386abe 100644 --- a/lib/review/configure.rb +++ b/lib/review/configure.rb @@ -62,7 +62,7 @@ def self.values 'ext' => '.re', 'image_types' => %w[.ai .psd .eps .pdf .tif .tiff .png .bmp .jpg .jpeg .gif .svg], 'bib_file' => 'bib.re', - 'wordcsvfile' => nil, + 'words_file' => nil, 'colophon_order' => %w[aut csl trl dsr ill cov edt pbl contact prt], 'externallink' => true, # for IDGXML From 7ae0a7b2ebf474221e7d062aa6cd47ed5b620945 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Fri, 18 May 2018 19:48:36 +0900 Subject: [PATCH 5/5] test for inline_w and inline_wb --- test/test_htmlbuilder.rb | 15 +++++++++++++++ test/test_latexbuilder.rb | 19 +++++++++++++++++++ test/test_topbuilder.rb | 21 +++++++++++++++------ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index 597fc1640..6afc55d92 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -1720,6 +1720,21 @@ def test_inline_fence assert_equal 'test @<code>{$サンプル$}', actual end + def test_inline_w + Dir.mktmpdir do |dir| + File.open(File.join(dir, 'words.csv'), 'w') do |f| + f.write <_@{BAZ}" +EOB + end + @book.config['words_file'] = File.join(dir, 'words.csv') + + actual = compile_block('@{F} @{B} @{B} @{N}') + assert_equal %Q(

foo bar"\\<>_@<b>{BAZ} bar"\\<>_@<b>{BAZ} [missing word: N]

\n), actual + end + end + def test_inline_unknown e = assert_raises(ReVIEW::ApplicationError) { compile_block "@{n}\n" } assert_equal ':1: error: unknown image: n', e.message diff --git a/test/test_latexbuilder.rb b/test/test_latexbuilder.rb index 1fa1b8a78..04ad64203 100644 --- a/test/test_latexbuilder.rb +++ b/test/test_latexbuilder.rb @@ -993,6 +993,25 @@ def test_inline_fence assert_equal 'test \\texttt{@\\textless{}code\\textgreater{}\\{\\textdollar{}サンプル\\textdollar{}\\}}', actual end + def test_inline_w + Dir.mktmpdir do |dir| + File.open(File.join(dir, 'words.csv'), 'w') do |f| + f.write <_@{BAZ}" +EOB + end + @book.config['words_file'] = File.join(dir, 'words.csv') + + actual = compile_block('@{F} @{B} @{B} @{N}') + expected = <<-EOS + +foo bar"\\reviewbackslash{}\\textless{}\\textgreater{}\\textunderscore{}@\\textless{}b\\textgreater{}\\{BAZ\\} \\textbf{bar"\\reviewbackslash{}\\textless{}\\textgreater{}\\textunderscore{}@\\textless{}b\\textgreater{}\\{BAZ\\}} [missing word: N] +EOS + assert_equal expected, actual + end + end + def test_inline_unknown e = assert_raises(ReVIEW::ApplicationError) { compile_block "@{n}\n" } assert_equal ':1: error: unknown image: n', e.message diff --git a/test/test_topbuilder.rb b/test/test_topbuilder.rb index dc299cb90..8acbcbb28 100644 --- a/test/test_topbuilder.rb +++ b/test/test_topbuilder.rb @@ -19,12 +19,6 @@ def setup location = Location.new(nil, nil) @builder.bind(@compiler, @chapter, location) - @builder.instance_eval do - # to ignore lineno in original method - def warn(msg) - puts msg - end - end I18n.setup(@config['language']) end @@ -297,6 +291,21 @@ def test_texequation assert_equal %Q(◆→開始:TeX式←◆\n\\sin\n1^{2}\n◆→終了:TeX式←◆\n\n), actual end + def test_inline_w + Dir.mktmpdir do |dir| + File.open(File.join(dir, 'words.csv'), 'w') do |f| + f.write <_@{BAZ}" +EOB + end + @book.config['words_file'] = File.join(dir, 'words.csv') + + actual = compile_block('@{F} @{B} @{B} @{N}') + assert_equal %Q(foo bar"\\<>_@{BAZ} ★bar"\\<>_@{BAZ}☆ [missing word: N]\n), actual + end + end + def test_inline_unknown e = assert_raises(ReVIEW::ApplicationError) { compile_block "@{n}\n" } assert_equal ':1: error: unknown image: n', e.message