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

簡易パースをしてnonumを採番スキップする #554

Merged
merged 5 commits into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 check_header == "nonum"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このcheck_headerってChapterのインスタンスを作るときに1回だけチェックしているようですが、[nonum]が章・節・小節等のいろんなところに出たり出なかったりする場合でもこのコードで問題ないんでしょうか…?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうか、だめですねこれでは…

だいぶ厄介な気がしてきましたぞ…

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いや、大丈夫っぽいです。
ここでやっていることは

  • ともかく先頭の見出しを探して(最初のレベルが1とは限らない)、
  • nonum, nodisp, notocだったらそのファイルは章として採番しない(章カウンタをスキップしたい)ので、このChapterオブジェクトのnumber参照時をnilにする

という目的です。

本文のほうは、HTMLBuilderであればheadline→sec_counterで採番していますが、nonumはnonum_beginに落ちてsec_counterを使わないので採番増加も起きません(ほかのno*も同様)。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名前をfind_first_header_option にでもするか

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、ここのロジックとしては理解できました。先頭が章レベルの見出しじゃない場合があるのが微妙な挙動につながるわけですね。うーむ。

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 check_header
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
5 changes: 4 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 Down
1 change: 1 addition & 0 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ 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)
Expand Down