Skip to content

Commit

Permalink
refactoring for rubocop.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Sep 24, 2017
1 parent b2f2aa5 commit 0a077db
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 158 deletions.
59 changes: 28 additions & 31 deletions lib/review/book/compilable.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#
# $Id: book.rb 4315 2009-09-02 04:15:24Z kmuto $
#
# Copyright (c) 2002-2008 Minero Aoki
# 2009-2017 Minero Aoki, Kenshi Muto
# Copyright (c) 2009-2017 Minero Aoki, Kenshi Muto
# 2002-2008 Minero Aoki
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
# For details of the GNU LGPL, see the file "COPYING".
#
require 'review/textutils'

module ReVIEW
module Book
module Compilable
Expand Down Expand Up @@ -42,7 +40,7 @@ def title
return @title if @title

@title = ''
return @title if !content
return @title unless content
content.each_line do |line|
if line =~ /\A=+/
@title = line.sub(/\A=+(\[.+?\])?(\{.+?\})?/, '').strip
Expand All @@ -57,16 +55,16 @@ def size
end

def volume
if !@volume
@volume = Volume.count_file(path())
unless @volume
@volume = Volume.count_file(path)
@volume.page_per_kbyte = @book.page_metric.page_per_kbyte
end
@volume
end

# deprecated; use content()
def open(&block)
return (block_given?() ? yield(@io) : @io) if @io
def open(&_block)
return (block_given? ? yield(@io) : @io) if @io
StringIO.new(content)
end

Expand All @@ -78,73 +76,73 @@ def content

def lines
# FIXME: we cannot duplicate Enumerator on ruby 1.9 HEAD
(@lines ||= content().lines.to_a).dup
(@lines ||= content.lines.to_a).dup
end

def list(id)
list_index()[id]
list_index[id]
end

def list_index
@list_index ||= ListIndex.parse(lines())
@list_index ||= ListIndex.parse(lines)
@list_index
end

def table(id)
table_index()[id]
table_index[id]
end

def table_index
@table_index ||= TableIndex.parse(lines())
@table_index ||= TableIndex.parse(lines)
@table_index
end

def footnote(id)
footnote_index()[id]
footnote_index[id]
end

def footnote_index
@footnote_index ||= FootnoteIndex.parse(lines())
@footnote_index ||= FootnoteIndex.parse(lines)
@footnote_index
end

def image(id)
return image_index()[id] if image_index().key?(id)
return icon_index()[id] if icon_index().key?(id)
return numberless_image_index()[id] if numberless_image_index().key?(id)
indepimage_index()[id]
return image_index[id] if image_index.key?(id)
return icon_index[id] if icon_index.key?(id)
return numberless_image_index[id] if numberless_image_index.key?(id)
indepimage_index[id]
end

def numberless_image_index
@numberless_image_index ||=
NumberlessImageIndex.parse(lines(), id(),
NumberlessImageIndex.parse(lines, id,
"#{book.basedir}/#{@book.config['imagedir']}",
@book.image_types, @book.config['builder'])
end

def image_index
@image_index ||= ImageIndex.parse(lines(), id(),
@image_index ||= ImageIndex.parse(lines, id,
"#{book.basedir}/#{@book.config['imagedir']}",
@book.image_types, @book.config['builder'])
@image_index
end

def icon_index
@icon_index ||= IconIndex.parse(lines(), id(),
@icon_index ||= IconIndex.parse(lines, id,
"#{book.basedir}/#{@book.config['imagedir']}",
@book.image_types, @book.config['builder'])
@icon_index
end

def indepimage_index
@indepimage_index ||=
IndepImageIndex.parse(lines(), id(),
IndepImageIndex.parse(lines, id,
"#{book.basedir}/#{@book.config['imagedir']}",
@book.image_types, @book.config['builder'])
end

def bibpaper(id)
bibpaper_index()[id]
bibpaper_index[id]
end

def bibpaper_index
Expand All @@ -154,19 +152,19 @@ def bibpaper_index
end

def headline(caption)
headline_index()[caption]
headline_index[caption]
end

def headline_index
@headline_index ||= HeadlineIndex.parse(lines(), self)
@headline_index ||= HeadlineIndex.parse(lines, self)
end

def column(id)
column_index()[id]
column_index[id]
end

def column_index
@column_index ||= ColumnIndex.parse(lines())
@column_index ||= ColumnIndex.parse(lines)
end

def next_chapter
Expand All @@ -179,4 +177,3 @@ def prev_chapter
end
end
end

26 changes: 10 additions & 16 deletions lib/review/book/part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@ def initialize(book, number, chapters, name = '', io = nil)
@chapters = chapters
@name = name ? File.basename(name, '.re') : nil
@path = name
@content =
if io
io.read
elsif @path && File.exist?(@path)
File.read(@path, mode: 'r:BOM|utf-8')
else
nil
end
@title =
if file?
nil
else
name
end
@content = nil
if io
@content = io.read
elsif @path && File.exist?(@path)
@content = File.read(@path, mode: 'r:BOM|utf-8')
end
@title = name
@title = nil if file?
@volume = nil
end

Expand All @@ -47,7 +41,7 @@ def each_chapter(&block)
end

def volume
vol = Volume.sum(@chapters.map { |chap| chap.volume })
vol = Volume.sum(@chapters.map(&:volume))
vol.page_per_kbyte = @book.page_metric.page_per_kbyte
vol
end
Expand All @@ -58,7 +52,7 @@ def file?

def format_number(heading = true)
if heading
"#{I18n.t('part', @number)}"
I18n.t('part', @number)
else
@number.to_s
end
Expand Down
22 changes: 9 additions & 13 deletions lib/review/book/volume.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#
# $Id: volume.rb 3883 2008-02-10 11:48:23Z aamine $
#
# Copyright (c) 2002-2007 Minero Aoki
# Copyright (c) 2007-2017 Minero Aoki, Kenshi Muto
# 2002-2007 Minero Aoki
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand All @@ -11,11 +9,10 @@
module ReVIEW
module Book
class Volume

def Volume.count_file(path)
def self.count_file(path)
b = c = l = 0
File.foreach(path) do |line|
next if %r<\A\#@> =~ line
next if /\A\#@/ =~ line
text = line.gsub(/\s+/, '')
b += text.bytesize
c += text.charsize
Expand All @@ -24,11 +21,11 @@ def Volume.count_file(path)
new(b, c, l)
end

def Volume.sum(vols)
vols.inject(new()) {|sum, i| sum + i }
def self.sum(vols)
vols.inject(new) { |sum, i| sum + i }
end

def Volume.dummy
def self.dummy
new(-1, -1, -1)
end

Expand All @@ -49,19 +46,18 @@ def kbytes
end

def page
(kbytes.to_f/@page_per_kbyte).ceil
(kbytes.to_f / @page_per_kbyte).ceil
end

def to_s
"#{kbytes()}KB #{@chars}C #{@lines}L #{page()}P"
"#{kbytes}KB #{@chars}C #{@lines}L #{page}P"
end

def +(other)
Volume.new(@bytes + other.bytes,
@chars + other.chars,
@lines + other.lines)
end

end
end
end
2 changes: 1 addition & 1 deletion lib/review/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.values
'ill' => nil, # Illustrator
'pht' => nil, # Photographer
'trl' => nil, # Translator
'date' => nil, # publishing date
'date' => Time.now.strftime('%Y-%m-%d'), # publishing date
'rights' => nil, # Copyright messages
'description' => nil, # Description
'urnid' => "urn:uid:#{SecureRandom.uuid}", # Identifier
Expand Down
Loading

0 comments on commit 0a077db

Please sign in to comment.