Sha256: 61199c2afa19fb27aec99c6f75641038143f31d20e459c48b3232ac4e43d7713

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

require "asciidoctor/attribute_list"
require_relative "formatter"

module AsciidoctorBibliography
  class Index
    REGEXP = /^(bibliography)::(\S+)?\[(|.*?[^\\])\]$/

    attr_reader :macro, :target, :attributes

    def initialize(macro, target, attributes)
      @macro = macro
      @target = target
      @attributes = ::Asciidoctor::AttributeList.new(attributes).parse
    end

    def render(bibliographer)
      formatter = setup_formatter bibliographer

      lines = []
      formatter.bibliography.each_with_index do |reference, index|
        line = "{empty}"
        line << "anchor:#{anchor_id(formatter.data[index].id)}[]"
        line << reference
        lines << line
      end

      # Intersperse the lines with empty ones to render as paragraphs.
      lines.join("\n\n").lines.map(&:strip)
    end

    private

    def setup_formatter(bibliographer)
      formatter = Formatter.new(bibliographer.options.style, locale: bibliographer.options.locale)

      formatter.replace_bibliography_sort bibliographer.options.sort unless bibliographer.options.sort.nil?

      filtered_db = prepare_filtered_db bibliographer
      formatter.import filtered_db
      formatter.force_sort!(mode: :bibliography)

      formatter
    end

    def prepare_filtered_db(bibliographer)
      bibliographer.occurring_keys.
        map { |id| bibliographer.database.find_entry_by_id(id) }.
        map { |entry| prepare_entry_metadata bibliographer, entry }
    end

    def prepare_entry_metadata(bibliographer, entry)
      entry.
        merge('citation-number': bibliographer.appearance_index_of(entry["id"])).
        merge('citation-label': entry["id"]) # TODO: smart label generators
    end

    def anchor_id(target)
      ["bibliography", target].compact.join("-")
    end

    def render_entry_label(target, formatter)
      formatter.render(:bibliography, id: target).join
    end

    def render_entry(target, formatter)
      "anchor:#{anchor_id(target)}[]#{render_entry_label(target, formatter)}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
asciidoctor-bibliography-0.4.4 lib/asciidoctor-bibliography/index.rb
asciidoctor-bibliography-0.4.3 lib/asciidoctor-bibliography/index.rb
asciidoctor-bibliography-0.4.2 lib/asciidoctor-bibliography/index.rb
asciidoctor-bibliography-0.4.1 lib/asciidoctor-bibliography/index.rb
asciidoctor-bibliography-0.4.0 lib/asciidoctor-bibliography/index.rb