Class: Foliokit::Exporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issue) ⇒ Exporter

Returns a new instance of Exporter.



6
7
8
9
# File 'lib/foliokit/exporter.rb', line 6

def initialize(issue)
  @issue = issue
  @converted = false
end

Instance Attribute Details

#convertedObject (readonly)

Returns the value of attribute converted.



4
5
6
# File 'lib/foliokit/exporter.rb', line 4

def converted
  @converted
end

#issueObject

Returns the value of attribute issue.



3
4
5
# File 'lib/foliokit/exporter.rb', line 3

def issue
  @issue
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



4
5
6
# File 'lib/foliokit/exporter.rb', line 4

def output_dir
  @output_dir
end

Instance Method Details

#build_html(url: nil, cover: nil, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/foliokit/exporter.rb', line 57

def build_html(url: nil, cover: nil, &block)
  builder = Nokogiri::HTML::Builder.new do |b|
    b.doc.internal_subset.remove
    b.doc.create_internal_subset('html', nil, nil)
    b.html do
      b.head do
        b.meta("http-equiv" => 'Content-Type', content: "text/html; charset=UTF-8")
        b.title(issue.title)
        b.meta(charset: 'utf-8')
        b.meta("http-equiv" => 'X-UA-Compatible', content: "IE=edge,chrome=1")
        b.meta(name: 'viewport', content: "width=#{issue.width}.00,minimum-scale=.2,user-scalable=no")
        b.meta(name: 'description', content: issue.description)
        b.meta(name: 'author', content: "MagLoft")
        b.meta(property: 'og:url', content: url) unless url.nil?
        b.meta(property: 'og:image', content: cover) unless cover.nil?
        b.meta(property: 'og:type', content: "website")
        b.meta(property: 'og:title', content: issue.title)
        b.meta(property: 'og:description', content: issue.description)
        b.meta(property: 'fb:app_id', content: "631288863616870")
        b.link(rel: 'stylesheet', href: "foliokit.css")
      end
      b.body do
        b.div(class: "foliokit-wrapper") do
          b.div(class: "foliokit-container swiper-container", style: "width: #{issue.width}px; height: #{issue.height}px") do
            b.div(class: "swiper-wrapper") do |wrapper_h|
              issue.slides.each_with_index do |slide, x|
                yield(slide, x) if block_given?
                if slide.articles.length > 1
                  b.article(class: "swiper-slide swiper-container") do
                    b.div(class: "swiper-wrapper") do |wrapper_v|
                      slide.articles.each_with_index do |article, y|
                        wrapper_v << article.export(b.doc, x, y)
                      end
                    end
                  end
                else
                  wrapper_h << slide.articles.first.export(b.doc, x, 0)
                end
              end
            end
          end
        end
        b.script(type: 'text/javascript', src: "foliokit.js")
      end
    end
  end
  builder.doc.to_html(indent: 4)
end

#export(dir, url: nil, cover: nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/foliokit/exporter.rb', line 11

def export(dir, url: nil, cover: nil, &block)
  spec = Gem::Specification.find_by_name("foliokit")
  assets_dir = Workspace::Dir.new(spec.gem_dir).dir("assets")
  issue_dir = issue.dir
  dir.clean
  dir.file("index.html").write(build_html(url: url, cover: cover, &block))
  issue.store_assets(dir.dir("assets"))
  issue_dir.dir("html").copy(dir.dir("html")) if issue_dir.dir("html").exists?
  assets_dir.file("foliokit.css").copy(dir.file("foliokit.css"))
  assets_dir.file("foliokit.js").copy(dir.file("foliokit.js"))
  @output_dir = dir
  @converted = true
  true
end

#pack_hpub(hpub_file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/foliokit/exporter.rb', line 26

def pack_hpub(hpub_file)
  # export book.json
  book_json = {
    "hpub": 1,
    "title": issue.title,
    "author": ["MagLoft"],
    "creator": ["MagLoft"],
    "date": Date.today.strftime,
    "url": "book://#{issue.package.id}",
    "orientation": issue.package.manifest.orientation,
    "zoomable": false,
    "-baker-background": "#FFFFFF",
    "-baker-vertical-bounce": false,
    "-baker-media-autoplay": true,
    "-baker-page-numbers-color": "#000000",
    "-baker-rendering": "three-cards",
    "cover": "cover.jpg",
    "navigator": "disable",
    "contents": ["index.html"]
  }
  output_dir.file("book.json").write(JSON.dump(book_json))
  output_dir.file(".nomedia").write("")
  # export cover
  rendition = issue.slides.first.articles.first.content_stack.renditions(0).first.slices.first
  cover_file = output_dir.file("cover.jpg")
  issue.dir.file(rendition.src).copy(cover_file)
  cover_file.fit(width: 768, height: 1024)
  # compress hpub
  output_dir.compress(hpub_file)
end