Module: Jekyll::Scholar::Utilities
- Included in:
- BibTeXConverter, BibTeXTag, 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.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#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(key) ⇒ 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
- #group(ungrouped) ⇒ Object
- #group? ⇒ Boolean
- #group_by ⇒ Object
- #group_compare(key, v1, v2) ⇒ Object
- #group_keys ⇒ Object
- #group_name(key, value) ⇒ Object
- #group_order ⇒ Object
- #group_order=(value) ⇒ Object
- #group_tags ⇒ Object
- #group_value(key, item) ⇒ Object
- #grouper(items, keys, order) ⇒ Object
- #interpolate(string) ⇒ Object
- #join_strings? ⇒ Boolean
- #keys ⇒ Object
- #labels ⇒ Object
- #limit_entries? ⇒ Boolean
- #link_target_for(key) ⇒ Object
- #link_to(href, content, attributes = {}) ⇒ Object
- #liquid_template ⇒ Object
- #liquidify(entry) ⇒ Object
- #load_repository ⇒ Object
- #load_style(uri) ⇒ Object
- #locators ⇒ Object
- #missing_reference ⇒ Object
- #month_names ⇒ Object
- #optparse(arguments) ⇒ Object
- #query ⇒ Object
- #raw_bibtex? ⇒ Boolean
- #reference_data(entry, index = nil) ⇒ Object
- #reference_tag(entry, index = nil) ⇒ Object
- #reference_tagname ⇒ Object
- #relative ⇒ Object
- #render_bibliography(entry, index = nil) ⇒ Object
- #render_citation(items) ⇒ Object
- #renderer(force = false) ⇒ Object
- #replace_strings? ⇒ Boolean
- #repository ⇒ Object
- #repository? ⇒ Boolean
- #repository_file_delimiter ⇒ Object
- #repository_link_for(entry, base = base_url) ⇒ Object
- #repository_links_for(entry, base = base_url) ⇒ Object
- #repository_path ⇒ Object
- #scholar_source ⇒ Object
- #set_context_to(context) ⇒ Object
- #skip_sort? ⇒ Boolean
- #sort(unsorted) ⇒ Object
- #sort_keys ⇒ Object
- #sort_order ⇒ Object
- #split_arguments(arguments) ⇒ Object
- #style ⇒ Object
- #styles(style) ⇒ Object
- #suppress_author? ⇒ Boolean
- #type_aliases ⇒ Object
- #type_names ⇒ Object
- #type_order ⇒ Object
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def config @config end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def context @context end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def max @max end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def offset @offset end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def prefix @prefix end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def site @site end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
16 17 18 |
# File 'lib/jekyll/scholar/utilities.rb', line 16 def text @text end |
Instance Method Details
#base_url ⇒ Object
571 572 573 |
# File 'lib/jekyll/scholar/utilities.rb', line 571 def base_url @base_url ||= site.config['baseurl'] || site.config['base_url'] || '' end |
#bibliography ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/jekyll/scholar/utilities.rb', line 141 def bibliography unless @bibliography @bibliography = BibTeX::Bibliography.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
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/jekyll/scholar/utilities.rb', line 457 def bibliography_tag(entry, index) return missing_reference unless entry tmp = liquid_template.render( reference_data(entry,index) .merge(site.site_payload) .merge({ 'index' => index, 'details' => details_link_for(entry) }), { :registers => { :site => site }, :filters => [Jekyll::Filters] } ) # process the generated reference with Liquid, to get the same behaviour as # when it is used on a page Liquid::Template.parse(tmp).render( site.site_payload, { :registers => { :site => site }, :filters => [Jekyll::Filters] } ) end |
#bibliography_template ⇒ Object
437 438 439 |
# File 'lib/jekyll/scholar/utilities.rb', line 437 def bibliography_template @bibliography_template || config['bibliography_template'] end |
#bibtex_file ⇒ Object
:nodoc: backwards compatibility
117 118 119 |
# File 'lib/jekyll/scholar/utilities.rb', line 117 def bibtex_file bibtex_files[0] end |
#bibtex_files ⇒ Object
112 113 114 |
# File 'lib/jekyll/scholar/utilities.rb', line 112 def bibtex_files @bibtex_files ||= [config['bibliography']] end |
#bibtex_filters ⇒ Object
126 127 128 |
# File 'lib/jekyll/scholar/utilities.rb', line 126 def bibtex_filters config['bibtex_filters'] ||= [] end |
#bibtex_options ⇒ Object
121 122 123 124 |
# File 'lib/jekyll/scholar/utilities.rb', line 121 def @bibtex_options ||= (config['bibtex_options'] || {}).symbolize_keys end |
#bibtex_path ⇒ Object
:nodoc: backwards compatibility
137 138 139 |
# File 'lib/jekyll/scholar/utilities.rb', line 137 def bibtex_path bibtex_paths[0] end |
#bibtex_paths ⇒ Object
130 131 132 133 134 |
# File 'lib/jekyll/scholar/utilities.rb', line 130 def bibtex_paths @bibtex_paths ||= bibtex_files.map { |file| extend_path file } end |
#citation_item_for(entry, citation_number = nil) ⇒ Object
604 605 606 607 608 609 610 |
# File 'lib/jekyll/scholar/utilities.rb', line 604 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 c.data.suppress! 'author' if end end |
#citation_number(key) ⇒ Object
616 617 618 |
# File 'lib/jekyll/scholar/utilities.rb', line 616 def citation_number(key) (context['citation_numbers'] ||= {})[key] ||= cited_keys.length end |
#cite(keys) ⇒ Object
624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/jekyll/scholar/utilities.rb', line 624 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 link_target_for(keys[0]), render_citation(items) end |
#cite_details(key, text) ⇒ Object
637 638 639 640 641 642 643 |
# File 'lib/jekyll/scholar/utilities.rb', line 637 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
612 613 614 |
# File 'lib/jekyll/scholar/utilities.rb', line 612 def cited_keys context['cited'] = context.environments.first['page']['cited'] ||= [] end |
#cited_only? ⇒ Boolean
380 381 382 |
# File 'lib/jekyll/scholar/utilities.rb', line 380 def cited_only? !!@cited end |
#cited_references ⇒ Object
665 666 667 |
# File 'lib/jekyll/scholar/utilities.rb', line 665 def cited_references context && cited_keys end |
#content_tag(name, content_or_attributes, attributes = {}) ⇒ Object
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/jekyll/scholar/utilities.rb', line 645 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
540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/jekyll/scholar/utilities.rb', line 540 def details_file_for(entry) name = entry.key.to_s.dup name.gsub!(/[:\s]+/, '_') if site.config['permalink'] == 'pretty' name << '/' else name << '.html' end end |
#details_link_for(entry, base = base_url) ⇒ Object
567 568 569 |
# File 'lib/jekyll/scholar/utilities.rb', line 567 def details_link_for(entry, base = base_url) File.join(base, details_path, details_file_for(entry)) end |
#details_path ⇒ Object
575 576 577 |
# File 'lib/jekyll/scholar/utilities.rb', line 575 def details_path config['details_dir'] end |
#entries ⇒ Object
158 159 160 |
# File 'lib/jekyll/scholar/utilities.rb', line 158 def entries sort bibliography[query || config['query']].select { |x| x.instance_of? BibTeX::Entry} end |
#extend_path(name) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/jekyll/scholar/utilities.rb', line 388 def extend_path(name) if name.nil? || name.empty? name = config['bibliography'] end # Return as is if it is an absolute path # Improve by using Pathname from stdlib? return name if name.start_with?('/') && File.exists?(name) name = File.join scholar_source, name name << '.bib' if File.extname(name).empty? && !File.exists?(name) name end |
#generate_details? ⇒ Boolean
536 537 538 |
# File 'lib/jekyll/scholar/utilities.rb', line 536 def generate_details? site.layouts.key?(File.basename(config['details_layout'], '.html')) end |
#group(ungrouped) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/jekyll/scholar/utilities.rb', line 219 def group(ungrouped) def grouper(items, keys, order) groups = items.group_by do |item| group_value(keys.first, item) end if keys.count == 1 groups else groups.merge(groups) do |key, items| grouper(items, keys.drop(1), order.drop(1)) end end end grouper(ungrouped, group_keys, group_order) end |
#group? ⇒ Boolean
215 216 217 |
# File 'lib/jekyll/scholar/utilities.rb', line 215 def group? group_by != 'none' end |
#group_by ⇒ Object
211 212 213 |
# File 'lib/jekyll/scholar/utilities.rb', line 211 def group_by @group_by ||= config['group_by'] end |
#group_compare(key, v1, v2) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/jekyll/scholar/utilities.rb', line 258 def group_compare(key,v1,v2) case key when 'type' o1 = type_order.find_index(v1) o2 = type_order.find_index(v2) if o1.nil? && o2.nil? 0 elsif o1.nil? 1 elsif o2.nil? -1 else o1 <=> o2 end else v1 <=> v2 end end |
#group_keys ⇒ Object
238 239 240 241 242 243 244 245 |
# File 'lib/jekyll/scholar/utilities.rb', line 238 def group_keys return @group_keys unless @group_keys.nil? @group_keys = Array(group_by) .map { |key| key.to_s.split(/\s*,\s*/) } .flatten .map { |key| key == 'month' ? 'month_numeric' : key } end |
#group_name(key, value) ⇒ Object
301 302 303 304 305 306 307 308 309 310 |
# File 'lib/jekyll/scholar/utilities.rb', line 301 def group_name(key,value) case key when 'type' type_names[value] || value.to_s when 'month_numeric' month_names[value] || "(unknown)" else value.to_s end end |
#group_order ⇒ Object
247 248 249 250 |
# File 'lib/jekyll/scholar/utilities.rb', line 247 def group_order self.group_order = config['group_order'] if @group_order.nil? @group_order end |
#group_order=(value) ⇒ Object
252 253 254 255 256 |
# File 'lib/jekyll/scholar/utilities.rb', line 252 def group_order=(value) @group_order = Array(value) .map { |key| key.to_s.split(/\s*,\s*/) } .flatten end |
#group_tags ⇒ Object
293 294 295 296 297 298 299 |
# File 'lib/jekyll/scholar/utilities.rb', line 293 def return @group_tags unless @group_tags.nil? @group_tags = Array(config['bibliography_group_tag']) .map { |key| key.to_s.split(/\s*,\s*/) } .flatten end |
#group_value(key, item) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/jekyll/scholar/utilities.rb', line 277 def group_value(key,item) case key when 'type' type_aliases[item.type.to_s] || item.type.to_s else value = item[key] if value.numeric? value.to_i elsif value.date? value.to_date else value.to_s end end end |
#grouper(items, keys, order) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/jekyll/scholar/utilities.rb', line 220 def grouper(items, keys, order) groups = items.group_by do |item| group_value(keys.first, item) end if keys.count == 1 groups else groups.merge(groups) do |key, items| grouper(items, keys.drop(1), order.drop(1)) end end end |
#interpolate(string) ⇒ Object
678 679 680 681 682 683 684 |
# File 'lib/jekyll/scholar/utilities.rb', line 678 def interpolate(string) return unless string string.gsub(/{{\s*([\w\.]+)\s*}}/) do |match| context[$1] || match end end |
#join_strings? ⇒ Boolean
376 377 378 |
# File 'lib/jekyll/scholar/utilities.rb', line 376 def join_strings? config['join_strings'] end |
#keys ⇒ Object
669 670 671 672 673 674 675 676 |
# File 'lib/jekyll/scholar/utilities.rb', line 669 def keys # De-reference keys (in case they are variables) # We need to do this every time, to support for loops, # where the context can change for each invocation. Array(@keys).map do |key| context[key] || key end end |
#labels ⇒ Object
108 109 110 |
# File 'lib/jekyll/scholar/utilities.rb', line 108 def labels @labels ||= [] end |
#limit_entries? ⇒ Boolean
170 171 172 |
# File 'lib/jekyll/scholar/utilities.rb', line 170 def limit_entries? !offset.nil? || !max.nil? end |
#link_target_for(key) ⇒ Object
620 621 622 |
# File 'lib/jekyll/scholar/utilities.rb', line 620 def link_target_for(key) "#{relative}##{[prefix, key].compact.join('-')}" end |
#link_to(href, content, attributes = {}) ⇒ Object
661 662 663 |
# File 'lib/jekyll/scholar/utilities.rb', line 661 def link_to(href, content, attributes = {}) content_tag :a, content || href, attributes.merge(:href => href) end |
#liquid_template ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/jekyll/scholar/utilities.rb', line 441 def liquid_template return @liquid_template if @liquid_template Liquid::Template.register_filter(Jekyll::Filters) 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
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/jekyll/scholar/utilities.rb', line 494 def liquidify(entry) e = {} e['key'] = entry.key e['type'] = entry.type.to_s if entry.field_names(config['bibtex_skip_fields']).empty? e['bibtex'] = entry.to_s({ quotes: config['bibtex_quotes'] }) else tmp = entry.dup config['bibtex_skip_fields'].each do |name| tmp.delete name if tmp.field?(name) end e['bibtex'] = tmp.to_s({ quotes: config['bibtex_quotes'] }) end if raw_bibtex? e['bibtex'] = "{%raw%}#{e['bibtex']}{%endraw%}" end entry.fields.each do |key, value| value = value.convert(*bibtex_filters) unless bibtex_filters.empty? e[key.to_s] = value.to_s if value.is_a?(BibTeX::Names) e["#{key}_array"] = arr = [] value.each.with_index do |name, idx| parts = {} name.each_pair do |k, v| e["#{key}_#{idx}_#{k}"] = v.to_s parts[k.to_s] = v.to_s end arr << parts end end end e end |
#load_repository ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/jekyll/scholar/utilities.rb', line 346 def load_repository repo = Hash.new { |h,k| h[k] = {} } return repo unless repository? # ensure that the base directory format is literally # the same as the entries that are in the directory base = Dir[site.source][0] Dir[File.join(site.source, repository_path, '**/*')].each do |path| parts = File.basename(path).split(repository_file_delimiter, 2) repo[parts[0]][parts[1]] = Pathname(path).relative_path_from(Pathname(base)) end repo end |
#load_style(uri) ⇒ Object
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/jekyll/scholar/utilities.rb', line 692 def load_style(uri) begin style = CSL::Style.load uri rescue CSL::ParseError => error # Try to resolve local style paths # relative to Jekyll's source directory site_relative_style = File.join(site.source, uri) raise error unless File.exist?(site_relative_style) style = CSL::Style.load site_relative_style end if style.independent? style else style.independent_parent end end |
#locators ⇒ Object
104 105 106 |
# File 'lib/jekyll/scholar/utilities.rb', line 104 def locators @locators ||= [] end |
#missing_reference ⇒ Object
429 430 431 |
# File 'lib/jekyll/scholar/utilities.rb', line 429 def missing_reference config['missing_reference'] end |
#month_names ⇒ Object
324 325 326 327 328 |
# File 'lib/jekyll/scholar/utilities.rb', line 324 def month_names return @month_names unless @month_names.nil? @month_names = config['month_names'].nil? ? Date::MONTHNAMES : config['month_names'].unshift(nil) end |
#optparse(arguments) ⇒ Object
29 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jekyll/scholar/utilities.rb', line 29 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('-C', '--cited_in_order') do |cited| @cited, @skip_sort = true, true end opts.on('-A', '--suppress_author') do |cited| @suppress_author = 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('-L', '--label LABEL') do |label| labels << label end opts.on('-o', '--offset OFFSET') do |offset| @offset = offset.to_i end opts.on('-m', '--max MAX') do |max| @max = max.to_i end opts.on('-s', '--style STYLE') do |style| @style = style end opts.on('-g', '--group_by GROUP') do |group_by| @group_by = group_by end opts.on('-G', '--group_order ORDER') do |group_order| self.group_order = group_order end opts.on('-O', '--type_order ORDER') do |type_order| @group_by = type_order end opts.on('-T', '--template TEMPLATE') do |template| @bibliography_template = template end end argv = arguments.split(/(\B-[cCfqptTsgGOlLomA]|\B--(?:cited(_in_order)?|file|query|prefix|text|style|group_(?:by|order)|type_order|template|locator|label|offset|max|suppress_author|))/) parser.parse argv.map(&:strip).reject(&:empty?) end |
#query ⇒ Object
154 155 156 |
# File 'lib/jekyll/scholar/utilities.rb', line 154 def query interpolate @query end |
#raw_bibtex? ⇒ Boolean
334 335 336 |
# File 'lib/jekyll/scholar/utilities.rb', line 334 def raw_bibtex? config['use_raw_bibtex_entry'] end |
#reference_data(entry, index = nil) ⇒ Object
483 484 485 486 487 488 489 490 491 492 |
# File 'lib/jekyll/scholar/utilities.rb', line 483 def reference_data(entry, index = nil) { 'entry' => liquidify(entry), 'reference' => reference_tag(entry, index), 'key' => entry.key, 'type' => entry.type.to_s, 'link' => repository_link_for(entry), 'links' => repository_links_for(entry) } end |
#reference_tag(entry, index = nil) ⇒ Object
415 416 417 418 419 420 421 422 423 |
# File 'lib/jekyll/scholar/utilities.rb', line 415 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
433 434 435 |
# File 'lib/jekyll/scholar/utilities.rb', line 433 def reference_tagname config['reference_tagname'] || :span end |
#relative ⇒ Object
411 412 413 |
# File 'lib/jekyll/scholar/utilities.rb', line 411 def relative config['relative'] end |
#render_bibliography(entry, index = nil) ⇒ Object
599 600 601 602 |
# File 'lib/jekyll/scholar/utilities.rb', line 599 def render_bibliography(entry, index = nil) renderer.render citation_item_for(entry, index), styles(style).bibliography end |
#render_citation(items) ⇒ Object
586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/jekyll/scholar/utilities.rb', line 586 def render_citation(items) renderer.render items.zip(locators.zip(labels)).map { |entry, (locator, label)| cited_keys << entry.key cited_keys.uniq! item = citation_item_for entry, citation_number(entry.key) item.locator = locator item.label = label unless label.nil? item }, styles(style).citation end |
#renderer(force = false) ⇒ Object
579 580 581 582 583 584 |
# File 'lib/jekyll/scholar/utilities.rb', line 579 def renderer(force = false) return @renderer if @renderer && !force @renderer = CiteProc::Ruby::Renderer.new :format => 'html', :style => style, :locale => config['locale'] end |
#replace_strings? ⇒ Boolean
372 373 374 |
# File 'lib/jekyll/scholar/utilities.rb', line 372 def replace_strings? config['replace_strings'] end |
#repository ⇒ Object
342 343 344 |
# File 'lib/jekyll/scholar/utilities.rb', line 342 def repository @repository ||= load_repository end |
#repository? ⇒ Boolean
338 339 340 |
# File 'lib/jekyll/scholar/utilities.rb', line 338 def repository? !config['repository'].nil? && !config['repository'].empty? end |
#repository_file_delimiter ⇒ Object
368 369 370 |
# File 'lib/jekyll/scholar/utilities.rb', line 368 def repository_file_delimiter config['repository_file_delimiter'] end |
#repository_link_for(entry, base = base_url) ⇒ Object
552 553 554 555 556 557 558 559 |
# File 'lib/jekyll/scholar/utilities.rb', line 552 def repository_link_for(entry, base = base_url) links = repository[entry.key] url = links['pdf'] || links['ps'] return unless url File.join(base, url) end |
#repository_links_for(entry, base = base_url) ⇒ Object
561 562 563 564 565 |
# File 'lib/jekyll/scholar/utilities.rb', line 561 def repository_links_for(entry, base = base_url) Hash[repository[entry.key].map { |ext, url| [ext, File.join(base, url)] }] end |
#repository_path ⇒ Object
364 365 366 |
# File 'lib/jekyll/scholar/utilities.rb', line 364 def repository_path config['repository'] end |
#scholar_source ⇒ Object
402 403 404 405 406 407 408 409 |
# File 'lib/jekyll/scholar/utilities.rb', line 402 def scholar_source source = config['source'] # Improve by using Pathname from stdlib? return source if source.start_with?('/') && File.exists?(source) File.join site.source, source end |
#set_context_to(context) ⇒ Object
686 687 688 689 690 |
# File 'lib/jekyll/scholar/utilities.rb', line 686 def set_context_to(context) @context, @site, = context, context.registers[:site] config.merge!(site.config['scholar'] || {}) self end |
#skip_sort? ⇒ Boolean
384 385 386 |
# File 'lib/jekyll/scholar/utilities.rb', line 384 def skip_sort? @skip_sort || config['sort_by'] == 'none' end |
#sort(unsorted) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/jekyll/scholar/utilities.rb', line 174 def sort(unsorted) return unsorted if skip_sort? sorted = unsorted.sort do |e1, e2| sort_keys .map.with_index do |key, idx| v1 = e1[key].nil? ? BibTeX::Value.new : e1[key] v2 = e2[key].nil? ? BibTeX::Value.new : e2[key] if (sort_order[idx] || sort_order.last) =~ /^(desc|reverse)/i v2 <=> v1 else v1 <=> v2 end end .find { |c| c != 0 } || 0 end sorted end |
#sort_keys ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/jekyll/scholar/utilities.rb', line 194 def sort_keys return @sort_keys unless @sort_keys.nil? @sort_keys = Array(config['sort_by']) .map { |key| key.to_s.split(/\s*,\s*/) } .flatten .map { |key| key == 'month' ? 'month_numeric' : key } end |
#sort_order ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/jekyll/scholar/utilities.rb', line 203 def sort_order return @sort_order unless @sort_order.nil? @sort_order = Array(config['order']) .map { |key| key.to_s.split(/\s*,\s*/) } .flatten end |
#split_arguments(arguments) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 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
425 426 427 |
# File 'lib/jekyll/scholar/utilities.rb', line 425 def style @style || config['style'] end |
#styles(style) ⇒ Object
711 712 713 |
# File 'lib/jekyll/scholar/utilities.rb', line 711 def styles(style) STYLES[style] ||= load_style(style) end |
#suppress_author? ⇒ Boolean
330 331 332 |
# File 'lib/jekyll/scholar/utilities.rb', line 330 def !!@suppress_author end |
#type_aliases ⇒ Object
316 317 318 |
# File 'lib/jekyll/scholar/utilities.rb', line 316 def type_aliases @type_aliases ||= Scholar.defaults['type_aliases'].merge(config['type_aliases']) end |
#type_names ⇒ Object
320 321 322 |
# File 'lib/jekyll/scholar/utilities.rb', line 320 def type_names @type_names ||= Scholar.defaults['type_names'].merge(config['type_names']) end |
#type_order ⇒ Object
312 313 314 |
# File 'lib/jekyll/scholar/utilities.rb', line 312 def type_order @type_order ||= config['type_order'] end |