Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodispのサポート #555

Merged
merged 11 commits into from
Apr 17, 2016
2 changes: 1 addition & 1 deletion lib/epubmaker/epubv2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def opf_tocx

@producer.contents.each do |item|
next if item.media !~ /xhtml\+xml/ # skip non XHTML
s << %Q[ <itemref idref="#{item.id}"/>\n] if item.notoc.nil?
s << %Q[ <itemref idref="#{item.id}"/>\n]
end
s << %Q[ </spine>\n]
s
Expand Down
2 changes: 1 addition & 1 deletion lib/epubmaker/epubv3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def opf_tocx

@producer.contents.each do |item|
next if item.media !~ /xhtml\+xml/ # skip non XHTML
s << %Q[ <itemref idref="#{item.id}"/>\n] if item.notoc.nil?
s << %Q[ <itemref idref="#{item.id}"/>\n]
end
s << %Q[ </spine>\n]

Expand Down
12 changes: 8 additions & 4 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#
# $Id: book.rb 4315 2009-09-02 04:15:24Z kmuto $
#
# Copyright (c) 2002-2008 Minero Aoki
# 2009 Minero Aoki, Kenshi Muto
# 2009-2016 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -336,11 +334,17 @@ def parse_chapters
return catalog.parts_with_chaps.map do |entry|
if entry.is_a? Hash
chaps = entry.values.first.map do |chap|
Chapter.new(self, (num += 1), chap, "#{@basedir}/#{chap}")
chap = Chapter.new(self, (num += 1), chap, "#{@basedir}/#{chap}")
chap
end
Part.new(self, (part += 1), chaps, read_PART.split("\n")[part - 1])
else
chap = Chapter.new(self, (num += 1), entry, "#{@basedir}/#{entry}")
if chap.number
num = chap.number
else
num -= 1
end
Part.new(self, nil, [chap])
end
end
Expand Down
25 changes: 22 additions & 3 deletions lib/review/book/chapter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#
# $Id: book.rb 4315 2009-09-02 04:15:24Z kmuto $
#
# Copyright (c) 2002-2008 Minero Aoki
# 2009 Minero Aoki, Kenshi Muto
# 2009-2016 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -35,6 +33,7 @@ def initialize(book, number, name, path, io = nil)
end
if !@content && @path && File.exist?(@path)
@content = File.read(@path).sub(/\A\xEF\xBB\xBF/u, '')
@number = nil if ['nonum', 'nodisp', 'notoc'].include?(find_first_header_option)
end
@list_index = nil
@table_index = nil
Expand All @@ -48,11 +47,31 @@ def initialize(book, number, name, path, io = nil)
@volume = nil
end

def find_first_header_option
f = LineInput.new(Preprocessor::Strip.new(StringIO.new(@content)))
while f.next?
case f.peek
when /\A=+[\[\s\{]/
m = /\A(=+)(?:\[(.+?)\])?(?:\{(.+?)\})?(.*)/.match(f.gets)
return m[2] # tag
when %r</\A//[a-z]+/>
line = f.gets
if line.rstrip[-1,1] == "{"
f.until_match(%r<\A//\}>)
end
end
f.gets
end
nil
end

def inspect
"\#<#{self.class} #{@number} #{@path}>"
end

def format_number(heading = true)
return "" unless @number

if on_PREDEF?
return "#{@number}"
end
Expand Down
25 changes: 20 additions & 5 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ def build_body(basetmpdir, yamlfile)
@manifeststr = ""
@ncxstr = ""
@tocdesc = Array.new
# toccount = 2 ## not used

basedir = File.dirname(yamlfile)
base_path = Pathname.new(basedir)
Expand Down Expand Up @@ -297,13 +296,25 @@ def build_chap(chap, base_path, basetmpdir, ispart)
begin
@converter.convert(filename, File.join(basetmpdir, htmlfile))
write_info_body(basetmpdir, id, htmlfile, ispart, chaptype)
remove_hidden_title(basetmpdir, htmlfile)
rescue => e
@compile_errors = true
warn "compile error in #{filename} (#{e.class})"
warn e.message
end
end

def remove_hidden_title(basetmpdir, htmlfile)
File.open("#{basetmpdir}/#{htmlfile}", "r+") do |f|
body = f.read.
gsub(/<h\d .*?hidden=['"]true['"].*?>.*?<\/h\d>\n/, '').
gsub(/(<h\d .*?)\s*notoc=['"]true['"]\s*(.*?>.*?<\/h\d>\n)/, '\1\2')
f.rewind
f.print body
f.truncate(f.tell)
end
end

def detect_properties(path)
properties = []
File.open(path) do |f|
Expand All @@ -320,7 +331,6 @@ def detect_properties(path)

def write_info_body(basetmpdir, id, filename, ispart=nil, chaptype=nil)
headlines = []
# FIXME:nonumを修正する必要あり
path = File.join(basetmpdir, filename)
Document.parse_stream(File.new(path), ReVIEWHeaderListener.new(headlines))
properties = detect_properties(path)
Expand All @@ -332,9 +342,9 @@ def write_info_body(basetmpdir, id, filename, ispart=nil, chaptype=nil)
headlines.each do |headline|
headline["level"] = 0 if ispart.present? && headline["level"] == 1
if first.nil?
@htmltoc.add_item(headline["level"], filename+"#"+headline["id"], headline["title"], {:chaptype => chaptype})
@htmltoc.add_item(headline["level"], filename+"#"+headline["id"], headline["title"], {:chaptype => chaptype, :notoc => headline["notoc"]})
else
@htmltoc.add_item(headline["level"], filename, headline["title"], {:force_include => true, :chaptype => chaptype+prop_str})
@htmltoc.add_item(headline["level"], filename, headline["title"], {:force_include => true, :chaptype => chaptype+prop_str, :notoc => headline["notoc"]})
first = nil
end
end
Expand All @@ -352,6 +362,9 @@ def push_contents(basetmpdir)
if args[:properties].present?
hash["properties"] = args[:properties].split(" ")
end
if args[:notoc].present?
hash["notoc"] = args[:notoc]
end
@producer.contents.push(Content.new(hash))
end
end
Expand Down Expand Up @@ -458,6 +471,7 @@ def tag_start(name, attrs)
end
@level = $1.to_i
@id = attrs["id"] if attrs["id"].present?
@notoc = attrs["notoc"] if attrs["notoc"].present?
elsif !@level.nil?
if name == "img" && attrs["alt"].present?
@content << attrs["alt"]
Expand All @@ -469,10 +483,11 @@ def tag_start(name, attrs)

def tag_end(name)
if name =~ /\Ah\d+/
@headlines.push({"level" => @level, "id" => @id, "title" => @content}) if @id.present?
@headlines.push({"level" => @level, "id" => @id, "title" => @content, "notoc" => @notoc}) if @id.present?
@content = ""
@level = nil
@id = nil
@notoc = nil
end
end

Expand Down
37 changes: 36 additions & 1 deletion lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def builder_init_file
@chapter.book.image_types = %w( .png .jpg .jpeg .gif .svg )
@column = 0
@sec_counter = SecCounter.new(5, @chapter)
@nonum_counter = 0
end
private :builder_init_file

Expand Down Expand Up @@ -194,10 +195,12 @@ def headline(level, label, caption)
end

def nonum_begin(level, label, caption)
@nonum_counter += 1
puts '' if level > 1
unless caption.empty?
if label.nil?
puts %Q[<h#{level}>#{compile_inline(caption)}</h#{level}>]
id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}")
puts %Q[<h#{level} id="#{id}">#{compile_inline(caption)}</h#{level}>]
else
puts %Q[<h#{level} id="#{normalize_id(label)}">#{compile_inline(caption)}</h#{level}>]
end
Expand All @@ -207,6 +210,38 @@ def nonum_begin(level, label, caption)
def nonum_end(level)
end

def notoc_begin(level, label, caption)
@nonum_counter += 1
puts '' if level > 1
unless caption.empty?
if label.nil?
id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}")
puts %Q[<h#{level} id="#{id}" notoc="true">#{compile_inline(caption)}</h#{level}>]
else
puts %Q[<h#{level} id="#{normalize_id(label)}" notoc="true">#{compile_inline(caption)}</h#{level}>]
end
end
end

def notoc_end(level)
end

def nodisp_begin(level, label, caption)
@nonum_counter += 1
puts '' if level > 1
unless caption.empty?
if label.nil?
id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}")
puts %Q[<a id="#{id}" /><h#{level} id="#{id}" hidden="true">#{compile_inline(caption)}</h#{level}>]
else
puts %Q[<a id="#{normalize_id(label)}" /><h#{level} id="#{normalize_id(label)}" hidden="true">#{compile_inline(caption)}</h#{level}>]
end
end
end

def nodisp_end(level)
end

def column_begin(level, label, caption)
puts %Q[<div class="column">]

Expand Down
2 changes: 1 addition & 1 deletion lib/review/htmltoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def decode_args(args_str)
end

def encode_args(args)
args.map{|k,v| "#{k}=#{v}"}.join(",")
args.delete_if {|k,v| v.nil? }.map{|k,v| "#{k}=#{v}"}.join(",")
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,30 @@ def headline(level, label, caption)
def nonum_begin(level, label, caption)
blank unless @output.pos == 0
puts macro(HEADLINE[level]+"*", compile_inline(caption))
puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption))
end

def nonum_end(level)
end

def notoc_begin(level, label, caption)
blank unless @output.pos == 0
puts macro(HEADLINE[level]+"*", compile_inline(caption))
end

def notoc_end(level)
end

def nodisp_begin(level, label, caption)
blank unless @output.pos == 0
puts macro('clearpage') if @output.pos == 0
puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption))
# FIXME: headings
end

def nodisp_end(level)
end

def column_begin(level, label, caption)
blank
puts "\\begin{reviewcolumn}\n"
Expand Down