Class: Aozoragen::Syosetu

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/aozoragen/syosetu.rb

Instance Method Summary collapse

Methods included from Util

#detag

Constructor Details

#initialize(index_uri) ⇒ Syosetu

Returns a new instance of Syosetu.



19
20
21
22
# File 'lib/aozoragen/syosetu.rb', line 19

def initialize(index_uri)
	@index_uri = index_uri
	@index_html = Nokogiri(open(@index_uri, 'r:utf-8', &:read))
end

Instance Method Details

#each_chapterObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/aozoragen/syosetu.rb', line 31

def each_chapter
	(@index_html / '.subtitle a').each do |a|
		uri = @index_uri + a.attr('href')

		chapter = Nokogiri(open(uri, 'r:utf-8', &:read).tr('《》.|', '『』・|'))
		text = get_chapter_text(chapter)
		chapter_id = '%03d' % Pathname(uri.path).basename.to_s.to_i
		yield({id: chapter_id, uri: uri, text: text})
	end
end

#get_chapter_text(chapter) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/aozoragen/syosetu.rb', line 42

def get_chapter_text(chapter)
	text = ''
	text << (chapter / '.novel_subtitle')[0].text.subhead
	(chapter / '#novel_honbun').each do |page|
		text << detag(page).gsub(/\n{2,5}/, "\n").gsub(/^ *◆$/, '[#10字下げ]◆')
		text << "[#改ページ]\n"
	end
	text.han2zen.for_tategaki.fix_aozora_notation
end

#metainfoObject



24
25
26
27
28
29
# File 'lib/aozoragen/syosetu.rb', line 24

def metainfo
	info = {:id => Pathname(@index_uri.path).basename.to_s, :author => []}
	info[:title] = (@index_html / 'title')[0].text
	info[:author] << (@index_html / '.novel_writername a')[0].text
	info
end