Class: HardCiter::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bib_key_pattern = nil, citation_key_pattern = nil) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
# File 'lib/hardciter/parser.rb', line 6

def initialize(bib_key_pattern=nil,citation_key_pattern=nil)
  @bib_key_pattern = bib_key_pattern ? bib_key_pattern : HardCiter.configuration.bibliography_pattern
  @citation_key_pattern = citation_key_pattern ? citation_key_pattern : HardCiter.configuration.intext_pattern
end

Instance Attribute Details

#bib_key_patternObject

Returns the value of attribute bib_key_pattern.



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

def bib_key_pattern
  @bib_key_pattern
end

#citation_key_patternObject

Returns the value of attribute citation_key_pattern.



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

def citation_key_pattern
  @citation_key_pattern
end

Instance Method Details

#create_bib_match(line) ⇒ Object



15
16
17
18
# File 'lib/hardciter/parser.rb', line 15

def create_bib_match(line)
  match_data = @bib_key_pattern.match(line)
  create_match(match_data, HardCiter::BIBLIOGRAPHY_OUT_MATCH)
end

#create_citation_match(match_data) ⇒ Object



20
21
22
# File 'lib/hardciter/parser.rb', line 20

def create_citation_match(match_data)
  create_match(match_data, HardCiter::INTEXT_CITATION_MATCH)
end

#create_match(match_data, type) ⇒ Object



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

def create_match(match_data,type)
  match = HardCiter::IntextMatch.new()
  match.position = match_data.begin(0) 
  match.type = type
  match.regex_match = match_data.to_s
  match
end

#find_intext_citations(line) ⇒ Object



32
33
34
35
36
# File 'lib/hardciter/parser.rb', line 32

def find_intext_citations(line)
  line.enum_for(:scan, @citation_key_pattern).map do
    create_citation_match(Regexp.last_match)
  end
end

#has_bibliography_key?(line) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/hardciter/parser.rb', line 11

def has_bibliography_key?(line)
  line =~ @bib_key_pattern
end

#parse_line(line) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/hardciter/parser.rb', line 38

def parse_line(line)
  if has_bibliography_key?(line)
    [create_bib_match(line)]
  else
    find_intext_citations(line)
  end
end