Class: Epuber::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/compiler.rb,
lib/epuber/compiler/generator.rb,
lib/epuber/compiler/file_resolver.rb,
lib/epuber/compiler/nav_generator.rb,
lib/epuber/compiler/opf_generator.rb,
lib/epuber/compiler/xhtml_processor.rb,
lib/epuber/compiler/meta_inf_generator.rb,
lib/epuber/compiler/compilation_context.rb,
lib/epuber/compiler/file_finders/normal.rb,
lib/epuber/compiler/file_types/nav_file.rb,
lib/epuber/compiler/file_types/opf_file.rb,
lib/epuber/compiler/file_types/bade_file.rb,
lib/epuber/compiler/file_finders/abstract.rb,
lib/epuber/compiler/file_types/image_file.rb,
lib/epuber/compiler/file_types/xhtml_file.rb,
lib/epuber/compiler/file_finders/imaginary.rb,
lib/epuber/compiler/file_types/source_file.rb,
lib/epuber/compiler/file_types/static_file.rb,
lib/epuber/compiler/file_types/stylus_file.rb,
lib/epuber/compiler/file_types/abstract_file.rb,
lib/epuber/compiler/file_types/generated_file.rb,
lib/epuber/compiler/file_types/mime_type_file.rb,
lib/epuber/compiler/file_types/container_xml_file.rb,
lib/epuber/compiler/file_types/ibooks_display_options_file.rb

Defined Under Namespace

Modules: FileFinders, FileTypes Classes: CompilationContext, FileResolver, Generator, MetaInfGenerator, NavGenerator, OPFGenerator, XHTMLProcessor

Constant Summary collapse

EPUB_CONTENT_FOLDER =
'OEBPS'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, target) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:



47
48
49
50
51
# File 'lib/epuber/compiler.rb', line 47

def initialize(book, target)
  @book = book
  @target = target
  @compilation_context = CompilationContext.new(book, target)
end

Instance Attribute Details

#compilation_contextCompilationContext (readonly)

Returns:



42
43
44
# File 'lib/epuber/compiler.rb', line 42

def compilation_context
  @compilation_context
end

#file_resolverEpuber::Compiler::FileResolver (readonly)



38
39
40
# File 'lib/epuber/compiler.rb', line 38

def file_resolver
  @file_resolver
end

Class Method Details

.globals_catcherEpuber::GlobalsContext



32
33
34
# File 'lib/epuber/compiler.rb', line 32

def self.globals_catcher
  @globals_catcher ||= Epuber::GlobalsContext.new
end

Instance Method Details

#archive(path = nil, configuration_suffix: nil) ⇒ String

Archives current target files to epub

Parameters:

  • path (String) (defaults to: nil)

    path to created archive

Returns:

  • (String)

    path



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
# File 'lib/epuber/compiler.rb', line 97

def archive(path = nil, configuration_suffix: nil)
  path ||= epub_name(configuration_suffix)

  epub_path = File.expand_path(path)

  Dir.chdir(@file_resolver.destination_path) do
    new_paths = @file_resolver.package_files.map(&:pkg_destination_path)

    if ::File.exists?(epub_path)
      Zip::File.open(epub_path, true) do |zip_file|
        old_paths = zip_file.instance_eval { @entry_set.entries.map(&:name) }
        diff = old_paths - new_paths
        diff.each do |file_to_remove|
          puts "DEBUG: removing file from result EPUB: #{file_to_remove}" if compilation_context.verbose?
          zip_file.remove(file_to_remove)
        end
      end
    end

    run_command(%(zip -q0X "#{epub_path}" mimetype))
    run_command(%(zip -qXr9D "#{epub_path}" "#{new_paths.join('" "')}" --exclude \\*.DS_Store))
  end

  path
end

#compile(build_folder, check: false, write: false, release: false, verbose: false) ⇒ void

This method returns an undefined value.

Compile target to build folder

Parameters:

  • build_folder (String)

    path to folder, where will be stored all compiled files

  • check (Bool) (defaults to: false)

    should run non-release checkers

  • write (Bool) (defaults to: false)

    should perform transformations of source files and write them back

  • release (Bool) (defaults to: false)

    this is release build



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
# File 'lib/epuber/compiler.rb', line 62

def compile(build_folder, check: false, write: false, release: false, verbose: false)
  @file_resolver = FileResolver.new(Config.instance.project_path, build_folder)
  compilation_context.file_resolver = @file_resolver
  compilation_context.should_check = check
  compilation_context.should_write = write
  compilation_context.release_build = release
  compilation_context.verbose = verbose

  self.class.globals_catcher.catch do
    @build_folder = build_folder

    FileUtils.mkdir_p(build_folder)

    puts "  handling target #{@target.name.inspect} in build dir `#{Config.instance.pretty_path_from_project(build_folder)}`"

    parse_toc_item(@target.root_toc)
    parse_target_file_requests

    process_all_target_files
    generate_other_files

    # build folder cleanup
    remove_unnecessary_files
    remove_empty_folders
  end
ensure
  self.class.globals_catcher.clear_all
end

#epub_name(configuration_suffix = nil) ⇒ String

Creates name of epub file for current book and current target

Returns:

  • (String)

    name of result epub file



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/epuber/compiler.rb', line 127

def epub_name(configuration_suffix = nil)
  epub_name = if !@book.output_base_name.nil?
                @book.output_base_name
              elsif @book.from_file?
                ::File.basename(@book.file_path, ::File.extname(@book.file_path))
              else
                @book.title
              end

  epub_name += @book.build_version.to_s unless @book.build_version.nil?
  epub_name += "-#{@target.name}" if @target != @book.default_target
  epub_name += "-#{configuration_suffix}" unless configuration_suffix.nil?
  epub_name + '.epub'
end