Class: Researchmap2bib::Generator

Inherits:
Object
  • Object
show all
Includes:
UniqueId, Utils
Defined in:
lib/researchmap2bib/generator.rb

Instance Method Summary collapse

Methods included from UniqueId

#unique_id

Methods included from Utils

#concatenate_authors, #family_name, #first_author, #to_hankaku, #year_month

Instance Method Details

#generate(file_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/researchmap2bib/generator.rb', line 15

def generate(file_name)
  bibliography =  make_bibliography(file_name)

  base_name = File.basename(file_name, '.zip')
  File.write(base_name + '.bib', bibliography)

  ids = list_up_id
  template = <<EOS
\\documentclass[uplatex]{jsarticle}
\\begin{document}
文献\\cite{<%= ids.join(',') %>}
\\bibliographystyle{junsrt}
\\bibliography{<%= base_name %>}
\\end{document}
EOS
  erb = ERB.new(template)
  sample = erb.result(binding)
  File.write('sample.tex', sample)
end

#generate_key(authors, date) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/researchmap2bib/generator.rb', line 168

def generate_key(authors, date)
  first_author = first_author(authors)
  name = family_name(first_author)
  year, month = year_month(date)
  year = /[0-9][0-9]([0-9][0-9])/.match(year).captures[0]
  if month.to_i >0
    name + year + month
  else
    name + year
  end
end

#list_up_idObject



35
36
37
38
39
40
41
# File 'lib/researchmap2bib/generator.rb', line 35

def list_up_id
  ids = Array.new
  @entries.each do |entry|
    ids.push(entry.id)
  end
  ids
end

#make_bibliography(file_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/researchmap2bib/generator.rb', line 43

def make_bibliography(file_name)
  @entries = read_researchmap(file_name)

  results = Array.new
  @entries.each do |entry|
    results.push(make_bibliography_entry(entry))
  end

  results.join("\n")
end

#make_bibliography_entry(entry) ⇒ Object

0:未設定、1:研究論文(学術雑誌)、2:研究論文(国際会議プロシーディングス)、 3:研究論文(大学,研究機関紀要)、 4:研究論文(研究会,シンポジウム資料等)、5:研究論文(その他学術会議資料等)



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/researchmap2bib/generator.rb', line 101

def make_bibliography_entry(entry)
  author = concatenate_authors(entry.author)

  year, month = year_month(entry.publicationDate)
  month = month.to_i

  case entry.paperType.to_i
  when 1, 3 then
    record = <<EOS
@Article{<%= entry.id %>,
  author  = {<%= author %>},
  title   = {<%= entry.title %>},
  journal = {<%= entry.journal %>},
EOS
    record += '  year    = <%= year %>'
    if month > 0
       record += ",\n  month   = <%= month %>"
    end
    if entry.volume
      record += ",\n  volume    = <%= entry.volume %>"
    end
    if entry.number
      record += ",\n  number    = <%= entry.number %>"
    end
    if entry.startingPage && entry.endingPage
      record += ",\n  pages     = {<%= entry.startingPage %>-<%= entry.endingPage %>}"
    elsif entry.startingPage
      record += ",\n  pages     = {<%= entry.startingPage %> %>}"
    end
    record += "}\n"
    if entry.summary
      record += "% <%= entry.summary %>\n"
    end
  else
    record = <<EOS
@InProceedings{<%= entry.id %>,
  author    = {<%= author %>},
  title     = {<%= entry.title %>},
  booktitle = {<%= entry.journal %>},
EOS
    record += '  year      = <%= year %>'
    if month > 0
      record += ",\n  month     = <%= month %>"
    end
    if entry.volume
      record += ",\n  volume    = <%= entry.volume %>"
    end
    if entry.number
      record += ",\n  number    = <%= entry.number %>"
    end
    if entry.startingPage && entry.endingPage
      record += ",\n  pages     = {<%= entry.startingPage %>-<%= entry.endingPage %>}"
    elsif entry.startingPage
      record += ",\n  pages     = {<%= entry.startingPage %>}"
    end
    if entry.publisher
      record += ",\n  publisher = {<%= entry.publisher %>}"
    end
    record += "}\n"
    if entry.summary
      record += "% <%= entry.summary %>\n"
    end
  end
  erb = ERB.new(record)
  erb.result(binding)
end

#read_entry(entry) ⇒ Object



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
# File 'lib/researchmap2bib/generator.rb', line 70

def read_entry(entry)
  title           = entry.elements["title"].text
  author          = entry.elements["author"].elements["name"].text
  author          = to_hankaku(author)
  summary         = entry.elements["summary"].text
  journal         = entry.elements["rm:journal"].text
  publisher       = entry.elements["rm:publisher"].text
  publicationName = entry.elements["rm:publicationName"].text
  volume          = entry.elements["rm:volume"].text
  number          = entry.elements["rm:number"].text
  startingPage    = entry.elements["rm:startingPage"].text
  endingPage      = entry.elements["rm:endingPage"].text
  publicationDate = entry.elements["rm:publicationDate"].text
  referee         = entry.elements["rm:referee"] ? true : false
  language        = entry.elements["rm:language"].text
  paperType       = entry.elements["rm:paperType"].elements["id"].text

  key = generate_key(author, publicationDate)
  id = unique_id(key)

  Entry.new(
    id, # id
    title, author, summary, journal, publisher, publicationName,
    volume, number, startingPage, endingPage, publicationDate,
    referee, language, paperType,
  )
end

#read_paper(xml) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/researchmap2bib/generator.rb', line 61

def read_paper(xml)
  doc = REXML::Document.new(xml)
  entries = Array.new
  REXML::XPath.each(doc, '/feed/entry') do |entry|
    entries.push(read_entry(entry))
  end
  entries
end

#read_researchmap(file_name) ⇒ Object



54
55
56
57
58
59
# File 'lib/researchmap2bib/generator.rb', line 54

def read_researchmap(file_name)
  entries = Zip::File.open(file_name) do |zip_file|
    entry = zip_file.glob('*\/paper.xml').first
    read_paper(entry.get_input_stream.read)
  end
end