Module: Jekyll::Scholar::Utilities
- Included in:
- BibTeXConverter, 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_files, and #site readers
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #base_url ⇒ Object
- #bibliography ⇒ Object
- #bibliography_tag(entry, index) ⇒ Object
- #bibliography_template ⇒ Object
-
#bibtex_file ⇒ Object
:nodoc: backwards compatibility.
- #bibtex_files ⇒ Object
- #bibtex_filters ⇒ Object
- #bibtex_options ⇒ Object
-
#bibtex_path ⇒ Object
:nodoc: backwards compatibility.
- #bibtex_paths ⇒ Object
- #citation_item_for(entry, citation_number = nil) ⇒ Object
- #citation_number ⇒ Object
- #cite(keys) ⇒ Object
- #cite_details(key, text) ⇒ Object
- #cited_keys ⇒ Object
- #cited_only? ⇒ Boolean
- #cited_references ⇒ Object
- #content_tag(name, content_or_attributes, attributes = {}) ⇒ Object
- #details_file_for(entry) ⇒ Object
- #details_link_for(entry, base = base_url) ⇒ Object
- #details_path ⇒ Object
- #entries ⇒ Object
- #extend_path(name) ⇒ Object
- #generate_details? ⇒ Boolean
- #join_strings? ⇒ Boolean
- #link_to(href, content, attributes = {}) ⇒ Object
- #liquid_template ⇒ Object
- #liquidify(entry) ⇒ Object
- #load_repository ⇒ Object
- #locators ⇒ Object
- #missing_reference ⇒ Object
- #optparse(arguments) ⇒ Object
- #reference_tag(entry, index = nil) ⇒ Object
- #reference_tagname ⇒ Object
- #render_bibliography(entry, index = nil) ⇒ Object
- #render_citation(items) ⇒ Object
- #renderer ⇒ Object
- #replace_strings? ⇒ Boolean
- #repository ⇒ Object
- #repository? ⇒ Boolean
- #repository_link_for(entry, base = base_url) ⇒ Object
- #repository_path ⇒ Object
- #set_context_to(context) ⇒ Object
- #split_arguments(arguments) ⇒ Object
- #style ⇒ Object
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def config @config end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def context @context end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def keys @keys end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def prefix @prefix end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def query @query end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def site @site end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
17 18 19 |
# File 'lib/jekyll/scholar/utilities.rb', line 17 def text @text end |
Instance Method Details
#base_url ⇒ Object
273 274 275 |
# File 'lib/jekyll/scholar/utilities.rb', line 273 def base_url @base_url ||= site.config['baseurl'] || site.config['base_url'] || '' end |
#bibliography ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jekyll/scholar/utilities.rb', line 105 def bibliography unless @bibliography @bibliography = BibTeX.parse( bibtex_paths.reduce('') { |s, p| s << IO.read(p) }, ) @bibliography.replace_strings if replace_strings? @bibliography.join if join_strings? && replace_strings? end @bibliography end |
#bibliography_tag(entry, index) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/jekyll/scholar/utilities.rb', line 215 def bibliography_tag(entry, index) return missing_reference unless entry liquid_template.render({ 'entry' => liquidify(entry), 'reference' => reference_tag(entry, index), 'key' => entry.key, 'type' => entry.type, 'link' => repository_link_for(entry), 'index' => index }) end |
#bibliography_template ⇒ Object
196 197 198 |
# File 'lib/jekyll/scholar/utilities.rb', line 196 def bibliography_template @bibliography_template || config['bibliography_template'] end |
#bibtex_file ⇒ Object
:nodoc: backwards compatibility
82 83 84 |
# File 'lib/jekyll/scholar/utilities.rb', line 82 def bibtex_file bibtex_files[0] end |
#bibtex_files ⇒ Object
77 78 79 |
# File 'lib/jekyll/scholar/utilities.rb', line 77 def bibtex_files @bibtex_files ||= [config['bibliography']] end |
#bibtex_filters ⇒ Object
90 91 92 |
# File 'lib/jekyll/scholar/utilities.rb', line 90 def bibtex_filters config['bibtex_filters'] ||= [] end |
#bibtex_options ⇒ Object
86 87 88 |
# File 'lib/jekyll/scholar/utilities.rb', line 86 def config['bibtex_options'] ||= {} end |
#bibtex_path ⇒ Object
:nodoc: backwards compatibility
101 102 103 |
# File 'lib/jekyll/scholar/utilities.rb', line 101 def bibtex_path bibtex_paths[0] end |
#bibtex_paths ⇒ Object
94 95 96 97 98 |
# File 'lib/jekyll/scholar/utilities.rb', line 94 def bibtex_paths @bibtex_paths ||= bibtex_files.map { |file| extend_path file } end |
#citation_item_for(entry, citation_number = nil) ⇒ Object
302 303 304 305 306 307 |
# File 'lib/jekyll/scholar/utilities.rb', line 302 def citation_item_for(entry, citation_number = nil) CiteProc::CitationItem.new id: entry.id do |c| c.data = CiteProc::Item.new entry.to_citeproc c.data[:'citation-number'] = citation_number end end |
#citation_number ⇒ Object
313 314 315 316 317 |
# File 'lib/jekyll/scholar/utilities.rb', line 313 def citation_number number = context['citation_number'] || 1 context['citation_number'] = number.succ number end |
#cite(keys) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/jekyll/scholar/utilities.rb', line 319 def cite(keys) items = keys.map do |key| if bibliography.key?(key) entry = bibliography[key] entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty? else return missing_reference end end link_to "##{[prefix, keys[0]].compact.join('-')}", render_citation(items) end |
#cite_details(key, text) ⇒ Object
332 333 334 335 336 337 338 |
# File 'lib/jekyll/scholar/utilities.rb', line 332 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_keys ⇒ Object
309 310 311 |
# File 'lib/jekyll/scholar/utilities.rb', line 309 def cited_keys context['cited'] ||= [] end |
#cited_only? ⇒ Boolean
157 158 159 |
# File 'lib/jekyll/scholar/utilities.rb', line 157 def cited_only? !!@cited end |
#cited_references ⇒ Object
360 361 362 |
# File 'lib/jekyll/scholar/utilities.rb', line 360 def cited_references context && context['cited'] || [] end |
#content_tag(name, content_or_attributes, attributes = {}) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/jekyll/scholar/utilities.rb', line 340 def content_tag(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
254 255 256 257 258 259 260 |
# File 'lib/jekyll/scholar/utilities.rb', line 254 def details_file_for(entry) name = entry.key.to_s.dup name.gsub!(/[:\s]+/, '_') [name, 'html'].join('.') end |
#details_link_for(entry, base = base_url) ⇒ Object
269 270 271 |
# File 'lib/jekyll/scholar/utilities.rb', line 269 def details_link_for(entry, base = base_url) File.join(base, details_path, details_file_for(entry)) end |
#details_path ⇒ Object
277 278 279 |
# File 'lib/jekyll/scholar/utilities.rb', line 277 def details_path config['details_dir'] end |
#entries ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jekyll/scholar/utilities.rb', line 118 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
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/jekyll/scholar/utilities.rb', line 161 def extend_path(name) if name.nil? || name.empty? name = config['bibliography'] end # return as is if it is an absolute path return name if name.start_with?('/') && File.exists?(name) p = File.join(config['source'], name) p << '.bib' unless File.exists?(p) p end |
#generate_details? ⇒ Boolean
250 251 252 |
# File 'lib/jekyll/scholar/utilities.rb', line 250 def generate_details? site.layouts.key?(File.basename(config['details_layout'], '.html')) end |
#join_strings? ⇒ Boolean
153 154 155 |
# File 'lib/jekyll/scholar/utilities.rb', line 153 def join_strings? config['join_strings'] end |
#link_to(href, content, attributes = {}) ⇒ Object
356 357 358 |
# File 'lib/jekyll/scholar/utilities.rb', line 356 def link_to(href, content, attributes = {}) content_tag :a, content || href, attributes.merge(:href => href) end |
#liquid_template ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/jekyll/scholar/utilities.rb', line 200 def liquid_template return @liquid_template if @liquid_template tmp = bibliography_template case when tmp.nil?, tmp.empty? tmp = '{{reference}}' when site.layouts.key?(tmp) tmp = site.layouts[tmp].content end @liquid_template = Liquid::Template.parse(tmp) end |
#liquidify(entry) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/jekyll/scholar/utilities.rb', line 228 def liquidify(entry) e = {} e['key'] = entry.key e['type'] = entry.type if entry.field?(:abstract) tmp = entry.dup tmp.delete :abstract e['bibtex'] = tmp.to_s else e['bibtex'] = entry.to_s end entry.fields.each do |key, value| value = value.convert(*bibtex_filters) unless bibtex_filters.empty? e[key.to_s] = value.to_s end e end |
#load_repository ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/jekyll/scholar/utilities.rb', line 137 def load_repository return {} unless repository? Hash[Dir[File.join(repository_path, '**/*.{pdf,ps}')].map { |path| [File.basename(path).sub(/\.(pdf|ps)$/, ''), path] }] end |
#locators ⇒ Object
73 74 75 |
# File 'lib/jekyll/scholar/utilities.rb', line 73 def locators @locators ||= [] end |
#missing_reference ⇒ Object
188 189 190 |
# File 'lib/jekyll/scholar/utilities.rb', line 188 def missing_reference config['missing_reference'] end |
#optparse(arguments) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jekyll/scholar/utilities.rb', line 30 def optparse(arguments) return if arguments.nil? || arguments.empty? parser = OptionParser.new do |opts| opts.on('-c', '--cited') do |cited| @cited = true end opts.on('-f', '--file FILE') do |file| @bibtex_files ||= [] @bibtex_files << file end opts.on('-q', '--query QUERY') do |query| @query = query end opts.on('-p', '--prefix PREFIX') do |prefix| @prefix = prefix end opts.on('-t', '--text TEXT') do |text| @text = text end opts.on('-l', '--locator LOCATOR') do |locator| locators << locator end opts.on('-s', '--style STYLE') do |style| @style = style end opts.on('-T', '--template TEMPLATE') do |template| @bibliography_template = template end end argv = arguments.split(/(\B-[cfqptTsl]|\B--(?:cited|file|query|prefix|text|style|template|locator|))/) parser.parse argv.map(&:strip).reject(&:empty?) end |
#reference_tag(entry, index = nil) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/jekyll/scholar/utilities.rb', line 174 def reference_tag(entry, index = nil) return missing_reference unless entry entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty? reference = render_bibliography entry, index content_tag reference_tagname, reference, :id => [prefix, entry.key].compact.join('-') end |
#reference_tagname ⇒ Object
192 193 194 |
# File 'lib/jekyll/scholar/utilities.rb', line 192 def reference_tagname config['reference_tagname'] || :span end |
#render_bibliography(entry, index = nil) ⇒ Object
297 298 299 300 |
# File 'lib/jekyll/scholar/utilities.rb', line 297 def render_bibliography(entry, index = nil) renderer.render citation_item_for(entry, index), STYLES[style].bibliography end |
#render_citation(items) ⇒ Object
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/jekyll/scholar/utilities.rb', line 286 def render_citation(items) renderer.render items.zip(locators).map { |entry, locator| cited_keys << entry.key item = citation_item_for entry, citation_number item.locator = locator item }, STYLES[style].citation end |
#renderer ⇒ Object
281 282 283 284 |
# File 'lib/jekyll/scholar/utilities.rb', line 281 def renderer @renderer ||= CiteProc::Ruby::Renderer.new :format => 'html', :style => style, :locale => config['locale'] end |
#replace_strings? ⇒ Boolean
149 150 151 |
# File 'lib/jekyll/scholar/utilities.rb', line 149 def replace_strings? config['replace_strings'] end |
#repository ⇒ Object
133 134 135 |
# File 'lib/jekyll/scholar/utilities.rb', line 133 def repository @repository ||= load_repository end |
#repository? ⇒ Boolean
129 130 131 |
# File 'lib/jekyll/scholar/utilities.rb', line 129 def repository? !config['repository'].nil? && !config['repository'].empty? end |
#repository_link_for(entry, base = base_url) ⇒ Object
262 263 264 265 266 267 |
# File 'lib/jekyll/scholar/utilities.rb', line 262 def repository_link_for(entry, base = base_url) url = repository[entry.key] return unless url File.join(base, url) end |
#repository_path ⇒ Object
145 146 147 |
# File 'lib/jekyll/scholar/utilities.rb', line 145 def repository_path config['repository'] end |
#set_context_to(context) ⇒ Object
364 365 366 367 |
# File 'lib/jekyll/scholar/utilities.rb', line 364 def set_context_to(context) @context, @site, = context, context.registers[:site] config.merge!(site.config['scholar'] || {}) end |
#split_arguments(arguments) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/jekyll/scholar/utilities.rb', line 20 def split_arguments(arguments) tokens = arguments.strip.split(/\s+/) args = tokens.take_while { |a| !a.start_with?('-') } opts = (tokens - args).join(' ') [args, opts] end |
#style ⇒ Object
184 185 186 |
# File 'lib/jekyll/scholar/utilities.rb', line 184 def style @style || config['style'] end |