Module: Citation

Included in:
EntryPoint::CLI
Defined in:
lib/citation.rb,
lib/citation/version.rb,
lib/citation/citation.rb

Defined Under Namespace

Classes: Base

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.parse(str, recursive = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/citation/citation.rb', line 46

def self.parse(str, recursive=false)
  base = Base.new
  string = str.dup
  urls = URI.extract(string, %w(http https))
  urls.each do | url |
    string.slice!(url)
  end
  base.url = urls
 
  # fetch square bracket
  bracket_matcher = string.match(/\[([\w\d_ ]).+\]/)
  base.sq = bracket_matcher[0] unless bracket_matcher.nil?
  string.slice!(base.sq)
 
  # fetch year
  year_matcher = string.scan(/\d{4}/)
  base.year = year_matcher.
    map(&:to_i).
    select{|i| i > 1970 and i < Time.now.year}.
    shift.to_s unless year_matcher.nil?
  string.slice!(base.year)
 
  # fetch title if format is MLA
  title_matcher = string.match(/".+"/)
  unless title_matcher.nil?
    base.title = title_matcher[0] unless title_matcher.nil?
    s = string.split(base.title)
    string.slice!(base.title)
    string.slice!('()')
    base.title.gsub!('"','')

    author = s.shift
    base.author = author
    base.desc = s.shift
    base.type = 'MLA'.to_sym

    string.slice!(base.author)
    string.slice!(base.desc)
  end

  # fetch title if format is APA
  title_matcher = string.match(/\(\)\. .+\. /)
  if base.type.empty? and not title_matcher.nil?
    base.title = title_matcher[0] unless title_matcher.nil?
    base.title.gsub!(/"|\(\)\. /,'')
    
    string.slice!(base.title)
    s = string.split('(). ')
    author = s.shift
    base.author = author
    base.desc = s.shift
    base.type = 'APA'.to_sym
    string.slice!(base.author)
    string.slice!(base.desc)
  end


  # fetch title if format is ISO 690
  iso_matcher = string.match(/, \.$/)
  if base.type.empty? and not iso_matcher.nil?
    s = string.gsub(/ ([A-Z]). /,'_\1_ ')
    arr = s.split('. ')
    base.author = arr.shift.gsub(/_([A-Z])_ /,' \1. ')
    base.title = arr.shift if base.title == ''
    base.desc = arr.shift
    base.type = 'ISO 6900'.to_sym
  end


  # fetch other
  if base.type.empty?
    s = string.gsub(/ ([A-Z]). /,'_\1_ ')
    arr = s.split('. ')
    base.author = arr.shift.gsub(/_([A-Z])_ /,' \1. ')
    base.title = arr.shift if base.title == ''
    base.desc = arr
  end
  return base
  
end