class Mangdown::Page

Mangdown page

Attributes

page[R]

Public Class Methods

new(page) click to toggle source
# File lib/mangdown/page.rb, line 17
def initialize(page)
  @page = page
end

Public Instance Methods

append_file_data(_dir, data) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/mangdown/page.rb, line 64
def append_file_data(_dir, data)
  File.open(to_path, 'ab') { |file| file.write(data) }
end
append_file_ext(dir = nil) click to toggle source
# File lib/mangdown/page.rb, line 68
def append_file_ext(dir = nil)
  setup_path(dir) if dir
  path = to_path
  ext = Tools.image_extension(path)
  filename = "#{path}.#{ext}"

  FileUtils.mv(path, filename)
end
chapter() click to toggle source
# File lib/mangdown/page.rb, line 21
def chapter
  @chapter ||= Mangdown.chapter(page.chapter)
end
delete_files!(dir) click to toggle source

cleanup existing file (all extensions)

# File lib/mangdown/page.rb, line 84
def delete_files!(dir)
  File.delete(to_path) while setup_path(dir) && File.exist?(to_path)
end
download_to(dir = Dir.pwd, opts = { force_download: false }) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/mangdown/page.rb, line 43
def download_to(dir = Dir.pwd, opts = { force_download: false })
  delete_files!(dir) if opts[:force_download]

  return if file_exist?(dir)

  image = Tools.get(uri)

  append_file_data(dir, image)
  append_file_ext(dir)
rescue StandardError => error
  logger.error({
    msg: 'Failed to download page',
    page: self,
    uri: uri,
    error: error,
    error_msg: error.message,
    backtrace: error.backtrace
  }.to_s)
end
file_exist?(dir = nil) click to toggle source
# File lib/mangdown/page.rb, line 77
def file_exist?(dir = nil)
  setup_path(dir) if dir

  Dir.entries(dir).any? { |file| file.to_s[to_path.basename.to_s] }
end
path() click to toggle source
# File lib/mangdown/page.rb, line 25
def path
  @path ||= setup_path
end
Also aliased as: to_path
setup_path(dir = nil) click to toggle source

Set path of page to file path if a file exists or to path without file extension if file is not found. File extensions are appended only after the file has been downloaded.

# File lib/mangdown/page.rb, line 33
def setup_path(dir = nil)
  dir ||= chapter.path
  dir = Tools.valid_path_name(dir)
  name = self.name.tr('/', '')
  file = Dir.entries(dir).find { |f| f[name] } if Dir.exist?(dir)
  path = Tools.file_join(dir, file || name)
  @path = Tools.relative_or_absolute_path(path)
end
to_path()
Alias for: path