Module: Jekyll::Scholar::Utilities
- Included in:
- BibTeXConverter, BibTeXTag, BibliographyCountTag, BibliographyTag, CiteDetailsTag, CiteTag, Details, DetailsGenerator, DetailsLinkTag, NoCiteTag, 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
- #allow_locale_overrides? ⇒ Boolean
- #base_url ⇒ Object
- #bib_cache ⇒ Object
- #bibliography ⇒ Object
- #bibliography_list_tag ⇒ 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_cache ⇒ Object
- #cite_details(key, text) ⇒ Object
- #cited_entries ⇒ Object
- #cited_keys ⇒ Object
- #cited_only? ⇒ Boolean
- #cited_references ⇒ Object
- #clear? ⇒ Boolean
- #content_tag(name, content_or_attributes, attributes = {}) ⇒ Object
-
#csl_prefix ⇒ String
Prefix string before citations.
- #csl_renderer(force = false) ⇒ Object
-
#csl_suffix ⇒ String
Suffix string after citations.
-
#delimiter ⇒ String
Delimiter between citations in a citation group.
- #details_link(key) ⇒ Object
- #details_link_for(entry, base = base_url) ⇒ Object
- #details_path ⇒ Object
- #details_path_for(entry) ⇒ Object
- #entries ⇒ Object
- #extend_path(name) ⇒ Object
- #generate_details? ⇒ Boolean
- #generate_details_link? ⇒ 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
-
#layout ⇒ CSL::Style::Layout
The citation layout object for the currently selected style.
- #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) ⇒ CSL::Style
- #locales(lang) ⇒ Object
- #locators ⇒ Object
- #match_fields ⇒ Object
- #missing_reference ⇒ Object
- #month_names ⇒ Object
- #nocite(keys) ⇒ Object
- #optparse(arguments) ⇒ Object
- #query ⇒ Object
- #raw_bibtex_filters ⇒ Object
- #reference_data(entry, index = nil) ⇒ Object
- #reference_tag(entry, index = nil) ⇒ Object
- #reference_tagname ⇒ Object
- #relative ⇒ Object
- #remove_duplicates? ⇒ Boolean
- #render_bibliography(entry, index = nil) ⇒ Object
- #render_citation(items) ⇒ 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
- #resolve_sort_value(entry, key) ⇒ 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) ⇒ CSL::Style
Access or load style by style URI or relative path.
- #suppress_author? ⇒ Boolean
- #type_aliases ⇒ Object
- #type_names ⇒ Object
- #type_order ⇒ Object
- #update_dependency_tree ⇒ Object
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def config @config end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def context @context end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def max @max end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def offset @offset end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def prefix @prefix end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def site @site end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
19 20 21 |
# File 'lib/jekyll/scholar/utilities.rb', line 19 def text @text end |
Instance Method Details
#allow_locale_overrides? ⇒ Boolean
129 130 131 |
# File 'lib/jekyll/scholar/utilities.rb', line 129 def allow_locale_overrides? !!config['allow_locale_overrides'] end |
#base_url ⇒ Object
718 719 720 721 722 |
# File 'lib/jekyll/scholar/utilities.rb', line 718 def base_url @base_url ||= ( site.config['baseurl'] || site.config['base_url'] || '' ).to_s end |
#bib_cache ⇒ Object
185 186 187 |
# File 'lib/jekyll/scholar/utilities.rb', line 185 def bib_cache @@bib_cache ||= Jekyll::Cache.new("jekyll-scholar::BibCache") end |
#bibliography ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/jekyll/scholar/utilities.rb', line 193 def bibliography paths = bibtex_paths bib_mtimes = paths.reduce('') { |s, p| s << p << File.mtime(p).inspect } bib_hash = Digest::SHA256.hexdigest bib_mtimes bib_cache.getset(bib_hash) do bib = BibTeX::Bibliography.parse( paths.reduce('') { |s, p| s << IO.read(p) }, ) # clear cite caches on bibliography update cite_cache.clear @paths = paths bib.replace_strings if replace_strings? bib.join if join_strings? && replace_strings? # Remove duplicate entries bib.uniq!(*match_fields) if remove_duplicates? bib end end |
#bibliography_list_tag ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/jekyll/scholar/utilities.rb', line 121 def bibliography_list_tag if @bibliography_list_tag.nil? config['bibliography_list_tag'] else @bibliography_list_tag end end |
#bibliography_tag(entry, index) ⇒ Object
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/jekyll/scholar/utilities.rb', line 575 def bibliography_tag(entry, index) return missing_reference unless entry liquid_template.render( reference_data(entry,index) .merge(site.site_payload) .merge({ 'index' => index, 'page' => context.registers[:page], 'details' => details_link_for(entry) }), { :registers => context.registers, :filters => [Jekyll::Filters] }) end |
#bibliography_template ⇒ Object
555 556 557 |
# File 'lib/jekyll/scholar/utilities.rb', line 555 def bibliography_template @bibliography_template || config['bibliography_template'] end |
#bibtex_file ⇒ Object
:nodoc: backwards compatibility
156 157 158 |
# File 'lib/jekyll/scholar/utilities.rb', line 156 def bibtex_file bibtex_files[0] end |
#bibtex_files ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/jekyll/scholar/utilities.rb', line 146 def bibtex_files if config['bibliography'].include? '*' @bibtex_files ||= Dir.glob(File.join(config["source"], config['bibliography'])).collect do |f| Pathname(f).relative_path_from(Pathname(config['source'])).to_s end end @bibtex_files ||= [config['bibliography']] end |
#bibtex_filters ⇒ Object
165 166 167 |
# File 'lib/jekyll/scholar/utilities.rb', line 165 def bibtex_filters config['bibtex_filters'] ||= [] end |
#bibtex_options ⇒ Object
160 161 162 163 |
# File 'lib/jekyll/scholar/utilities.rb', line 160 def @bibtex_options ||= (config['bibtex_options'] || {}).symbolize_keys end |
#bibtex_path ⇒ Object
:nodoc: backwards compatibility
181 182 183 |
# File 'lib/jekyll/scholar/utilities.rb', line 181 def bibtex_path bibtex_paths[0] end |
#bibtex_paths ⇒ Object
173 174 175 176 177 178 |
# File 'lib/jekyll/scholar/utilities.rb', line 173 def bibtex_paths bibtex_files.map { |file| interpolated_file = interpolate file extend_path interpolated_file } end |
#citation_item_for(entry, citation_number = nil) ⇒ Object
763 764 765 766 767 768 769 |
# File 'lib/jekyll/scholar/utilities.rb', line 763 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
775 776 777 |
# File 'lib/jekyll/scholar/utilities.rb', line 775 def citation_number(key) (context['citation_numbers'] ||= {})[key] ||= cited_keys.length end |
#cite(keys) ⇒ Object
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 |
# File 'lib/jekyll/scholar/utilities.rb', line 783 def cite(keys) items = keys.map do |key| if bibliography.key?(key) entry = bibliography[key] cite_cache.getset(key) do entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty? if config['separate_links'] ## Render each citation in the group as a separate link # Render the single citation, stripping delimiting characters rendered = render_citation([entry]) .sub(/^#{Regexp.escape(csl_prefix)}/, '') .sub(/#{Regexp.escape(csl_suffix)}$/, '') # Then render to HTML with the link to this specific citation link_to link_target_for(key), rendered, {class: config['cite_class']} else entry end end else return missing_reference end end if config['separate_links'] # Combine individual linkified items csl_prefix + items.join(delimiter) + csl_suffix else # Render as a single link link_to link_target_for(keys[0]), render_citation(items), {class: config['cite_class']} end end |
#cite_cache ⇒ Object
189 190 191 |
# File 'lib/jekyll/scholar/utilities.rb', line 189 def cite_cache @@cite_cache ||= Jekyll::Cache.new("jekyll-scholar::CiteCache") end |
#cite_details(key, text) ⇒ Object
829 830 831 832 833 834 835 |
# File 'lib/jekyll/scholar/utilities.rb', line 829 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_entries ⇒ Object
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 |
# File 'lib/jekyll/scholar/utilities.rb', line 935 def cited_entries items = entries if cited_only? items = if skip_sort? cited_references.uniq.map do |key| items.detect { |e| e.key == key } end else entries.select do |e| cited_references.include? e.key end end # See #90 cited_keys.clear if clear? end items end |
#cited_keys ⇒ Object
771 772 773 |
# File 'lib/jekyll/scholar/utilities.rb', line 771 def cited_keys context['cited'] = context.environments.first['page']['cited'] ||= [] end |
#cited_only? ⇒ Boolean
470 471 472 |
# File 'lib/jekyll/scholar/utilities.rb', line 470 def cited_only? !!@cited end |
#cited_references ⇒ Object
865 866 867 |
# File 'lib/jekyll/scholar/utilities.rb', line 865 def cited_references context && cited_keys end |
#clear? ⇒ Boolean
474 475 476 |
# File 'lib/jekyll/scholar/utilities.rb', line 474 def clear? !!@clear end |
#content_tag(name, content_or_attributes, attributes = {}) ⇒ Object
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 |
# File 'lib/jekyll/scholar/utilities.rb', line 845 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 |
#csl_prefix ⇒ String
Prefix string before citations. If style has no prefix, return empty string
531 532 533 |
# File 'lib/jekyll/scholar/utilities.rb', line 531 def csl_prefix layout.attributes.prefix.nil? ? '' : layout.attributes.prefix end |
#csl_renderer(force = false) ⇒ Object
728 729 730 731 732 733 |
# File 'lib/jekyll/scholar/utilities.rb', line 728 def csl_renderer(force = false) return @csl_renderer if @csl_renderer && !force @csl_renderer = CiteProc::Ruby::Renderer.new :format => 'html', :style => style, :locale => config['locale'] end |
#csl_suffix ⇒ String
Suffix string after citations. If style has no suffix, return empty string
537 538 539 |
# File 'lib/jekyll/scholar/utilities.rb', line 537 def csl_suffix layout.attributes.suffix.nil? ? '' : layout.attributes.suffix end |
#delimiter ⇒ String
Delimiter between citations in a citation group
543 544 545 |
# File 'lib/jekyll/scholar/utilities.rb', line 543 def delimiter layout.delimiter end |
#details_link(key) ⇒ Object
837 838 839 840 841 842 843 |
# File 'lib/jekyll/scholar/utilities.rb', line 837 def details_link(key) if bibliography.key?(key) details_link_for(bibliography[key]) else raise Exception.new "Could not find entry for key '%s'" % key end end |
#details_link_for(entry, base = base_url) ⇒ Object
678 679 680 |
# File 'lib/jekyll/scholar/utilities.rb', line 678 def details_link_for(entry, base = base_url) File.join(base, details_path_for(entry)) end |
#details_path ⇒ Object
724 725 726 |
# File 'lib/jekyll/scholar/utilities.rb', line 724 def details_path config['details_dir'] end |
#details_path_for(entry) ⇒ Object
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/jekyll/scholar/utilities.rb', line 682 def details_path_for(entry) # Expand the details_permalink template into the complete URL for this entry. # First generate placeholders for all items in the bibtex entry url_placeholders = {} entry.fields.each_pair do |k, v| value = v.to_s.dup value = Jekyll::Utils::slugify(value, :mode => 'pretty') unless k == :doi || value.empty? url_placeholders[k] = value end # Maintain the same URLs are previous versions of jekyll-scholar # by replicating the way that it processed the key. url_placeholders[:key] = entry.key.to_s.gsub(/[:\s]+/, '_') url_placeholders[:details_dir] = details_path # Autodetect the appropriate file extension based upon the site config, # using the same rules as previous versions of jekyll-scholar. Users can # override these settings by defining a details_permalink # without the :extension field. if (site.config['permalink'] == 'pretty') || (site.config['permalink'].end_with? '/') url_placeholders[:extension] = '/' else url_placeholders[:extension] = '.html' end # Overwrite 'doi' key with the citation key if DOI field is empty or missing if !entry.has_field?('doi') || entry.doi.empty? url_placeholders[:doi] = url_placeholders[:key] end URL.new( template: config['details_permalink'], placeholders: url_placeholders ).to_s end |
#entries ⇒ Object
223 224 225 |
# File 'lib/jekyll/scholar/utilities.rb', line 223 def entries sort bibliography[query || config['query']].select { |x| x.instance_of? BibTeX::Entry} end |
#extend_path(name) ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/jekyll/scholar/utilities.rb', line 482 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.exist?(name) name = File.join scholar_source, name name << '.bib' if File.extname(name).empty? && !File.exist?(name) name end |
#generate_details? ⇒ Boolean
655 656 657 |
# File 'lib/jekyll/scholar/utilities.rb', line 655 def generate_details? site.layouts.key?(File.basename(config['details_layout'], '.html')) end |
#generate_details_link? ⇒ Boolean
648 649 650 651 652 653 |
# File 'lib/jekyll/scholar/utilities.rb', line 648 def generate_details_link? if !config['details_link'] then return false end return true end |
#group(ungrouped) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/jekyll/scholar/utilities.rb', line 299 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
295 296 297 |
# File 'lib/jekyll/scholar/utilities.rb', line 295 def group? group_by != 'none' end |
#group_by ⇒ Object
291 292 293 |
# File 'lib/jekyll/scholar/utilities.rb', line 291 def group_by @group_by = interpolate(@group_by) || config['group_by'] end |
#group_compare(key, v1, v2) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/jekyll/scholar/utilities.rb', line 338 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
318 319 320 321 322 323 324 325 |
# File 'lib/jekyll/scholar/utilities.rb', line 318 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
390 391 392 393 394 395 396 397 398 399 |
# File 'lib/jekyll/scholar/utilities.rb', line 390 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
327 328 329 330 |
# File 'lib/jekyll/scholar/utilities.rb', line 327 def group_order self.group_order = config['group_order'] if @group_order.nil? @group_order end |
#group_order=(value) ⇒ Object
332 333 334 335 336 |
# File 'lib/jekyll/scholar/utilities.rb', line 332 def group_order=(value) @group_order = Array(value) .map { |key| key.to_s.split(/\s*,\s*/) } .flatten end |
#group_tags ⇒ Object
382 383 384 385 386 387 388 |
# File 'lib/jekyll/scholar/utilities.rb', line 382 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
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/jekyll/scholar/utilities.rb', line 357 def group_value(key,item) case key when 'type' type_aliases[item.type.to_s] || item.type.to_s when 'year', 'date' value = item[key] if value&.date? value.to_date else value.to_s end when 'name' value = item[:author] || item[:editor] || item[:institution] || item[:organization] || item[:publisher] || BibTeX::Value.new value.to_s else value = item[key] if value.numeric? value.to_i else value.to_s end end end |
#grouper(items, keys, order) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/jekyll/scholar/utilities.rb', line 300 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
878 879 880 881 882 883 884 |
# File 'lib/jekyll/scholar/utilities.rb', line 878 def interpolate(string) return unless string string.gsub(/{{\s*([\w\.]+)\s*}}/) do |match| context[$1] || match end end |
#join_strings? ⇒ Boolean
466 467 468 |
# File 'lib/jekyll/scholar/utilities.rb', line 466 def join_strings? config['join_strings'] end |
#keys ⇒ Object
869 870 871 872 873 874 875 876 |
# File 'lib/jekyll/scholar/utilities.rb', line 869 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
142 143 144 |
# File 'lib/jekyll/scholar/utilities.rb', line 142 def labels @labels ||= [] end |
#layout ⇒ CSL::Style::Layout
The citation layout object for the currently selected style
525 526 527 |
# File 'lib/jekyll/scholar/utilities.rb', line 525 def layout styles(style).citation.layout end |
#limit_entries? ⇒ Boolean
235 236 237 |
# File 'lib/jekyll/scholar/utilities.rb', line 235 def limit_entries? !offset.nil? || !max.nil? end |
#link_target_for(key) ⇒ Object
779 780 781 |
# File 'lib/jekyll/scholar/utilities.rb', line 779 def link_target_for(key) "#{relative}##{[prefix, key].compact.join('-')}" end |
#link_to(href, content, attributes = {}) ⇒ Object
861 862 863 |
# File 'lib/jekyll/scholar/utilities.rb', line 861 def link_to(href, content, attributes = {}) content_tag :a, content || href, attributes.merge(:href => href) end |
#liquid_template ⇒ Object
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/jekyll/scholar/utilities.rb', line 559 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
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/jekyll/scholar/utilities.rb', line 603 def liquidify(entry) e = {} e['key'] = entry.key e['type'] = entry.type.to_s conv_opts = { quotes: config['bibtex_quotes'] } if !raw_bibtex_filters.empty? conv_opts[:filter] = *raw_bibtex_filters end if entry.field_names(config['bibtex_skip_fields']).empty? e['bibtex'] = entry.to_s(conv_opts) else tmp = entry.dup config['bibtex_skip_fields'].each do |name| tmp.delete name if tmp.field?(name) end e['bibtex'] = tmp.to_s(conv_opts) end #e['raw_bibtex'] = "{%raw%}#{e['bibtex']}{%endraw%}" 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
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/jekyll/scholar/utilities.rb', line 435 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 = Pathname(path).relative_path_from(Pathname(File.join(base, repository_path))) parts = parts.to_path.split(repository_file_delimiter, 2) repo[parts[0]][parts[1]] = Pathname(path).relative_path_from(Pathname(base)) end repo end |
#load_style(uri) ⇒ CSL::Style
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'lib/jekyll/scholar/utilities.rb', line 894 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 |
#locales(lang) ⇒ Object
919 920 921 |
# File 'lib/jekyll/scholar/utilities.rb', line 919 def locales(lang) LOCALES[lang] ||= CSL::Locale.load(lang) end |
#locators ⇒ Object
138 139 140 |
# File 'lib/jekyll/scholar/utilities.rb', line 138 def locators @locators ||= [] end |
#match_fields ⇒ Object
134 135 136 |
# File 'lib/jekyll/scholar/utilities.rb', line 134 def match_fields @match_fields ||= [] end |
#missing_reference ⇒ Object
547 548 549 |
# File 'lib/jekyll/scholar/utilities.rb', line 547 def missing_reference config['missing_reference'] end |
#month_names ⇒ Object
413 414 415 416 417 |
# File 'lib/jekyll/scholar/utilities.rb', line 413 def month_names return @month_names unless @month_names.nil? @month_names = config['month_names'].nil? ? Date::MONTHNAMES : [nil] + config['month_names'] end |
#nocite(keys) ⇒ Object
817 818 819 820 821 822 823 824 825 826 827 |
# File 'lib/jekyll/scholar/utilities.rb', line 817 def nocite(keys) items = keys.map do |key| if bibliography.key?(key) entry = bibliography[key] cited_keys << entry.key cited_keys.uniq! else return missing_reference end end end |
#optparse(arguments) ⇒ Object
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/jekyll/scholar/utilities.rb', line 32 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('--clear') do |cited| @clear = true end opts.on('-r', '--remove_duplicates [MATCH_FIELDS]') do |match_field| @remove_duplicates = true @match_fields = match_field.split(/,\s+/) if not match_field.nil? 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('-h', '--bibliography_list_tag TAG') do |tag| @bibliography_list_tag = tag 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-[cCfhqptTsgGOlLomAr]|\B--(?:cited(_in_order)?|clear|bibliography_list_tag|file|query|prefix|text|style|group_(?:by|order)|type_order|template|locator|label|offset|max|suppress_author|remove_duplicates|))/) parser.parse argv.map(&:strip).reject(&:empty?) end |
#query ⇒ Object
219 220 221 |
# File 'lib/jekyll/scholar/utilities.rb', line 219 def query interpolate @query end |
#raw_bibtex_filters ⇒ Object
169 170 171 |
# File 'lib/jekyll/scholar/utilities.rb', line 169 def raw_bibtex_filters config['raw_bibtex_filters'] ||= [] end |
#reference_data(entry, index = nil) ⇒ Object
592 593 594 595 596 597 598 599 600 601 |
# File 'lib/jekyll/scholar/utilities.rb', line 592 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
509 510 511 512 513 514 515 516 517 |
# File 'lib/jekyll/scholar/utilities.rb', line 509 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
551 552 553 |
# File 'lib/jekyll/scholar/utilities.rb', line 551 def reference_tagname config['reference_tagname'] || :span end |
#relative ⇒ Object
505 506 507 |
# File 'lib/jekyll/scholar/utilities.rb', line 505 def relative config['relative'] end |
#remove_duplicates? ⇒ Boolean
419 420 421 |
# File 'lib/jekyll/scholar/utilities.rb', line 419 def remove_duplicates? @remove_duplicates || config['remove_duplicates'] end |
#render_bibliography(entry, index = nil) ⇒ Object
748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/jekyll/scholar/utilities.rb', line 748 def render_bibliography(entry, index = nil) begin original_locale = csl_renderer.locale csl_renderer.locale = locales(entry.language) rescue # Locale failed to load; just use original one! end if allow_locale_overrides? && entry['language'] != csl_renderer.locale.language csl_renderer.render citation_item_for(entry, index), styles(style).bibliography ensure csl_renderer.locale = original_locale unless original_locale.nil? end |
#render_citation(items) ⇒ Object
735 736 737 738 739 740 741 742 743 744 745 746 |
# File 'lib/jekyll/scholar/utilities.rb', line 735 def render_citation(items) csl_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 || 'page' unless locator.nil? item }, styles(style).citation end |
#replace_strings? ⇒ Boolean
462 463 464 |
# File 'lib/jekyll/scholar/utilities.rb', line 462 def replace_strings? config['replace_strings'] end |
#repository ⇒ Object
431 432 433 |
# File 'lib/jekyll/scholar/utilities.rb', line 431 def repository @repository ||= load_repository end |
#repository? ⇒ Boolean
427 428 429 |
# File 'lib/jekyll/scholar/utilities.rb', line 427 def repository? !config['repository'].nil? && !config['repository'].empty? end |
#repository_file_delimiter ⇒ Object
458 459 460 |
# File 'lib/jekyll/scholar/utilities.rb', line 458 def repository_file_delimiter config['repository_file_delimiter'] end |
#repository_link_for(entry, base = base_url) ⇒ Object
659 660 661 662 663 664 665 666 667 668 |
# File 'lib/jekyll/scholar/utilities.rb', line 659 def repository_link_for(entry, base = base_url) name = entry.key.to_s.dup name.gsub!(/[:\s]+/, '_') links = repository[name] url = links['pdf'] || links['ps'] return unless url File.join(base, url) end |
#repository_links_for(entry, base = base_url) ⇒ Object
670 671 672 673 674 675 676 |
# File 'lib/jekyll/scholar/utilities.rb', line 670 def repository_links_for(entry, base = base_url) name = entry.key.to_s.dup name.gsub!(/[:\s]+/, '_') Hash[repository[name].map { |ext, url| [ext, File.join(base, url)] }] end |
#repository_path ⇒ Object
454 455 456 |
# File 'lib/jekyll/scholar/utilities.rb', line 454 def repository_path config['repository'] end |
#resolve_sort_value(entry, key) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/jekyll/scholar/utilities.rb', line 239 def resolve_sort_value(entry, key) case key when 'name' # Fallback chain: author → editor → institution → organization → publisher # Convert to lowercase string for case-insensitive cross-type comparison # Strip leading/trailing braces and whitespace (for corporate authors like {{Name}}) value = entry[:author] || entry[:editor] || entry[:institution] || entry[:organization] || entry[:publisher] normalized = value ? value.to_s.gsub(/\A[{\s]+|[}\s]+\z/, '').downcase : '' BibTeX::Value.new(normalized) else entry[key].nil? ? BibTeX::Value.new : entry[key] end end |
#scholar_source ⇒ Object
496 497 498 499 500 501 502 503 |
# File 'lib/jekyll/scholar/utilities.rb', line 496 def scholar_source source = config['source'] # Improve by using Pathname from stdlib? return source if source.start_with?('/') && File.exist?(source) File.join site.source, source end |
#set_context_to(context) ⇒ Object
886 887 888 889 890 891 |
# File 'lib/jekyll/scholar/utilities.rb', line 886 def set_context_to(context) @context, @site, page, = context, *context.registers.values_at(:site, :page) config.merge!(site.config['scholar'] || {}) config.merge!(page['scholar'] || {}) self end |
#skip_sort? ⇒ Boolean
478 479 480 |
# File 'lib/jekyll/scholar/utilities.rb', line 478 def skip_sort? @skip_sort || config['sort_by'] == 'none' end |
#sort(unsorted) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/jekyll/scholar/utilities.rb', line 254 def sort(unsorted) return unsorted if skip_sort? sorted = unsorted.sort do |e1, e2| sort_keys .map.with_index do |key, idx| v1 = resolve_sort_value(e1, key) v2 = resolve_sort_value(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
274 275 276 277 278 279 280 281 |
# File 'lib/jekyll/scholar/utilities.rb', line 274 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
283 284 285 286 287 288 289 |
# File 'lib/jekyll/scholar/utilities.rb', line 283 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
22 23 24 25 26 27 28 29 30 |
# File 'lib/jekyll/scholar/utilities.rb', line 22 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
519 520 521 |
# File 'lib/jekyll/scholar/utilities.rb', line 519 def style interpolate(@style)|| config['style'] end |
#styles(style) ⇒ CSL::Style
Access or load style by style URI or relative path
915 916 917 |
# File 'lib/jekyll/scholar/utilities.rb', line 915 def styles(style) STYLES[style] ||= load_style(style) end |
#suppress_author? ⇒ Boolean
423 424 425 |
# File 'lib/jekyll/scholar/utilities.rb', line 423 def !!@suppress_author end |
#type_aliases ⇒ Object
405 406 407 |
# File 'lib/jekyll/scholar/utilities.rb', line 405 def type_aliases @type_aliases ||= Scholar.defaults['type_aliases'].merge(config['type_aliases']) end |
#type_names ⇒ Object
409 410 411 |
# File 'lib/jekyll/scholar/utilities.rb', line 409 def type_names @type_names ||= Scholar.defaults['type_names'].merge(config['type_names']) end |
#type_order ⇒ Object
401 402 403 |
# File 'lib/jekyll/scholar/utilities.rb', line 401 def type_order @type_order ||= config['type_order'] end |
#update_dependency_tree ⇒ Object
923 924 925 926 927 928 929 930 931 932 933 |
# File 'lib/jekyll/scholar/utilities.rb', line 923 def update_dependency_tree # Add bibtex files to dependency tree if context.registers[:page] and context.registers[:page].key? "path" bibtex_paths.each do |bibtex_path| site.regenerator.add_dependency( site.in_source_dir(context.registers[:page]["path"]), bibtex_path ) end end end |