Class: AsciidoctorBibliography::Options

Inherits:
Hash
  • Object
show all
Defined in:
lib/asciidoctor-bibliography/options.rb

Constant Summary collapse

PREFIX =
"bibliography-".freeze
DEFAULTS =
{
  "bibliography-database" => nil,
  "bibliography-locale" => "en-US",
  "bibliography-style" => "apa",
  "bibliography-hyperlinks" => "true",
  "bibliography-order" => "alphabetical", # TODO: deprecate
  "bibliography-tex-style" => "authoryear",
  "bibliography-sort" => nil,
  "bibliography-prepend-empty" => "true",
  "bibliography-passthrough" => "false"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



21
22
23
# File 'lib/asciidoctor-bibliography/options.rb', line 21

def initialize
  merge DEFAULTS
end

Class Method Details

.build(document, reader) ⇒ Object



25
26
27
28
29
30
# File 'lib/asciidoctor-bibliography/options.rb', line 25

def self.build(document, reader)
  header_attributes = get_header_attributes_hash reader
  header_attributes.select! { |key, _| DEFAULTS.keys.include? key }
  cli_attributes = document.attributes.select { |key, _| DEFAULTS.keys.include? key }
  new.merge!(header_attributes).merge!(cli_attributes)
end

.get_header_attributes_hash(reader) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/asciidoctor-bibliography/options.rb', line 32

def self.get_header_attributes_hash(reader)
  # We peek at the document attributes we need, without perturbing the parsing flow.
  # NOTE: we'll use this in a preprocessor and they haven't been parsed yet, there.
  tmp_document = ::Asciidoctor::Document.new
  tmp_reader = ::Asciidoctor::PreprocessorReader.new(tmp_document, reader.source_lines)

  ::Asciidoctor::Parser.
    parse(tmp_reader, tmp_document, header_only: true).
    attributes
end

Instance Method Details

#databaseObject



74
75
76
77
78
79
80
81
82
# File 'lib/asciidoctor-bibliography/options.rb', line 74

def database
  value = self["bibliography-database"] || DEFAULTS["bibliography-database"]
  raise Errors::Options::Missing, <<~MESSAGE if value.nil?
    Option :bibliography-database: is mandatory.
    A bibliographic database is required.
  MESSAGE

  value
end

#hyperlinks?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
# File 'lib/asciidoctor-bibliography/options.rb', line 64

def hyperlinks?
  value = self["bibliography-hyperlinks"] || DEFAULTS["bibliography-hyperlinks"]
  raise_invalid <<~MESSAGE unless %w[true false].include? value
    Option :bibliography-hyperlinks: has an invalid value (#{value}).
    Allowed values are 'true' and 'false'.
  MESSAGE

  value == "true"
end

#localeObject



54
55
56
57
58
59
60
61
62
# File 'lib/asciidoctor-bibliography/options.rb', line 54

def locale
  value = self["bibliography-locale"] || DEFAULTS["bibliography-locale"]
  raise_invalid <<~MESSAGE unless CSL::Locale.list.include? value
    Option :bibliography-locale: has an invalid value (#{value}).
    Allowed values are #{CSL::Locale.list.inspect}.
  MESSAGE

  value
end

#passthrough?(context) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
# File 'lib/asciidoctor-bibliography/options.rb', line 108

def passthrough?(context)
  # NOTE: allowed contexts are :citation and :reference
  value = self["bibliography-passthrough"] || DEFAULTS["bibliography-passthrough"]
  raise_invalid <<~MESSAGE unless %w[true citations references false].include? value
    Option :bibliography-passthrough: has an invalid value (#{value}).
    Allowed values are 'true', 'citations', 'references' and 'false'.
  MESSAGE

  evaluate_ext_boolean_value_vs_context value: value, context: context
end

#prepend_empty?(context) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
# File 'lib/asciidoctor-bibliography/options.rb', line 119

def prepend_empty?(context)
  # NOTE: allowed contexts are :citation and :reference
  value = self["bibliography-prepend-empty"] || DEFAULTS["bibliography-prepend-empty"]
  raise_invalid <<~MESSAGE unless %w[true citations references false].include? value
    Option :bibliography-prepend-empty: has an invalid value (#{value}).
    Allowed values are 'true', 'citations', 'references' and 'false'.
  MESSAGE

  evaluate_ext_boolean_value_vs_context value: value, context: context
end

#sortObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/asciidoctor-bibliography/options.rb', line 84

def sort
  begin
    value = YAML.safe_load self["bibliography-sort"].to_s
  rescue Psych::SyntaxError => psych_error
    raise_invalid <<~MESSAGE
      Option :bibliography-sort: is not a valid YAML string: \"#{psych_error}\".
    MESSAGE
  end

  value = validate_parsed_sort_type! value
  value = validate_parsed_sort_contents! value unless value.nil?
  value
end

#styleObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/asciidoctor-bibliography/options.rb', line 43

def style
  # First we check whether an internal style exists to qualify its path.
  if self["bibliography-style"]
    filepath = File.join AsciidoctorBibliography.csl_styles_root,
                         self["bibliography-style"] + ".csl"
    self["bibliography-style"] = filepath if File.exist? filepath
  end
  # Then error throwing is delegated to CSL library.
  self["bibliography-style"] || DEFAULTS["bibliography-style"]
end

#tex_styleObject



98
99
100
101
102
103
104
105
106
# File 'lib/asciidoctor-bibliography/options.rb', line 98

def tex_style
  value = self["bibliography-tex-style"] || DEFAULTS["bibliography-tex-style"]
  raise_invalid <<~MESSAGE unless %w[authoryear numeric].include? value
    Option :bibliography-tex-style: has an invalid value (#{value}).
    Allowed values are 'authoryear' (default) and 'numeric'.
  MESSAGE

  value
end