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/css_file.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:



45
46
47
48
49
# File 'lib/epuber/compiler.rb', line 45

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

Instance Attribute Details

#compilation_contextCompilationContext (readonly)

Returns:



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

def compilation_context
  @compilation_context
end

#file_resolverEpuber::Compiler::FileResolver (readonly)



36
37
38
# File 'lib/epuber/compiler.rb', line 36

def file_resolver
  @file_resolver
end

Class Method Details

.globals_trackerBade::Runtime::GlobalsTracker

Returns:

  • (Bade::Runtime::GlobalsTracker)


29
30
31
32
# File 'lib/epuber/compiler.rb', line 29

def self.globals_tracker
  @globals_tracker ||=
    Bade::Runtime::GlobalsTracker.new(constants_location_prefixes: [Config.instance.project_path])
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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/epuber/compiler.rb', line 122

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.exist?(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|
          UI.debug "removing file from result EPUB: #{file_to_remove}"
          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



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
105
106
107
108
109
110
111
112
113
114
# File 'lib/epuber/compiler.rb', line 60

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_tracker.catch do
    @build_folder = build_folder

    FileUtils.mkdir_p(build_folder)

    UI.info "  #{<<~MSG}"
      building target #{@target.name.inspect} (build dir: #{Config.instance.pretty_path_from_project(build_folder)})
    MSG

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

    # validate bookspec
    compilation_context.perform_plugin_things(Checker, :bookspec) do |checker|
      checker.call(@book, compilation_context)
    end

    parse_toc_item(@target.root_toc)
    parse_target_file_requests

    process_all_target_files
    generate_other_files

    # run :after_all_text_files transformers
    compilation_context.perform_plugin_things(Transformer, :after_all_text_files) do |transformer|
      transformer.call(@book, compilation_context)
    end

    process_global_ids

    # build folder cleanup
    remove_unnecessary_files
    remove_empty_folders

    source_paths = file_resolver.files.select { |a| a.is_a?(FileTypes::SourceFile) }.map(&: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_tracker.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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/epuber/compiler.rb', line 152

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