Method: CSLAdaptor.replace_citations_with_csl

Defined in:
lib/texstylist/csl_adaptor.rb

.replace_citations_with_csl(text, citation_style, bibtex, options = {}) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/texstylist/csl_adaptor.rb', line 65

def self.replace_citations_with_csl(text, citation_style, bibtex, options={})
  options = {decorate: true}.merge(options)
  citation_style = CSLAdaptor.safe_style(citation_style)
  renderer = CiteProc::Ruby::Renderer.new(format: 'text', style: citation_style)
  # Dependent styles still experience issues, use the default chicago processor as a fallback
  default_renderer = CiteProc::Ruby::Renderer.new(format: 'text', style: :'chicago-author-date')

  csl_unique_count = 0
  csl_map = {}
  latex_util = LatexUtil.new
  references_section = "\\section*{References}\n"
  text = latex_util.preprocess_verb(text)
  text = text.gsub(LatexUtil.citation_regex) do |match|
    cite_type = $~[:type]
    star   = $~[:star]
    optional_arg1 = $~[:opt1]
    optional_arg2 = $~[:opt2]
    braces = $~[:braces]

    citations = braces.split(',').flatten
    citations = citations.map {|c| c.strip}
    length = citations.length
    csl_text = citations.map do |c|
      new_unique = !csl_map[c]
      if new_unique
        csl_unique_count += 1
        csl_map[c] = csl_unique_count
      end
      csl_index = csl_map[c]

      bib_data = !c.empty? && bibtex && bibtex[c.to_sym]
      if bib_data.nil? # fallback - no such bib entry
        '(missing citation)'
      else
        item = CiteProc::CitationItem.new id: c do |ci|
          ci.data = CiteProc::Item.new bib_data.to_citeproc
          # numeric styles not yet implemented in citeproc-ruby, so we need to manually set the number, see:
          # https://github.com/inukshuk/citeproc-ruby/issues/40
          ci.data[:'citation-number'] = csl_index
        end
        # I. If just added citation, add it to final Bibliography
        if new_unique
          begin # sometimes the CSL style has no bibliography definition, and the references render raises exceptions
            rendered_reference = renderer.render item, STYLES[citation_style].bibliography
            if options[:decorate]
              references_section << "\\phantomsection\n\\label{csl:#{csl_unique_count}}"
            end
            references_section << rendered_reference
            references_section << "\n\n"
          rescue => e
            puts "CSL bibliography render failed with: ", e
          end
        end

        # II. Always add the inline rendered citation
        begin
          inline_render = renderer.render [item], STYLES[citation_style].citation
          if inline_render.blank?
            inline_render = begin
              default_renderer.render [item], STYLES[citation_style].citation
            end
            if inline_render.blank?
              inline_render = '(missing citation)'
            end
          end
          if options[:decorate]
            "\\hyperref[csl:#{csl_index}]{#{inline_render}}"
          else
            inline_render
          end
        rescue => e
          puts "CSL citation render failed with: ", e
          ""
        end
      end
    end
    csl_text.join(" ")
  end
  return latex_util.postprocess_verb(text) + "\n\n" + references_section
end