Class: EpubForge::Builder::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, opts = {}) ⇒ Builder

Returns a new instance of Builder.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/epubforge/builder/builder.rb', line 25

def initialize project, opts = {}
  target_file = opts[:target_file] || project.filename_for_book.ext("epub")  # TODO: But what about notes?
  
  # puts "--------------- forgin' #{ target_file } ------------------"

  @project = project
  @config  = project.config
  @book_dir_short = opts[:book_dir] ? opts[:book_dir].split.last.to_s : "book"
  @book_dir = @project.root_dir.join( @book_dir_short )   # TODO: .expand?
  
  @config.page_orderer = Utils::FileOrderer.new( opts[:page_order] || @config.pages[@book_dir_short] )

  @metadata = @config. || {}
  
  
  initialize_page_assets
  initialize_image_assets
  initialize_stylesheet_assets
  initialize_font_assets
  install_cover
  
  @scratch_dir = FunWith::Files::FilePath.tmpdir.join( "ebookdir" )
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



23
24
25
# File 'lib/epubforge/builder/builder.rb', line 23

def project
  @project
end

#stylesheetsObject (readonly)

Returns the value of attribute stylesheets.



22
23
24
# File 'lib/epubforge/builder/builder.rb', line 22

def stylesheets
  @stylesheets
end

Instance Method Details

#initialize_font_assetsObject



81
82
83
84
85
# File 'lib/epubforge/builder/builder.rb', line 81

def initialize_font_assets
  @fonts = @book_dir.glob( "fonts", ext: FONT_FILE_EXTENSION ).map do |font|
    Assets::Font.new( font )
  end
end

#initialize_image_assetsObject



70
71
72
73
# File 'lib/epubforge/builder/builder.rb', line 70

def initialize_image_assets
  images = @book_dir.glob( "images", ext: IMAGE_FILE_EXTENSIONS )
  @images = images.map{ |img| Assets::Image.new(img) }
end

#initialize_page_assetsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/epubforge/builder/builder.rb', line 49

def initialize_page_assets
  page_files = @book_dir.glob( ext: PAGE_FILE_EXTENSIONS )
  @section_files = @config.page_orderer.reorder( page_files )
  
  @sections = @section_files.map do |section|
    case section.to_s.split(".").last
    when "markdown", "html", "textile", "xhtml"
      Assets::Page.new( section, @metadata, self )
    #   Assets::Markdown.new( section, @metadata, self ) 
    # when "html"
    #   Assets::HTML.new( section, @metadata, self )
    # when "textile"
    #   Assets::Textile.new( section, @metadata, self )
    # when "xhtml"
    #   Assets::XHTML.new( section, @metadata, self )  # These files are inserted into the book unaltered
    else
      raise "UNKNOWN EXTENSION TYPE"
    end
  end
end

#initialize_stylesheet_assetsObject



75
76
77
78
79
# File 'lib/epubforge/builder/builder.rb', line 75

def initialize_stylesheet_assets
  @stylesheets = @book_dir.glob( "stylesheets", "*.css" ).map do |sheet| 
    Assets::Stylesheet.new( sheet ) 
  end
end

#install_coverObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/epubforge/builder/builder.rb', line 88

def install_cover
  # Existing cover is moved to the very front
  if @cover_section = @sections.detect(&:cover?)
    # no need to do anything
  elsif @cover_image = @images.detect(&:cover?)
    # actually install cover
    contents = "<div id='cover'><img class='cover' src='#{@cover_image.link.relative_path_from(TEXT_DIR)}' alt='#{@metadata.name}, by #{@metadata.author}'/></div>"
    cover_file = @project.book_dir.join( "cover.xhtml" )
    cover_file.write( wrap_page( contents, "cover" ) )
    @cover_section = Assets::Page.new( cover_file, @metadata, @project )
    @sections.unshift( @cover_section )
    puts "cover page generated"
  else
    return false
  end
end