Class: Epuber::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/compiler.rb,
lib/epuber/compiler/problem.rb,
lib/epuber/compiler/file_stat.rb,
lib/epuber/compiler/generator.rb,
lib/epuber/compiler/file_database.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/coffee_script_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, FileDatabase, FileResolver, FileStat, Generator, MetaInfGenerator, NavGenerator, OPFGenerator, Problem, 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:



49
50
51
52
53
# File 'lib/epuber/compiler.rb', line 49

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

Instance Attribute Details

#compilation_contextCompilationContext (readonly)

Returns:



44
45
46
# File 'lib/epuber/compiler.rb', line 44

def compilation_context
  @compilation_context
end

#file_resolverEpuber::Compiler::FileResolver (readonly)



40
41
42
# File 'lib/epuber/compiler.rb', line 40

def file_resolver
  @file_resolver
end

Class Method Details

.globals_catcherEpuber::GlobalsContext



34
35
36
# File 'lib/epuber/compiler.rb', line 34

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/epuber/compiler.rb', line 112

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, use_cache: true) ⇒ 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



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/epuber/compiler.rb', line 64

def compile(build_folder, check: false, write: false, release: false, verbose: false, use_cache: true)
  @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
  compilation_context.use_cache = use_cache

  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)}`"

    file_resolver.add_file(FileTypes::SourceFile.new(Config.instance.pretty_path_from_project(@book.file_path).to_s))
    compilation_context.plugins

    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

    source_paths = file_resolver.files.select { |a| a.is_a?(FileTypes::SourceFile) }.map { |a| a.source_path }
    compilation_context.source_file_database.cleanup(source_paths)
    compilation_context.source_file_database.
    compilation_context.source_file_database.save_to_file

    compilation_context.target_file_database.cleanup(source_paths)
    compilation_context.target_file_database.
    compilation_context.target_file_database.save_to_file
  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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/epuber/compiler.rb', line 142

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