aozoragen 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,5 +2,6 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ vendor/*
5
6
  *.txt
6
7
  *.pdf
data/README.md CHANGED
@@ -12,7 +12,7 @@ Gemを使ってインストールする:
12
12
 
13
13
  % aozoragen <URL>
14
14
 
15
- `URL`には日本語の小説をHTML形式で配布しているサイトの目次ページを指定する。カレントディレクトリに章ごとのテキストファイル(拡張子.txt)を生成する。ファイル名はサイトごとに自動的に決定され、`hoge.NN.txt` (NNは数値)のような形式となる。これらのファイルを連結すると一冊の本になる。
15
+ `URL`には日本語の小説をHTML形式で配布しているサイトの目次ページを指定する。カレントディレクトリに章ごとのテキストファイル(拡張子.txt)を生成する。ファイル名はサイトごとに自動的に決定され、`hoge.NN.txt` (NNは連番数値またはその他の文字列)のような形式となる。これらのファイルを連結すると一冊の本になる。
16
16
 
17
17
  `aozoragen`コマンドが現在対応しているのは以下のサイト:
18
18
 
@@ -21,6 +21,11 @@ Gemを使ってインストールする:
21
21
  * 実行時に無償公開中の章のみが抽出される。
22
22
  * レンザブロー <http://renzaburo.jp/>
23
23
  * 指定例: http://renzaburo.jp/contents_t/061-katano/index.html
24
+ * Webミステリーズ! <http://www.webmysteries.jp/>
25
+ * Webミステリーズ!の掲載作品には目次ページがないため、GitHubのWikiで代用する。
26
+ * h1要素に書名、h2要素に続くリストで著者名、h3要素に続くリストで連載各回のURLを表現する。
27
+ * WikiページのURLは、ファイル名が「webmisteries-」で始まるようにする。
28
+ * 指定例: https://github.com/tdtds/aozoragen/wiki/webmysteries-mm9_destruction
24
29
 
25
30
  ### aozora2pdf
26
31
  [青空キンドル](http://a2k.aill.org/)を使ってテキストをKindle向けPDFにする。パラメタにはaozoragenで生成した青空文庫形式のテキストファイルを順番通りに指定する。PDFは標準出力に出るので、リダイレクトする:
@@ -7,6 +7,7 @@
7
7
  # Distributed under GPL
8
8
  #
9
9
  require 'uri'
10
+ require 'pathname'
10
11
 
11
12
  ARGV.each do |u|
12
13
  uri = URI( u )
@@ -18,6 +19,12 @@ ARGV.each do |u|
18
19
  when 'renzaburo.jp'
19
20
  require 'aozoragen/renzaburo'
20
21
  book = Renzaburo::new( uri )
22
+ when 'github.com'
23
+ case Pathname( uri.path ).basename.to_s.sub( %r|(.*?)-.*$|, '\1' )
24
+ when 'webmysteries'
25
+ require 'aozoragen/webmysteries'
26
+ book = Webmysteries::new( uri )
27
+ end
21
28
  else
22
29
  p "Error: unknown URI: #{uri}"
23
30
  end
@@ -39,15 +39,18 @@ class Renzaburo
39
39
  (Nokogiri( html ) / 'div#mainContent' ).each do |content|
40
40
  (content / 'h3').each do |t|
41
41
  title = t.text.sub( /^『#{book_title}』 /, '' )
42
- text << "\n     #{title}\n\n"
42
+ text << "\n[#小見出し]#{title}[#小見出し終わり]\n\n"
43
43
  end
44
44
  (content / 'div.textBlock p' ).each do |para|
45
45
  next if /<次回につづく>/ =~ para.text
46
- text << ' ' * 10 if (para.attr('class') || '').index( 'txtAlignC' )
46
+ text << '[#10字下げ]' if (para.attr('class') || '').index( 'txtAlignC' )
47
47
  text << para.text.gsub( /<br>/, "\n" ) << "\n\n"
48
48
  end
49
49
  end
50
50
  text << "[#改ページ]\n"
51
- text.gsub( /</, '〈' ).gsub( />/, '〉' )
51
+ text.gsub( /</, '〈' ).gsub( />/, '〉' ).tr(
52
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
53
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
54
+ )
52
55
  end
53
56
  end
@@ -32,11 +32,11 @@ class SaiZenSen
32
32
  page.children.each do |section|
33
33
  case section.name
34
34
  when 'hgroup'
35
- text << "\n     #{detag section}\n\n"
35
+ text << "\n[#小見出し]#{detag section}[#小見出し終わり]\n\n"
36
36
  when 'div'
37
37
  case section.attr( 'class' )
38
38
  when /delimiter/
39
- text << ' ' * 5 << '─' * 10 << "\n\n"
39
+ text << "[#5字下げ]#{'─' * 10}\n\n"
40
40
  when /pgroup/
41
41
  (section / 'p').each do |paragraph|
42
42
  text << " #{detag paragraph}\n"
@@ -1,3 +1,3 @@
1
1
  module Aozoragen
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,68 @@
1
+ # -*- coding: utf-8; -*-
2
+ #
3
+ # scraping webmysteries.jp
4
+ #
5
+ require 'nokogiri'
6
+ require 'open-uri'
7
+ require 'pathname'
8
+ require 'cgi'
9
+
10
+ class Webmysteries
11
+ def initialize( index_uri )
12
+ @index_uri = URI( index_uri )
13
+ @index_html = Nokogiri( open( @index_uri, 'r:UTF-8', &:read ) )
14
+ end
15
+
16
+ def metainfo
17
+ info = {:id => Pathname( @index_uri.path ).basename.sub( %r|.*?-(.*)$|, '\1' ).to_s}
18
+ info[:title] = (@index_html / '#wiki-body h1')[0].text
19
+ (@index_html / '#wiki-body h2 + ul li' ).each do |li|
20
+ info[:author] = [li.text]
21
+ end
22
+ info
23
+ end
24
+
25
+ def each_chapter
26
+ (@index_html / '#wiki-body h3 + ul li a' ).each_with_index do |a, i|
27
+ uri = URI( a.attr( 'href' ) )
28
+ text = "\n"
29
+ each_pages( uri ) do |page|
30
+ text << page
31
+ end
32
+ yield( {id: '%02d' % (i+1), uri: uri, text: text} )
33
+ end
34
+ end
35
+
36
+ def each_pages( index )
37
+ begin
38
+ pages = []
39
+ html = Nokogiri( open( index, 'r', &:read ) )
40
+ (html / 'ul.pageNavi a').each do |a|
41
+ pages << a.attr( 'href' )
42
+ end
43
+ pages.shift # delete current page
44
+ begin
45
+ (html / 'noscript param[name="FlashVars"]')[0].attr( 'value' ).scan( /entry=(\d+)/ ) do |i|
46
+ yield get_text( i[0] )
47
+ end
48
+ end while html = Nokogiri( open( pages.shift, 'r', &:read ) )
49
+ rescue TypeError
50
+ # ignore open nil
51
+ end
52
+ end
53
+
54
+ def get_text( xml_id )
55
+ result = ''
56
+ open( "http://www.webmysteries.jp/entry_xml_data/#{xml_id}.xml" ) do |fx|
57
+ CGI::unescape( fx.read ).scan( %r|<entryBody>(.*?)</entryBody>|m ) do |entry|
58
+ result << entry[0].gsub( %r|<.*?>|m, "" )
59
+ end
60
+ end
61
+ result.
62
+ gsub( /^.*(つづく).*$/, '[#改ページ]' ).
63
+ gsub( /(?<=.)(([あ-ん]+))/, '《\1》' ).
64
+ gsub( /‐/, '─' ).
65
+ gsub( /\uFF0D/, '─' ).
66
+ gsub( /\n{3,}/m, "\n\n" )
67
+ end
68
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aozoragen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-24 00:00:00.000000000 Z
12
+ date: 2012-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &85482890 !ruby/object:Gem::Requirement
16
+ requirement: &81978730 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *85482890
24
+ version_requirements: *81978730
25
25
  description: Scraping some Ebook web site and generating AOZORA format text files.
26
26
  email:
27
27
  - t@tdtds.jp
@@ -42,6 +42,7 @@ files:
42
42
  - lib/aozoragen/renzaburo.rb
43
43
  - lib/aozoragen/sai-zen-sen.rb
44
44
  - lib/aozoragen/version.rb
45
+ - lib/aozoragen/webmysteries.rb
45
46
  homepage: https://github.com/tdtds/aozoragen
46
47
  licenses: []
47
48
  post_install_message: