Class: TxtBook

Inherits:
Object show all
Includes:
HeaderDetect
Defined in:
lib/txt_book.rb

Overview

文本书籍

文本书籍现状

解决办法

问题1: 不包含目录结构

问题2: 包含目录结构,同时列出目录

问题3: 目录结构信息是以节等组成

Constant Summary

Constants included from HeaderDetect

HeaderDetect::HEAD_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HeaderDetect

#guess_appendix?, #guess_chapter?, #guess_digital_header?, #guess_digital_section?, #guess_glossary?, #guess_header?, #guess_index?, #guess_part?, #guess_preface?, #guess_section?, #guess_volume?, #hav_complete_sentence?, #valid_title?

Constructor Details

#initialize(content, options = {}) ⇒ TxtBook

Returns a new instance of TxtBook.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/txt_book.rb', line 56

def initialize(content,options={})
  @title = options[:title]
  @author = options[:author]
  @publisher = options[:publisher]
  @pubdate= options[:pubdate]
  @isbn = options[:isbn]
  @format = options[:format]

  unless Utils.detect_utf8(content)
    content = Utils.to_utf8(content) 
  end

  @content = preprocess_content(content)
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



47
48
49
# File 'lib/txt_book.rb', line 47

def author
  @author
end

#contentObject (readonly)

Returns the value of attribute content.



47
48
49
# File 'lib/txt_book.rb', line 47

def content
  @content
end

#isbnObject (readonly)

Returns the value of attribute isbn.



47
48
49
# File 'lib/txt_book.rb', line 47

def isbn
  @isbn
end

#pubdateObject (readonly)

Returns the value of attribute pubdate.



47
48
49
# File 'lib/txt_book.rb', line 47

def pubdate
  @pubdate
end

#publisherObject (readonly)

Returns the value of attribute publisher.



47
48
49
# File 'lib/txt_book.rb', line 47

def publisher
  @publisher
end

#titleObject (readonly)

Returns the value of attribute title.



47
48
49
# File 'lib/txt_book.rb', line 47

def title
  @title
end

Class Method Details

.load(filename, options = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/txt_book.rb', line 49

def self.load(filename,options={})
  raise '无效的文件' unless File.exists?(filename)
  options[:title] = File.basename(filename, File.extname(filename))
  content = File.open(filename).read
  new(content,options)
end

Instance Method Details

#breaklinesObject



81
82
83
# File 'lib/txt_book.rb', line 81

def breaklines
  @breaklines ||= Utils.breaklines(content)
end

#breaklines_countObject



85
86
87
# File 'lib/txt_book.rb', line 85

def breaklines_count
  breaklines.count
end

#struct_contentObject



71
72
73
74
75
76
77
78
79
# File 'lib/txt_book.rb', line 71

def struct_content
  return @struct_content if @struct_content 
  content = if breaklines_count > 100
              Utils.fixed_page_break(@content)
            else
              @content
            end
  @struct_content = extract_book_struct(content,:format=>@format)
end

#to_doc_bookObject



101
102
103
104
105
106
107
# File 'lib/txt_book.rb', line 101

def to_doc_book
  if struct_content
    build_doc_book(struct_content,{:title=>title,:publisher=>publisher,:pubdate=>pubdate,:author=>author,:isbn=>isbn})    
  else
    build_doc_book(content,{:title=>title,:publisher=>publisher,:pubdate=>pubdate,:author=>author,:isbn=>isbn})
  end
end

#tocObject



89
90
91
# File 'lib/txt_book.rb', line 89

def toc
  @toc ||= extract_toc_from_struct(struct_content) if struct_content
end

#toc_to_textObject



93
94
95
96
97
98
99
# File 'lib/txt_book.rb', line 93

def toc_to_text
  if toc
    gen_toc(toc) do |item,children|
      "#{item[:title]}\n#{children}"
    end
  end
end