Permalink
Browse files

Merge pull request #669 from kmuto/makeindex

索引の実装
  • Loading branch information...
2 parents fb51c8b + 6f8a2ae commit c0ed0e0b3b3e62b2341a3ce6ad0522b05bcd8e02 @kmuto committed on GitHub Jan 10, 2017
Showing with 111 additions and 15 deletions.
  1. +11 −0 lib/review/configure.rb
  2. +44 −12 lib/review/latexbuilder.rb
  3. +30 −2 lib/review/pdfmaker.rb
  4. +7 −0 templates/latex/layout.tex.erb
  5. +19 −1 test/test_latexbuilder.rb
@@ -65,11 +65,22 @@ def self.values
"chapref" => nil, # for IDGXML
"structuredxml" => nil, # for IDGXML
"pt_to_mm_unit" => 0.3528, # for IDGXML (DTP: 1pt = 0.3528mm, JIS: 1pt = 0.3514mm)
+
"footnotetext" => nil, # for LaTeX
"texcommand" => "uplatex", # for LaTeX
"texdocumentclass" => ["jsbook", "uplatex,oneside"], # for LaTeX
"dvicommand" => "dvipdfmx", # for LaTeX
"dvioptions" => "-d 5", # for LaTeX
+
+ "pdfmaker" => {
+ "makeindex" => nil, # Make index page
+ "makeindex_command" => "mendex", # works only when makeindex is true
+ "makeindex_options" => "-f -r -I utf8",
+ "makeindex_sty" => nil,
+ "makeindex_dic" => nil,
+ "makeindex_mecab" => true,
+ "makeindex_mecab_opts" => "-Oyomi",
+ },
]
conf.maker = nil
conf
@@ -39,10 +39,36 @@ def builder_init_file
@ol_num = nil
@first_line_num = nil
@sec_counter = SecCounter.new(5, @chapter)
+ setup_index
initialize_metachars(@book.config["texcommand"])
end
private :builder_init_file
+ def setup_index
+ @index_db = {}
+ @index_mecab = nil
+ if @book.config["pdfmaker"]["makeindex"]
+ @index_db = load_idxdb(@book.config["pdfmaker"]["makeindex_dic"]) if @book.config["pdfmaker"]["makeindex_dic"]
+ if @book.config["pdfmaker"]["makeindex_mecab"]
+ begin
+ require 'MeCab'
+ require 'nkf'
+ @index_mecab = MeCab::Tagger.new(@book.config["pdfmaker"]["makeindex_mecab_opts"])
+ rescue LoadError
+ end
+ end
+ end
+ end
+
+ def load_idxdb(file)
+ table = {}
+ File.foreach(file) do |line|
+ key, value = *line.strip.split(/\t+/, 2)
+ table[key] = value
+ end
+ table
+ end
+
def blank
@blank_needed = true
end
@@ -900,21 +926,27 @@ def bibpaper_bibpaper(id, caption, lines)
end
def index(str)
- str.sub!(/\(\)/, '')
- decl = ''
- if /@\z/ =~ str
- str.chop!
- decl = '|IndexDecl'
- end
- unless /[^ -~]/ =~ str
- if /\^/ =~ str
- macro('index', escape_index(str.gsub(/\^/, '')) + '@' + escape_index(text(str)) + decl)
+ sa = str.split('<<>>')
+
+ sa.map! do |item|
+ if @index_db[item]
+ item = escape_index(escape_latex(@index_db[item])) + "@" + escape_index(escape_latex(item))
else
- '\index{' + escape_index(text(str)) + decl + '}'
+ if item =~ /\A[[:ascii:]]+\Z/ || @index_mecab.nil?
+ _item = escape_index(escape_latex(item))
+ if _item != item
+ "#{escape_index(item)}@#{_item}"
+ else
+ _item
+ end
+ else
+ yomi = NKF.nkf("-w --hiragana", @index_mecab.parse(item).force_encoding("UTF-8").chomp)
+ escape_index(escape_latex(yomi)) + "@" + escape_index(escape_latex(item))
+ end
end
- else
- '\index{' + escape_index(@index_db[str]) + '@' + escape_index(text(str)) + '}'
end
+
+ "\\index{#{sa.join('!')}}"
end
def compile_kw(word, alt)
@@ -167,22 +167,50 @@ def build_pdf
## do compile
if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0
warn "command configuration is prohibited in safe mode. ignored."
+ texcommand = ReVIEW::Configure.values["texcommand"]
+ dvicommand = ReVIEW::Configure.values["dvicommand"]
+ dvioptions = ReVIEW::Configure.values["dvioptions"]
+ texoptions = ReVIEW::Configure.values["texoptions"]
+ makeindex_command = ReVIEW::Configure.values["pdfmaker"]["makeindex_command"]
+ makeindex_options = ReVIEW::Configure.values["pdfmaker"]["makeindex_options"]
+ makeindex_sty = ReVIEW::Configure.values["pdfmaker"]["makeindex_sty"]
+ makeindex_dic = ReVIEW::Configure.values["pdfmaker"]["makeindex_dic"]
else
texcommand = @config["texcommand"] if @config["texcommand"]
dvicommand = @config["dvicommand"] if @config["dvicommand"]
dvioptions = @config["dvioptions"] if @config["dvioptions"]
texoptions = @config["texoptions"] if @config["texoptions"]
+ makeindex_command = @config["pdfmaker"]["makeindex_command"]
+ makeindex_options = @config["pdfmaker"]["makeindex_options"]
+ makeindex_sty = @config["pdfmaker"]["makeindex_sty"]
+ makeindex_dic = @config["pdfmaker"]["makeindex_dic"]
end
- 3.times do
+
+ if makeindex_sty.present?
+ makeindex_sty = File.absolute_path(makeindex_sty, @basedir)
+ makeindex_options += " -s #{makeindex_sty}" if File.exist?(makeindex_sty)
+ end
+ if makeindex_dic.present?
+ makeindex_dic = File.absolute_path(makeindex_dic, @basedir)
+ makeindex_options += " -d #{makeindex_dic}" if File.exist?(makeindex_dic)
+ end
+
+ 2.times do
system_or_raise("#{texcommand} #{texoptions} book.tex")
end
+
+ call_hook("hook_beforemakeindex")
+ system_or_raise("#{makeindex_command} #{makeindex_options} book") if @config["pdfmaker"]["makeindex"] && File.exist?("book.idx")
+ call_hook("hook_aftermakeindex")
+
+ system_or_raise("#{texcommand} #{texoptions} book.tex")
call_hook("hook_aftertexcompile")
if File.exist?("book.dvi")
system_or_raise("#{dvicommand} #{dvioptions} book.dvi")
+ call_hook("hook_afterdvipdf")
end
end
- call_hook("hook_afterdvipdf")
end
def generate_pdf(yamlfile)
@@ -245,6 +245,10 @@
<%- if @config["usepackage"] -%>
<%= @config["usepackage"] %>
<%- end -%>
+<%- if @config["makeindex"] -%>
+\usepackage{makeidx}
+\makeindex
+<%- end -%>
\begin{document}
@@ -316,6 +320,9 @@
<%- if @input_files["POSTDEF"] -%>
<%= @input_files["POSTDEF"] %>
<%- end -%>
+<%- if @config["pdfmaker"]["makeindex"] -%>
+\printindex
+<%- end -%>
%%% profile
<%- if @config["profile"] -%>
@@ -207,7 +207,25 @@ def test_inline_uchar
def test_inline_idx
actual = compile_inline("@<idx>{__TEST%$}, @<hidx>{__TEST%$}")
- assert_equal %Q|\\textunderscore{}\\textunderscore{}TEST\\%\\textdollar{}\\index{__TEST%$}, \\index{__TEST%$}|, actual
+ assert_equal %Q|\\textunderscore{}\\textunderscore{}TEST\\%\\textdollar{}\\index{__TEST%$@\\textunderscore{}\\textunderscore{}TEST\\%\\textdollar{}}, \\index{__TEST%$@\\textunderscore{}\\textunderscore{}TEST\\%\\textdollar{}}|, actual
+ end
+
+ def test_inline_idx_yomi
+ begin
+ require 'MeCab'
+ require 'nkf'
+ rescue LoadError
+ $stderr.puts "skip test_inline_idx_yomi (cannot find MeCab)"
+ return true
+ end
+ tmpdir = Dir.mktmpdir
+ File.write("#{tmpdir}/sample.dic", "強運\tはーどらっく\n")
+ @book.config["pdfmaker"]["makeindex"] = true
+ @book.config["pdfmaker"]["makeindex_dic"] = "#{tmpdir}/sample.dic"
+ @builder.setup_index
+ actual = compile_inline("@<hidx>{漢字}@<hidx>{強運}@<hidx>{項目@1<<>>項目@2}")
+ FileUtils.remove_entry_secure(tmpdir)
+ assert_equal %Q|\\index{かんじ@漢字}\\index{はーどらっく@強運}\\index{こうもく"@1@項目"@1!こうもく"@2@項目"@2}|, actual
end
def test_jis_x_0201_kana

0 comments on commit c0ed0e0

Please sign in to comment.