Sha256: b3c0e98fee4827140f2e82479da66f38420960e62c59d86563ffbee802e89315

Contents?: true

Size: 1.48 KB

Versions: 15

Compression:

Stored size: 1.48 KB

Contents

require 'yaml'
require 'pathname'

# When using pandoc, put relevant bibliography metadata extracted in the YAML filter back in the document so it gets passed on to pandoc.
class Gollum::Filter::PandocBib < Gollum::Filter

  BIB_PATH_KEYS = ['bibliography', 'csl']
  BIB_KEYS = ['link-citations', 'nocite']
  ALL_BIB_KEYS = BIB_PATH_KEYS + BIB_KEYS
  
  def process(data)
    data
  end

  def extract(data)
    return data unless supported_format? && bibliography_metadata_present?
    bib_metadata = {}
    bib_metadata.merge!(@markup.metadata.select {|key, _value| BIB_KEYS.include?(key)})

    BIB_PATH_KEYS.each do |bibliography_key|
      if path = @markup.metadata[bibliography_key]
        next unless file = @markup.wiki.file(path)
        bib_metadata[bibliography_key] = path_for_bibfile(file)
      end
    end
    bib_metadata.empty? ? data : "#{bib_metadata.to_yaml}---\n#{data}"
  end

  private

  def path_for_bibfile(file)
    if @markup.wiki.repo_is_bare
      path = Pathname.new("#{::File.join(::Dir.tmpdir, file.sha)}#{::File.extname(file.path)}")
        unless path.exist?
          path.open('w') do |copy_file|
            copy_file.write(file.raw_data)
          end
        end
      path.to_s
    else
      ::File.expand_path(::File.join(@markup.wiki.path, file.path))
    end
  end

  def supported_format?
    @markup.format == :markdown
  end

  def bibliography_metadata_present?
     @markup.metadata && @markup.metadata.keys.any? {|key| ALL_BIB_KEYS.include?(key)}
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
gollum-lib-5.0.6-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.6 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.5-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.5 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.4-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.4 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.3-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.3 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.2-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.2 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.1-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.1 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0 lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.a.4-java lib/gollum-lib/filter/pandoc_bib.rb
gollum-lib-5.0.a.4 lib/gollum-lib/filter/pandoc_bib.rb