Class: HardCiter::Citer

Inherits:
Object
  • Object
show all
Defined in:
lib/hardciter/citer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Citer

Returns a new instance of Citer.



6
7
8
9
10
11
12
# File 'lib/hardciter/citer.rb', line 6

def initialize(path = nil)
  initialize_library_by_path(path) if path
  @bibliography = HardCiter::Bibliography.new
  @csl = HardCiter::Configuration::CSL
  @output = HardCiter::HtmlOutput.new(@csl)
  @parser = HardCiter::Parser.new
end

Instance Attribute Details

#bibliographyObject

Returns the value of attribute bibliography.



4
5
6
# File 'lib/hardciter/citer.rb', line 4

def bibliography
  @bibliography
end

#cslObject

Returns the value of attribute csl.



4
5
6
# File 'lib/hardciter/citer.rb', line 4

def csl
  @csl
end

#libraryObject

Returns the value of attribute library.



4
5
6
# File 'lib/hardciter/citer.rb', line 4

def library
  @library
end

#outputObject

Returns the value of attribute output.



4
5
6
# File 'lib/hardciter/citer.rb', line 4

def output
  @output
end

Instance Method Details

#add_and_group_intext_matches(document, intext_matches) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/hardciter/citer.rb', line 41

def add_and_group_intext_matches(document,intext_matches)
  intext_matches.each_with_index do |matches, index|
    line = document.text_array[index]
    matches.each do |match|
      citation = @bibliography.pair_match_to_citation(match,index)
      match.citation = citation if citation
    end
    group_matches!(matches,line)
  end
end

#cite_text(text) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/hardciter/citer.rb', line 22

def cite_text(text)
  validate_prerequisites
  document = Document.new(text)
  intext_matches = parse_all_lines(document)
  add_and_group_intext_matches(document, intext_matches)
  get_entries_from_library
  @output.prepare_bibliography(@bibliography)
  @output.process_and_output_text(document,intext_matches)
end

#get_entries_from_libraryObject



32
33
34
35
36
37
38
39
# File 'lib/hardciter/citer.rb', line 32

def get_entries_from_library
    raise "Library is not set. Cannot get entries." if @library.nil?
    raise "Bibliography is not set. Cannot get entries." if @bibliography.nil?
    raise "Bibliography has no citations" if @bibliography.citations.nil? || @bibliography.citations.empty?
    @bibliography.citations.each do |cite_key,citation|
      citation.entry = @library.get_entry(citation.key)
    end
end

#group_matches!(matches, line) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hardciter/citer.rb', line 66

def group_matches!(matches, line)
  temp_matches = []
  current_match = matches.shift unless matches.empty?
  head = current_match
  until matches.empty?
    next_match = matches.shift
    if matches_are_paired?(current_match, next_match, line)
      pair_matches(current_match,next_match)
    else
      temp_matches.push(head)
      head = next_match
    end
    current_match = next_match
  end
  temp_matches.each { |m| matches.push m }
  matches.push(head)
end

#initialize_library_by_path(path) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/hardciter/citer.rb', line 14

def initialize_library_by_path(path)
  if path =~ HardCiter.configuration.bibtex_library_regex
    @library = BibTexLibrary.new(path)
  else
    raise "Unknown library path type"
  end
end

#parse_all_lines(document) ⇒ Object



60
61
62
63
64
# File 'lib/hardciter/citer.rb', line 60

def parse_all_lines(document)
  document.text_array.each_with_object([]) do |line,out|
    out.push(@parser.parse_line(line))
  end
end

#validate_prerequisitesObject



52
53
54
55
56
57
58
# File 'lib/hardciter/citer.rb', line 52

def validate_prerequisites
  if @library.nil?
    raise "Library missing cannot proceed with citation"
  elsif @csl.nil?
    raise "No citation format found, cannot proceed with citation"
  end
end