class AsciidoctorBibliography::Asciidoctor::BibliographerPreprocessor

Public Instance Methods

process(document, reader) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 11
def process(document, reader)
  document.bibliographer.options =
    ::AsciidoctorBibliography::Options.build document, reader

  document.bibliographer.database =
    ::AsciidoctorBibliography::Database.new *expand_db_globs(document)

  lines = remove_comments(reader.read_lines)
  processed_lines = process_lines lines, document.bibliographer
  reader.unshift_lines processed_lines
  reader
end

Private Instance Methods

expand_db_globs(document) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 74
def expand_db_globs(document)
  glob_pattern(
    document.bibliographer.options.database,
    document.base_dir,
  )
end
fetch_citations(lines, bibliographer) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 43
def fetch_citations(lines, bibliographer)
  lines.join("\n").gsub(Citation::REGEXP) do
    macro_name, macro_pars = Regexp.last_match.captures
    target_and_attributes_list_pairs = macro_pars.scan(Citation::MACRO_PARAMETERS_REGEXP)
    citation = Citation.new(macro_name, *target_and_attributes_list_pairs)
    bibliographer.add_citation(citation)
    citation.uuid
  end.lines.map(&:chomp)
end
glob_pattern(pattern_string, base_dir) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 81
def glob_pattern(pattern_string, base_dir)
  pattern_string.split(/(?<!\\)\s+/).map do |pattern|
    Dir.chdir(base_dir) { Dir.glob(pattern) }
  end.flatten
end
process_lines(lines, bibliographer) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 26
def process_lines(lines, bibliographer)
  # First we fetch citations and replace them with uuids,
  lines = fetch_citations lines, bibliographer
  # then we render them
  lines = render_citations lines, bibliographer
  # and finally we render indices.
  render_indices lines, bibliographer
end
remove_comments(lines) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 35
def remove_comments(lines)
  # Remove block comments
  ls = lines.join("\n").split(/^\/\/\/\/\n/).
    select.with_index { |_, i| i.even? }.join
  # Remove line comments
  ls.split("\n").reject { |line| line.start_with?("//") }
end
render_citations(lines, bibliographer) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 53
def render_citations(lines, bibliographer)
  processed_lines = lines.join("\n")
  bibliographer.citations.each do |citation|
    processed_lines.sub!(citation.uuid) do
      citation.render bibliographer
    end
  end
  processed_lines.lines.map(&:chomp)
end
render_indices(lines, bibliographer) click to toggle source
# File lib/asciidoctor-bibliography/asciidoctor/bibliographer_preprocessor.rb, line 63
def render_indices(lines, bibliographer)
  lines.map do |line|
    if line =~ Index::REGEXP
      index = Index.new(*Regexp.last_match.captures)
      index.render bibliographer
    else
      line
    end
  end.flatten
end