Skip to content

Commit

Permalink
Merge pull request #1010 from kmuto/inline_w
Browse files Browse the repository at this point in the history
@<w>, @<wb>の追加
  • Loading branch information
kmuto authored May 18, 2018
2 parents 6e4f6e7 + 7ae0a7b commit 5cb50f3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ secnolevel: 2
# reファイルを格納するディレクトリ。省略した場合は以下 (. はカレントディレクトリを示す)
# contentdir: .

# @<w>命令で使用する単語ファイルのパス
# words_file: words.csv

# 1ページの行数文字数と1kbごとのページ数を用紙サイズで指定する(A5 or B5)。
# page_metric: A5
#
Expand Down
35 changes: 33 additions & 2 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'cgi'
require 'fileutils'
require 'tempfile'
require 'csv'

module ReVIEW
class Builder
Expand All @@ -36,6 +37,7 @@ def initialize(strict = false, *args)
@output = nil
@logger = ReVIEW.logger
@doc_status = {}
@dictionary = {}
builder_init(*args)
end

Expand All @@ -53,8 +55,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['words_file']
load_words(@book.config['words_file'])
end
if @book.config['tabwidth']
@tabwidth = @book.config['tabwidth']
end
end
builder_init_file
end
Expand Down Expand Up @@ -82,6 +89,16 @@ def target_name
self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase
end

def load_words(file)
if File.exist?(file)
if file =~ /\.csv\Z/i
CSV.foreach(file) do |row|
@dictionary[row[0]] = row[1]
end
end
end
end

def headline_prefix(level)
@sec_counter.inc(level)
anchor = @sec_counter.anchor(level)
Expand Down Expand Up @@ -346,6 +363,20 @@ def inline_tcy(arg)
"#{arg}[rotate 90 degree]"
end

def inline_w(s)
translated = @dictionary[s]
if translated
escape(translated)
else
warn "word not bound: #{s}"
escape("[missing word: #{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/, '') }
Expand Down
2 changes: 2 additions & 0 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def inline_defined?(name)
definline :tcy
definline :embed
definline :pageref
definline :w
definline :wb

private

Expand Down
1 change: 1 addition & 0 deletions lib/review/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
'words_file' => nil,
'colophon_order' => %w[aut csl trl dsr ill cov edt pbl contact prt],
'externallink' => true,
# for IDGXML
Expand Down
15 changes: 15 additions & 0 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,21 @@ def test_inline_fence
assert_equal 'test <code class="inline-code tt">@&lt;code&gt;{$サンプル$}</code>', actual
end

def test_inline_w
Dir.mktmpdir do |dir|
File.open(File.join(dir, 'words.csv'), 'w') do |f|
f.write <<EOB
"F","foo"
"B","bar""\\<>_@<b>{BAZ}"
EOB
end
@book.config['words_file'] = File.join(dir, 'words.csv')

actual = compile_block('@<w>{F} @<w>{B} @<wb>{B} @<w>{N}')
assert_equal %Q(<p>foo bar&quot;\\&lt;&gt;_@&lt;b&gt;{BAZ} <b>bar&quot;\\&lt;&gt;_@&lt;b&gt;{BAZ}</b> [missing word: N]</p>\n), actual
end
end

def test_inline_unknown
e = assert_raises(ReVIEW::ApplicationError) { compile_block "@<img>{n}\n" }
assert_equal ':1: error: unknown image: n', e.message
Expand Down
19 changes: 19 additions & 0 deletions test/test_latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOB
"F","foo"
"B","bar""\\<>_@<b>{BAZ}"
EOB
end
@book.config['words_file'] = File.join(dir, 'words.csv')

actual = compile_block('@<w>{F} @<w>{B} @<wb>{B} @<w>{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 "@<img>{n}\n" }
assert_equal ':1: error: unknown image: n', e.message
Expand Down
21 changes: 15 additions & 6 deletions test/test_topbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 <<EOB
"F","foo"
"B","bar""\\<>_@<b>{BAZ}"
EOB
end
@book.config['words_file'] = File.join(dir, 'words.csv')

actual = compile_block('@<w>{F} @<w>{B} @<wb>{B} @<w>{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 "@<img>{n}\n" }
assert_equal ':1: error: unknown image: n', e.message
Expand Down

0 comments on commit 5cb50f3

Please sign in to comment.