Class: BibSync::Actions::FindMyCitations

Inherits:
Object
  • Object
show all
Includes:
Log, Utils
Defined in:
lib/bibsync/actions/find_my_citations.rb

Constant Summary

Constants included from Log

Log::Blue, Log::Level, Log::Red, Log::Reset, Log::Yellow

Instance Method Summary collapse

Methods included from Utils

#arxiv_download, #arxiv_id, #fetch, #fetch_html, #fetch_xml, #name_without_ext

Methods included from Log

#log

Constructor Details

#initialize(options) ⇒ FindMyCitations

Returns a new instance of FindMyCitations.



7
8
9
10
11
# File 'lib/bibsync/actions/find_my_citations.rb', line 7

def initialize(options)
  raise 'Option --bib is required' unless @bib = options[:bib]
  raise 'Option --citedbyme is required' unless @dir = options[:citedbyme]
  raise "#{@dir} is not a directory" unless File.directory?(@dir)
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bibsync/actions/find_my_citations.rb', line 13

def run
  notice 'Find citations in my TeX files'

  cites = {}
  Dir[File.join(@dir, '**/*.tex')].each do |file|
    File.read(file).scan(/cite\{([^\}]+)\}/) do
      $1.split(/\s*,\s*/).each do |key|
        key.strip!
        file = @bib.relative_path(file)
        debug("Cited in #{file}", key: key)
        (cites[key] ||= []) << file
      end
    end
  end

  @bib.each do |entry|
    next if entry.comment?
    entry.delete(:cites) unless cites.include?(entry.key)
  end

  cites.each do |key, files|
    files = files.sort.uniq.join(';')
    if @bib[key]
      @bib[key][:citedbyme] = files
    else
      warning("Cited in #{files} but not found in #{@bib.file}", key: key)
    end
  end

  @bib.save
end