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

EPUBMaker.Contentのリファクタリング #1617

Merged
merged 6 commits into from
Dec 21, 2020
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
10 changes: 5 additions & 5 deletions lib/epubmaker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# = epubmaker.rb -- EPUB production set.
#
# Copyright (c) 2010-2017 Kenshi Muto
# Copyright (c) 2010-2020 Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand All @@ -10,11 +10,11 @@
# == Quick usage
# require 'epubmaker'
# producer = EPUBMaker::Producer.new
# config = producer.load("config.yml")
# producer.contents.push(EPUBMaker::Content.new({"file" => "ch01.xhtml"}))
# producer.contents.push(EPUBMaker::Content.new({"file" => "ch02.xhtml"}))
# config = producer.load('config.yml')
# producer.contents.push(EPUBMaker::Content.new(file: 'ch01.xhtml'))
# producer.contents.push(EPUBMaker::Content.new(file: 'ch02.xhtml'))
# ...
# producer.import_imageinfo("images")
# producer.import_imageinfo('images')
# producer.produce

require 'epubmaker/producer'
Expand Down
37 changes: 12 additions & 25 deletions lib/epubmaker/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,18 @@ def inspect
end

# :call-seq:
# initialize(file, id, media, title, level, notoc)
# initialize(hash)
# Construct Content object by passing a sequence of parameters or hash.
# Keys of +hash+ relate with each parameters.
# +file+ (or +hash+["file"]) is required. Others are optional.
def initialize(fileorhash, id = nil, media = nil, title = nil, level = nil, notoc = nil, properties = nil, chaptype = nil) # rubocop:disable Metrics/ParameterLists
if fileorhash.instance_of?(Hash)
@id = fileorhash['id']
@file = fileorhash['file']
@media = fileorhash['media']
@title = fileorhash['title']
@level = fileorhash['level']
@notoc = fileorhash['notoc']
@properties = fileorhash['properties'] || []
@chaptype = fileorhash['chaptype']
else
@file = fileorhash
@id = id
@media = media
@title = title
@level = level
@notoc = notoc
@properties = properties || []
@chaptype = chaptype
end
# initialize(params)
# Construct Content object by passing named parameters.
# +params+[:file] is required. Others are optional.
def initialize(file:, id: nil, media: nil, title: nil, level: nil, notoc: nil, properties: nil, chaptype: nil)
@id = id
@file = file
@media = media
@title = title
@level = level
@notoc = notoc
@properties = properties || []
@chaptype = chaptype
complement
end

Expand Down
4 changes: 2 additions & 2 deletions lib/epubmaker/producer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ def import_imageinfo(path, base = nil, allow_exts = nil)
if f =~ /\.(#{allow_exts.join('|')})\Z/i
path.chop! if path =~ %r{/\Z}
if base.nil?
@contents.push(EPUBMaker::Content.new('file' => "#{path}/#{f}"))
@contents.push(EPUBMaker::Content.new(file: "#{path}/#{f}"))
else
@contents.push(EPUBMaker::Content.new('file' => "#{path.sub(base + '/', '')}/#{f}"))
@contents.push(EPUBMaker::Content.new(file: "#{path.sub(base + '/', '')}/#{f}"))
end
end
if FileTest.directory?("#{path}/#{f}")
Expand Down
18 changes: 9 additions & 9 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,20 +482,20 @@ def push_contents(_basetmpdir)
next if level.to_i > @config['toclevel'] && args[:force_include].nil?
log("Push #{file} to ePUB contents.")

hash = { 'file' => file,
'level' => level.to_i,
'title' => title,
'chaptype' => args[:chaptype] }
params = { file: file,
level: level.to_i,
title: title,
chaptype: args[:chaptype] }
if args[:id].present?
hash['id'] = args[:id]
params[:id] = args[:id]
end
if args[:properties].present?
hash['properties'] = args[:properties].split(' ') # rubocop:disable Style/RedundantArgument
params[:properties] = args[:properties].split(' ') # rubocop:disable Style/RedundantArgument
end
if args[:notoc].present?
hash['notoc'] = args[:notoc]
params[:notoc] = args[:notoc]
end
@producer.contents.push(Content.new(hash))
@producer.contents.push(Content.new(**params))
end
end

Expand All @@ -506,7 +506,7 @@ def copy_stylesheet(basetmpdir)
error "#{sfile} is not found."
end
FileUtils.cp(sfile, basetmpdir)
@producer.contents.push(Content.new('file' => sfile))
@producer.contents.push(Content.new(file: sfile))
end
end

Expand Down
88 changes: 44 additions & 44 deletions test/test_epub3maker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,16 @@ def test_stage1_ncx

def stage2
# add one item
@producer.contents << Content.new({ 'file' => 'ch01.html', 'title' => 'CH01', 'level' => 1 })
@producer.contents << Content.new(file: 'ch01.html', title: 'CH01', level: 1)
end

def test_stage2_add_l1item
stage2
expect = EPUBMaker::Content.new('ch01.html',
'ch01-html',
'application/xhtml+xml',
'CH01',
1)
expect = EPUBMaker::Content.new(file: 'ch01.html',
id: 'ch01-html',
media: 'application/xhtml+xml',
title: 'CH01',
level: 1)
assert_equal expect, @producer.contents[0]
end

Expand Down Expand Up @@ -251,49 +251,49 @@ def test_stage2_ncx

def stage3
# add more items
@producer.contents << Content.new({ 'file' => 'ch01.html', 'title' => %Q(CH01<>&"), 'level' => 1 })
@producer.contents << Content.new({ 'file' => 'ch02.html', 'title' => 'CH02', 'level' => 1 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S1', 'title' => 'CH02.1', 'level' => 2 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S1.1', 'title' => 'CH02.1.1', 'level' => 3 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S1.1.1', 'title' => 'CH02.1.1.1', 'level' => 4 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S1.1.1.1', 'title' => 'CH02.1.1.1.1', 'level' => 5 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S1.1.2', 'title' => 'CH02.1.1.2', 'level' => 4 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S2', 'title' => 'CH02.2', 'level' => 2 })
@producer.contents << Content.new({ 'file' => 'ch02.html#S2.1', 'title' => 'CH02.2.1', 'level' => 3 })
@producer.contents << Content.new({ 'file' => 'ch03.html', 'title' => 'CH03', 'level' => 1, 'properties' => ['mathml'] })
@producer.contents << Content.new({ 'file' => 'ch03.html#S1', 'title' => 'CH03.1', 'level' => 2 })
@producer.contents << Content.new({ 'file' => 'ch03.html#S1.1', 'title' => 'CH03.1.1', 'level' => 3 })
@producer.contents << Content.new({ 'file' => 'ch04.html', 'title' => 'CH04', 'level' => 1 })
@producer.contents << Content.new({ 'file' => 'sample.png' })
@producer.contents << Content.new({ 'file' => 'sample.jpg' })
@producer.contents << Content.new({ 'file' => 'sample.JPEG' })
@producer.contents << Content.new({ 'file' => 'sample.SvG' })
@producer.contents << Content.new({ 'file' => 'sample.GIF' })
@producer.contents << Content.new({ 'file' => 'sample.css' })
@producer.contents << Content.new(file: 'ch01.html', title: %Q(CH01<>&"), level: 1)
@producer.contents << Content.new(file: 'ch02.html', title: 'CH02', level: 1)
@producer.contents << Content.new(file: 'ch02.html#S1', title: 'CH02.1', level: 2)
@producer.contents << Content.new(file: 'ch02.html#S1.1', title: 'CH02.1.1', level: 3)
@producer.contents << Content.new(file: 'ch02.html#S1.1.1', title: 'CH02.1.1.1', level: 4)
@producer.contents << Content.new(file: 'ch02.html#S1.1.1.1', title: 'CH02.1.1.1.1', level: 5)
@producer.contents << Content.new(file: 'ch02.html#S1.1.2', title: 'CH02.1.1.2', level: 4)
@producer.contents << Content.new(file: 'ch02.html#S2', title: 'CH02.2', level: 2)
@producer.contents << Content.new(file: 'ch02.html#S2.1', title: 'CH02.2.1', level: 3)
@producer.contents << Content.new(file: 'ch03.html', title: 'CH03', level: 1, properties: ['mathml'])
@producer.contents << Content.new(file: 'ch03.html#S1', title: 'CH03.1', level: 2)
@producer.contents << Content.new(file: 'ch03.html#S1.1', title: 'CH03.1.1', level: 3)
@producer.contents << Content.new(file: 'ch04.html', title: 'CH04', level: 1)
@producer.contents << Content.new(file: 'sample.png')
@producer.contents << Content.new(file: 'sample.jpg')
@producer.contents << Content.new(file: 'sample.JPEG')
@producer.contents << Content.new(file: 'sample.SvG')
@producer.contents << Content.new(file: 'sample.GIF')
@producer.contents << Content.new(file: 'sample.css')
end

def test_stage3_add_various_items
stage3
expect = [
Content.new('ch01.html', 'ch01-html', 'application/xhtml+xml', %Q(CH01<>&"), 1),
Content.new('ch02.html', 'ch02-html', 'application/xhtml+xml', 'CH02', 1),
Content.new('ch02.html#S1', 'ch02-html#S1', 'html#s1', 'CH02.1', 2),
Content.new('ch02.html#S1.1', 'ch02-html#S1-1', '1', 'CH02.1.1', 3),
Content.new('ch02.html#S1.1.1', 'ch02-html#S1-1-1', '1', 'CH02.1.1.1', 4),
Content.new('ch02.html#S1.1.1.1', 'ch02-html#S1-1-1-1', '1', 'CH02.1.1.1.1', 5),
Content.new('ch02.html#S1.1.2', 'ch02-html#S1-1-2', '2', 'CH02.1.1.2', 4),
Content.new('ch02.html#S2', 'ch02-html#S2', 'html#s2', 'CH02.2', 2),
Content.new('ch02.html#S2.1', 'ch02-html#S2-1', '1', 'CH02.2.1', 3),
Content.new('ch03.html', 'ch03-html', 'application/xhtml+xml', 'CH03', 1, nil, ['mathml']),
Content.new('ch03.html#S1', 'ch03-html#S1', 'html#s1', 'CH03.1', 2),
Content.new('ch03.html#S1.1', 'ch03-html#S1-1', '1', 'CH03.1.1', 3),
Content.new('ch04.html', 'ch04-html', 'application/xhtml+xml', 'CH04', 1),
Content.new('sample.png', 'sample-png', 'image/png'),
Content.new('sample.jpg', 'sample-jpg', 'image/jpeg'),
Content.new('sample.JPEG', 'sample-JPEG', 'image/jpeg'),
Content.new('sample.SvG', 'sample-SvG', 'image/svg+xml'),
Content.new('sample.GIF', 'sample-GIF', 'image/gif'),
Content.new('sample.css', 'sample-css', 'text/css')
Content.new(file: 'ch01.html', id: 'ch01-html', media: 'application/xhtml+xml', title: %Q(CH01<>&"), level: 1),
Content.new(file: 'ch02.html', id: 'ch02-html', media: 'application/xhtml+xml', title: 'CH02', level: 1),
Content.new(file: 'ch02.html#S1', id: 'ch02-html#S1', media: 'html#s1', title: 'CH02.1', level: 2),
Content.new(file: 'ch02.html#S1.1', id: 'ch02-html#S1-1', media: '1', title: 'CH02.1.1', level: 3),
Content.new(file: 'ch02.html#S1.1.1', id: 'ch02-html#S1-1-1', media: '1', title: 'CH02.1.1.1', level: 4),
Content.new(file: 'ch02.html#S1.1.1.1', id: 'ch02-html#S1-1-1-1', media: '1', title: 'CH02.1.1.1.1', level: 5),
Content.new(file: 'ch02.html#S1.1.2', id: 'ch02-html#S1-1-2', media: '2', title: 'CH02.1.1.2', level: 4),
Content.new(file: 'ch02.html#S2', id: 'ch02-html#S2', media: 'html#s2', title: 'CH02.2', level: 2),
Content.new(file: 'ch02.html#S2.1', id: 'ch02-html#S2-1', media: '1', title: 'CH02.2.1', level: 3),
Content.new(file: 'ch03.html', id: 'ch03-html', media: 'application/xhtml+xml', title: 'CH03', level: 1, properties: ['mathml']),
Content.new(file: 'ch03.html#S1', id: 'ch03-html#S1', media: 'html#s1', title: 'CH03.1', level: 2),
Content.new(file: 'ch03.html#S1.1', id: 'ch03-html#S1-1', media: '1', title: 'CH03.1.1', level: 3),
Content.new(file: 'ch04.html', id: 'ch04-html', media: 'application/xhtml+xml', title: 'CH04', level: 1),
Content.new(file: 'sample.png', id: 'sample-png', media: 'image/png'),
Content.new(file: 'sample.jpg', id: 'sample-jpg', media: 'image/jpeg'),
Content.new(file: 'sample.JPEG', id: 'sample-JPEG', media: 'image/jpeg'),
Content.new(file: 'sample.SvG', id: 'sample-SvG', media: 'image/svg+xml'),
Content.new(file: 'sample.GIF', id: 'sample-GIF', media: 'image/gif'),
Content.new(file: 'sample.css', id: 'sample-css', media: 'text/css')
]

assert_equal expect, @producer.contents
Expand Down
Loading