Class: QuickCite::Main

Inherits:
Object
  • Object
show all
Includes:
QuickCite
Defined in:
lib/quickcite.rb

Constant Summary

Constants included from QuickCite

CITE_REGEX, SPLIT_REGEX, VERSION

Instance Method Summary collapse

Methods included from QuickCite

ask_user, cite_to_query, run

Constructor Details

#initialize(latex_files, bibtex_file) ⇒ Main

Returns a new instance of Main.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/quickcite.rb', line 61

def initialize(latex_files, bibtex_file)
  @source = DBLP.new
  
  if !File.exists?(bibtex_file) then
    puts "Bibtex file #{bibtex_file} does not exist.  Creating an empty file."
    open(bibtex_file, "w").write("")
  end
  
  @bib = BibTeX.open(bibtex_file, :include => [:meta_content])
  
  latex_files.each { |f|
    puts("Processing... #{f}")
    process_latex(f)
  }
  
  puts("Writing bibtex...")
  outfile = open(bibtex_file, "w")
  outfile.write(@bib.to_s)
  outfile.close
end

Instance Method Details

#process_cite(cite) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/quickcite.rb', line 88

def process_cite(cite)
  if @bib.has_key?(cite) then
    puts("Skipping matched reference #{cite}")
  else
    puts("Missing reference for #{cite}")
    query = QuickCite.cite_to_query(cite)
    results = @source.search(query)
    accepted = QuickCite.ask_user(cite, results)
    if accepted == nil
      puts "Skipping update for reference #{cite}"
    else
      puts "Updating bibtex for #{cite} with result: \n#{accepted.title}"
      update_bibtex(cite, accepted)
    end
  end
end

#process_latex(f) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/quickcite.rb', line 105

def process_latex(f)
  latex = File.read(f)
  latex.scan(CITE_REGEX) { |m|
    m[0].split(",").map  { |c|
      process_cite(c)
    }
  }
end

#update_bibtex(cite, entry) ⇒ Object



82
83
84
85
86
# File 'lib/quickcite.rb', line 82

def update_bibtex(cite, entry)
  e = BibTeX.parse(@source.bibtex(entry)).first
  e.key = cite
  @bib << e
end