Class: Epub

Inherits:
Object
  • Object
show all
Defined in:
lib/link2epub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf_file) ⇒ Epub

Returns a new instance of Epub.



47
48
49
50
51
52
53
# File 'lib/link2epub.rb', line 47

def initialize(conf_file)
    @conf_file = conf_file
    @conf = YAML::load(File.read(@conf_file))
    @links = Links.new(@conf['links_file']).to_a
    @articles = []
    @titles = []
end

Instance Attribute Details

#articlesObject

Returns the value of attribute articles.



45
46
47
# File 'lib/link2epub.rb', line 45

def articles
  @articles
end

#confObject

Returns the value of attribute conf.



45
46
47
# File 'lib/link2epub.rb', line 45

def conf
  @conf
end

#conf_fileObject

Returns the value of attribute conf_file.



45
46
47
# File 'lib/link2epub.rb', line 45

def conf_file
  @conf_file
end

Returns the value of attribute links.



45
46
47
# File 'lib/link2epub.rb', line 45

def links
  @links
end

#titlesObject

Returns the value of attribute titles.



45
46
47
# File 'lib/link2epub.rb', line 45

def titles
  @titles
end

Instance Method Details

#cleanObject



126
127
128
129
130
131
132
# File 'lib/link2epub.rb', line 126

def clean
    FileUtils.rm('./nav.html')
    FileUtils.rm('./index.html')
    @articles.each do |a|
        FileUtils.rm(a.file)
    end
end

#createFile(tpl, file) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/link2epub.rb', line 76

def createFile(tpl, file)
    eruby = Erubis::Eruby.new(File.read(tpl))

    File.open(file, 'w') do |f|
        f.puts eruby.evaluate(self)
    end
end

#getArticlesObject



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

def getArticles
    @links.each_with_index do |l, i|
        art = Article.new(l)
        art.tpl = "../tpl/article.tpl"
        art.file = "./file#{i+1}.html"
        art.content = Nokogiri::HTML(open(art.link)).css('.post .content').to_html
        art.title = Nokogiri::HTML(open(art.link)).css('.post h2').text
        art.author = Nokogiri::HTML(open(art.link)).css('.post .author').text
        art.date = Nokogiri::HTML(open(art.link)).css('.post .date').text
        art.to_file
        @articles.push(art)
    end
end

#getTitlesObject



69
70
71
72
73
74
# File 'lib/link2epub.rb', line 69

def getTitles
    @articles.each do |a|
        @titles.push(a.title)
    end
    @titles
end

#to_epubObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/link2epub.rb', line 90

def to_epub
    getArticles
    getTitles
    createFile('../tpl/nav.tpl', './nav.html')
    createFile('../tpl/index.tpl', './index.html')
    conf = @conf
    articles = @articles

    builder = GEPUB::Builder.new {
        language 'zh-cn'
        unique_identifier conf['link'], 'BookID', 'URL'
        title conf['title']
        subtitle conf['subtitle']
        creator conf['author']
        date Time.new
        # contributors '123', '456'

        resources(:workdir => '.') {
            nav 'nav.html'

            # cover_image 'img/image1.jpg' => 'image1.jpg'
            ordered {
                file 'index.html'
                file 'nav.html'

                articles.each do |a|
                    file a.file
                    heading a.title
                end
            }
        }
    }
    builder.generate_epub(@conf['file_name'])
    clean
end

#to_sObject



84
85
86
87
88
# File 'lib/link2epub.rb', line 84

def to_s
    @articles.each do |a|
        puts a.title
    end
end