|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright (c) 2018-2022 The Ruby Citation File Format Developers. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +require_relative 'formatters' |
| 18 | + |
| 19 | +## |
| 20 | +module CFF |
| 21 | + # Methods to enable turning a CFF model or file into a citation. |
| 22 | + # |
| 23 | + # The core functionality is in the `citation` method. In addition, each |
| 24 | + # available output format has a `to_{format}` method generated for it as |
| 25 | + # well, e.g. `to_bibtex` or `to_apalike`. These methods take a single |
| 26 | + # parameter, `preferred_citation:`, which defaults to `true` as in the |
| 27 | + # `citation` method. |
| 28 | + module Citable |
| 29 | + # :call-seq: |
| 30 | + # citation(format, preferred_citation: true) -> String |
| 31 | + # |
| 32 | + # Output this Index in the specified format. Setting |
| 33 | + # `preferred_citation: true` will honour the `preferred_citation` field in |
| 34 | + # the index if one is present (default). |
| 35 | + # |
| 36 | + # `format` can be supplied as a String or a Symbol. |
| 37 | + # |
| 38 | + # Formats that are built-in to Ruby CFF are: |
| 39 | + # |
| 40 | + # * APAlike (e.g. `:apalike`, `'apalike'` or `'APAlike'`) |
| 41 | + # * BibTeX (e.g. `:bibtex`, `'bibtex'` or `'BibTeX'`) |
| 42 | + # |
| 43 | + # *Note:* This method assumes that this Index is valid when called. |
| 44 | + def citation(format, preferred_citation: true) |
| 45 | + formatter = Formatters.formatter_for(format) |
| 46 | + return '' if formatter.nil? |
| 47 | + |
| 48 | + formatter.format(model: self, preferred_citation: preferred_citation) |
| 49 | + end |
| 50 | + |
| 51 | + def self.add_to_format_method(format) # :nodoc: |
| 52 | + method = "to_#{format}" |
| 53 | + return if method_defined?(method) |
| 54 | + |
| 55 | + class_eval( |
| 56 | + # def to_bibtex(preferred_citation: true) |
| 57 | + # citation(:bibtex, preferred_citation: preferred_citation) |
| 58 | + # end |
| 59 | + <<-END_TO_FORMAT, __FILE__, __LINE__ + 1 |
| 60 | + def #{method}(preferred_citation: true) |
| 61 | + citation(:#{format}, preferred_citation: preferred_citation) |
| 62 | + end |
| 63 | + END_TO_FORMAT |
| 64 | + ) |
| 65 | + end |
| 66 | + |
| 67 | + # Add the formatters we know about already upfront. |
| 68 | + Formatters.formatters.each do |format| |
| 69 | + add_to_format_method(format) |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments