Module: Jekyll::Scholar::Utilities

Included in:
BibliographyTag, CiteDetailsTag, CiteTag, Details, DetailsGenerator, QuoteTag, ReferenceTag
Defined in:
lib/jekyll/scholar/utilities.rb

Overview

Utility methods used by several Scholar plugins. The methods in this module may depend on the presence of #config, #bibtex_file, and #site readers

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bibtex_fileObject (readonly)

Returns the value of attribute bibtex_file


9
10
11
# File 'lib/jekyll/scholar/utilities.rb', line 9

def bibtex_file
  @bibtex_file
end

#configObject (readonly)

Returns the value of attribute config


9
10
11
# File 'lib/jekyll/scholar/utilities.rb', line 9

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context


9
10
11
# File 'lib/jekyll/scholar/utilities.rb', line 9

def context
  @context
end

#queryObject (readonly)

Returns the value of attribute query


9
10
11
# File 'lib/jekyll/scholar/utilities.rb', line 9

def query
  @query
end

#siteObject (readonly)

Returns the value of attribute site


9
10
11
# File 'lib/jekyll/scholar/utilities.rb', line 9

def site
  @site
end

Instance Method Details

#base_urlObject


60
61
62
# File 'lib/jekyll/scholar/utilities.rb', line 60

def base_url
  @base_url ||= site.config['baseurl'] || site.config['base_url'] || ''
end

#bibliographyObject


19
20
21
# File 'lib/jekyll/scholar/utilities.rb', line 19

def bibliography
  @bibliography ||= BibTeX.open(bibtex_path, bibtex_options)
end

#bibtex_optionsObject


11
12
13
# File 'lib/jekyll/scholar/utilities.rb', line 11

def bibtex_options
  @bibtex_options ||= { :filter => :latex }
end

#bibtex_pathObject


15
16
17
# File 'lib/jekyll/scholar/utilities.rb', line 15

def bibtex_path
  @bibtex_path ||= extend_path(bibtex_file)
end

#cite(key) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll/scholar/utilities.rb', line 68

def cite(key)
  entry = bibliography[key]

  if bibliography.key?(key)
    citation = CiteProc.process entry.to_citeproc, :style => config['style'],
      :locale => config['locale'], :format => 'html', :mode => :citation

    link_to "##{entry.key}", citation.join
  else
    "(missing reference)"
  end
rescue
  "(#{key})"
end

#cite_details(key, text) ⇒ Object


83
84
85
86
87
88
89
# File 'lib/jekyll/scholar/utilities.rb', line 83

def cite_details(key, text)
  if bibliography.key?(key)
    link_to details_link_for(bibliography[key]), text || config['details_link']
  else
    "(missing reference)"
  end
end

#cited_referencesObject


111
112
113
# File 'lib/jekyll/scholar/utilities.rb', line 111

def cited_references
  context && context['cited'] || []
end

#content_tag(name, content_or_attributes, attributes = {}) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jekyll/scholar/utilities.rb', line 91

def (name, content_or_attributes, attributes = {})
  if content_or_attributes.is_a?(Hash)
    content, attributes = nil, content_or_attributes
  else
    content = content_or_attributes
  end

  attributes = attributes.map { |k,v| %Q(#{k}="#{v}") }

  if content.nil?
    "<#{[name, attributes].flatten.compact.join(' ')}/>"
  else
    "<#{[name, attributes].flatten.compact.join(' ')}>#{content}</#{name}>"
  end
end

#details_file_for(entry) ⇒ Object


48
49
50
51
52
53
54
# File 'lib/jekyll/scholar/utilities.rb', line 48

def details_file_for(entry)
  name = entry.key.to_s.dup

  name.gsub!(/[:\s]+/, '_')

  [name, 'html'].join('.')
end

56
57
58
# File 'lib/jekyll/scholar/utilities.rb', line 56

def details_link_for(entry, base = base_url)
  File.join(base, details_path, details_file_for(entry))
end

#details_pathObject


64
65
66
# File 'lib/jekyll/scholar/utilities.rb', line 64

def details_path
  config['details_dir']
end

#entriesObject


23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll/scholar/utilities.rb', line 23

def entries
  b = bibliography[query || config['query']]

  unless config['sort_by'] == 'none'
    b = b.sort_by { |e| e[config['sort_by']].to_s }
    b.reverse! if config['order'] =~ /^(desc|reverse)/i
  end

  b
end

#extend_path(name) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/jekyll/scholar/utilities.rb', line 34

def extend_path(name)
  if name.nil? || name.empty?
    name = config['bibliography']
  end

  p = File.join(config['source'], name)
  p << '.bib' unless File.exists?(p)
  p
end

#generate_details?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/jekyll/scholar/utilities.rb', line 44

def generate_details?
  site.layouts.key?(File.basename(config['details_layout'], '.html'))
end

107
108
109
# File 'lib/jekyll/scholar/utilities.rb', line 107

def link_to(href, content, attributes = {})
   :a, content || href, attributes.merge(:href => href)
end

#set_context_to(context) ⇒ Object


115
116
117
118
# File 'lib/jekyll/scholar/utilities.rb', line 115

def set_context_to(context)
  @context, @site, = context, context.registers[:site]
  config.merge!(site.config['scholar'] || {})
end