Sha256: f82f3d0614e1061e40196e74aa9f86c54aef932dfee1382a94aebd7eff4b751a

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

module Jekyll
  module Zettel
    # Enrich page front matter with object meta data
    class References < Jekyll::Generator
      include Jekyll::Zettel

      priority :highest

      attr_reader :site

      def generate(site)
        @site = site

        @site.data['references'] = {}
        configure_citeproc

        site.pages.each do |page|
          next unless page.path.to_s.end_with?('index.html') || page.path.to_s.end_with?('index.md')

          tie_reference(page)
        end

        write_catalog 'references'
      end

      def configure_citeproc
        locale = @site.config['citation']['locale'] || 'en-US'
        style = @site.config['citation']['style'] || 'apa'

        @site.config['citeproc'] = CiteProc::Processor.new locale: locale, style: style, format: 'html'
        Jekyll.logger.info LOG_KEY, "Configured cite processor `#{locale}`/`#{style}`"
      end

      def tie_reference(doc)
        dir = File.dirname(doc.path)
        file = @site.in_source_dir(dir, 'reference.json')
        return unless File.exist?(file)

        doc.data['reference'] = SafeYAML.load_file(file)

        register_reference(doc, file)
      end

      def register_reference(doc, file)
        if doc.data['reference'].include?('id')
          @site.data['references'][doc.data['reference']['id']] = doc.data['reference']
          @site.config['citeproc'].register doc.data['reference']
          doc.data['citekey'] = doc.data['reference']['id']
        else
          Jekyll.logger.warn LOG_KEY, 'missing property @id'
          Jekyll.logger.warn '', "./#{file}"
        end
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
jekyll-zettel-0.6.10 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.9 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.8 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.7 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.5 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.4 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.3 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.2 lib/jekyll/zettel/references.rb
jekyll-zettel-0.6.1 lib/jekyll/zettel/references.rb