Skip to content

Commit

Permalink
Index implementation for pdfbuilder. ref #261
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Aug 28, 2016
1 parent eb68457 commit 42abfc8
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/review/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def self.values
"params" => "", # specify review2html parameters
"toclevel" => 3, # level of toc
"secnolevel" => 2, # level of section #
"makeindex" => nil, # Make index page
"epubversion" => 3,
"titlepage" => true, # Use title page
"toc" => nil, # Use table of contents in body
Expand Down Expand Up @@ -65,11 +66,20 @@ 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_command" => "mendex", # works only when makeindex is true
"makeindex_options" => "-f -r -I utf8",
"makeindex_sty" => nil, # only mendex
"makeindex_dic" => nil, # only mendex
"makeindex_mecab" => true, # only mendex
},
]
conf.maker = nil
conf
Expand Down
51 changes: 39 additions & 12 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,32 @@ def builder_init_file
@table_caption = nil
@ol_num = nil
@sec_counter = SecCounter.new(5, @chapter)
@index_db = {}
@index_mecab = nil
if @book.config["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("-Oyomi")
rescue LoadError
end
end
end
initialize_metachars(@book.config["texcommand"])
end
private :builder_init_file

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
Expand Down Expand Up @@ -884,21 +906,26 @@ 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)
# too mendex specific...
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
item = "#{escape_index(item)}@#{_item}"
end
else
yomi = NKF.nkf("-w --hiragana", @index_mecab.parse(item).force_encoding("UTF-8").chomp)
item = 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)
Expand Down
32 changes: 31 additions & 1 deletion lib/review/pdfmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,45 @@ def generate_pdf(yamlfile)
## do compile
if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0
warn "command configuration is prohibited in safe mode. ignored."
# revert to default
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

# too mendex specific...
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

if File.exist?("book.idx")
system_or_raise("#{makeindex_command} #{makeindex_options} book")
end

system_or_raise("#{texcommand} #{texoptions} book.tex")
call_hook("hook_aftertexcompile")

if File.exist?("book.dvi")
Expand Down
7 changes: 7 additions & 0 deletions templates/latex/layout.tex.erb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@
<%- if @config["usepackage"] -%>
<%= @config["usepackage"] %>
<%- end -%>
<%- if @config["makeindex"] -%>
\usepackage{makeidx}
\makeindex
<%- end -%>

\begin{document}

Expand Down Expand Up @@ -307,6 +311,9 @@
<%- if @input_files["POSTDEF"] -%>
<%= @input_files["POSTDEF"] %>
<%- end -%>
<%- if @config["makeindex"] -%>
\printindex
<%- end -%>

%%% profile
<%- if @config["profile"] -%>
Expand Down
2 changes: 1 addition & 1 deletion test/test_latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ 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_jis_x_0201_kana
Expand Down

0 comments on commit 42abfc8

Please sign in to comment.