Class: Xantora::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/xantora/document.rb

Overview

Document represents a Antora document and holds the functionality for PDF conversion based on asciidoctor-pdf

Constant Summary collapse

GEM_DIR =
File.expand_path("../..", __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Document

Returns a new instance of Document.



13
14
15
16
17
# File 'lib/xantora/document.rb', line 13

def initialize(path, options = {})
  @path = path
  @to_dir = options[:to_dir]
  @to_file = options[:to_file]
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/xantora/document.rb', line 9

def path
  @path
end

Instance Method Details

#asciidoc_attributes(optional_attributes = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/xantora/document.rb', line 83

def asciidoc_attributes(optional_attributes = {})
  attributes = {
    "toc" => "auto",
    "toclevels" => "1",
    "pdf-theme" => "puzzle",
    "pdf-themesdir" => File.join(GEM_DIR, "asciidoctor-pdf/themes"),
    "pdf-fontsdir" => "#{File.join(GEM_DIR, "asciidoctor-pdf/fonts")};GEM_FONTS_DIR",
    "imagesdir" => images_dir
  }
  attributes.merge!({ "page-component-title" => page_component_title })
  attributes.merge(optional_attributes)
end

#asciidoctor_options(options) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/xantora/document.rb', line 74

def asciidoctor_options(options)
  a_opts = {}
  a_opts[:to_file] = pdf_path(options)
  a_opts[:backend] = "pdf"
  a_opts[:safe] = :unsafe
  a_opts[:attributes] = asciidoc_attributes(options[:attributes])
  a_opts
end

#attachments_pathObject



56
57
58
59
60
61
62
# File 'lib/xantora/document.rb', line 56

def attachments_path
  if Dir.exist? File.join(module_dir, "attachments")
    File.join(module_dir, "attachments")
  else
    File.join(module_dir, "assets/attachments")
  end
end

#component_dirObject



19
20
21
# File 'lib/xantora/document.rb', line 19

def component_dir
  File.expand_path "../../../", File.dirname(@path)
end

#convert_to_pdf(options = {}) ⇒ Object



96
97
98
# File 'lib/xantora/document.rb', line 96

def convert_to_pdf(options = {})
  Asciidoctor.convert_file @path, asciidoctor_options(options)
end

#images_dirObject



64
65
66
67
68
69
70
71
72
# File 'lib/xantora/document.rb', line 64

def images_dir
  if Dir.exist? File.join(module_dir, "images")
    "../images"
  elsif Dir.exist? File.join(module_dir, "assets/images")
    "../assets/images"
  else
    ""
  end
end

#module_dirObject



23
24
25
# File 'lib/xantora/document.rb', line 23

def module_dir
  File.expand_path "..", File.dirname(@path)
end

#page_component_titleObject



27
28
29
30
31
32
33
# File 'lib/xantora/document.rb', line 27

def page_component_title
  YAML.load_file(
    File.join(component_dir, "antora.yml")
  )["title"]
rescue StandardError
  ""
end

#pdf_nameObject



35
36
37
38
39
40
41
42
# File 'lib/xantora/document.rb', line 35

def pdf_name
  name = File.basename(@path, ".adoc")
  if name == "index"
    doc = Asciidoctor.load_file @path, safe: :safe
    name = doc.doctitle ? doc.doctitle.gsub(/[^0-9A-Za-z.\-]/, "_") : "index"
  end
  "#{name}.pdf"
end

#pdf_path(options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xantora/document.rb', line 44

def pdf_path(options)
  if options[:to_attachments]
    File.join(attachments_path, pdf_name)
  elsif !options[:output]
    pdf_name
  elsif options[:output].end_with? ".pdf"
    options[:output]
  else
    File.join options[:output], pdf_name
  end
end