Module: Compilers

Included in:
Whedon::Processor
Defined in:
lib/whedon/compilers.rb

Overview

This module has methods to compile PDFs and Crossref XML depending upon the content type of the paper (Markdown or LaTeX)

Instance Method Summary collapse

Instance Method Details

#crossref_from_latex(paper_issue = nil, paper_volume = nil, paper_year = nil, paper_month = nil, paper_day = nil) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/whedon/compilers.rb', line 197

def crossref_from_latex(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  cross_ref_template_path = "#{Whedon.resources}/crossref.template"
  bibtex = Whedon::BibtexParser.new(paper.bibtex_path)

  # Pass the citations that are actually in the paper to the CrossRef
  # citations generator.

  citations_in_paper = File.read(paper.paper_path).scan(/(?<=\\cite\{)\w+/)
  # FIXME
  # Because of the way citations are handled in Pandoc, we need to prepend and @
  # to the front of each of the citation strings.
  citations_in_paper = citations_in_paper.map {|c| c.prepend("@")}

  citations = bibtex.generate_citations(citations_in_paper)
  authors = paper.crossref_authors
  # TODO fix this when we update the DOI URLs
  # crossref_doi = archive_doi.gsub("http://dx.doi.org/", '')

  paper_day ||= Time.now.strftime('%d')
  paper_month ||= Time.now.strftime('%m')
  paper_year ||= @current_year
  paper_issue ||= @current_issue
  paper_volume ||= @current_volume

  `cd #{paper.directory} && pandoc \
  -V timestamp=#{Time.now.strftime('%Y%m%d%H%M%S')} \
  -V doi_batch_id=#{generate_doi_batch_id} \
  -V formatted_doi=#{paper.formatted_doi} \
  -V archive_doi=#{archive_doi} \
  -V review_issue_url=#{paper.review_issue_url} \
  -V paper_url=#{paper.pdf_url} \
  -V joss_resource_url=#{paper.joss_resource_url} \
  -V journal_alias=#{ENV['JOURNAL_ALIAS']} \
  -V journal_abbrev_title=#{ENV['JOURNAL_ABBRV_TITLE']} \
  -V journal_url=#{ENV['JOURNAL_URL']} \
  -V journal_name='#{ENV['JOURNAL_NAME']}' \
  -V journal_issn=#{ENV['JOURNAL_ISSN']} \
  -V citations='#{citations}' \
  -V authors='#{authors}' \
  -V month=#{paper_month} \
  -V day=#{paper_day} \
  -V year=#{paper_year} \
  -V issue=#{paper_issue} \
  -V volume=#{paper_volume} \
  -V page=#{paper.review_issue_id} \
  -V title='#{paper.plain_title}' \
  -f markdown #{File.basename(paper.paper_path)} -o #{paper.filename_doi}.crossref.xml \
  --template #{cross_ref_template_path}`

  if File.exists?("#{paper.directory}/#{paper.filename_doi}.crossref.xml")
    "#{paper.directory}/#{paper.filename_doi}.crossref.xml"
  else
    abort("Looks like we failed to compile the Crossref XML")
  end
end

#crossref_from_markdown(paper_issue = nil, paper_volume = nil, paper_year = nil, paper_month = nil, paper_day = nil) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/whedon/compilers.rb', line 253

def crossref_from_markdown(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  cross_ref_template_path = "#{Whedon.resources}/crossref.template"
  bibtex = Whedon::BibtexParser.new(paper.bibtex_path)

  # Pass the citations that are actually in the paper to the CrossRef
  # citations generator.

  citations_in_paper = File.read(paper.paper_path).scan(/@[\w|\-|:|_|\/|\+]+/)
  citations = bibtex.generate_citations(citations_in_paper)
  authors = paper.crossref_authors
  # TODO fix this when we update the DOI URLs
  # crossref_doi = archive_doi.gsub("http://dx.doi.org/", '')

  url = "#{ENV['JOURNAL_URL']}/papers/lookup/#{@review_issue_id}"
  response = RestClient.get(url)
  parsed = JSON.parse(response)
   = parsed['submitted']
  published = parsed['accepted']

  # If we have already published the paper then overwrite the year, volume, issue
  if published
    paper_day = generate_day(published)
    paper_month = generate_month(published)
    paper_year = generate_year(published)
    paper_issue = generate_issue(published)
    paper_volume = generate_volume(published)
  else
    paper_issue ||= @current_issue
    paper_volume ||= @current_volume
    paper_day ||= Time.now.strftime('%d')
    paper_month ||= Time.now.strftime('%m')
    paper_year ||= Time.now.strftime('%Y')
  end

   = {
    "timestamp" => Time.now.strftime('%Y%m%d%H%M%S'),
    "doi_batch_id" => generate_doi_batch_id,
    "formatted_doi" => paper.formatted_doi,
    "repository_doi" => repository_doi,
    "data_doi" => data_doi,
    "book_doi" => book_doi,
    "docker_doi" => docker_doi,
    "book_exec_url" => book_exec_url,
    "review_issue_url" => paper.review_issue_url,
    "paper_url" => paper.pdf_url,
    "joss_resource_url" => paper.joss_resource_url,
    "journal_alias" => ENV['JOURNAL_ALIAS'],
    "journal_abbrev_title" => ENV['JOURNAL_ABBRV_TITLE'],
    "journal_url" => ENV['JOURNAL_URL'],
    "journal_name" => ENV['JOURNAL_NAME'],
    "journal_issn"=> ENV['JOURNAL_ISSN'],
    "month" => paper_month,
    "day" => paper_day,
    "year" => paper_year,
    "issue" => paper_issue,
    "volume" => paper_volume,
    "page" => paper.review_issue_id,
    "title" => paper.plain_title,
    "crossref_authors" => authors,
    "citations" => citations
  }

  File.open("#{paper.directory}/crossref-metadata.yaml", 'w') { |file| file.write(.to_yaml) }

  `cd #{paper.directory} && pandoc \
  -V title="#{paper.plain_title}" \
  -f markdown #{File.basename(paper.paper_path)} -o #{paper.filename_doi}.crossref.xml \
  --template #{cross_ref_template_path} \
  --metadata-file=crossref-metadata.yaml`

  if File.exists?("#{paper.directory}/#{paper.filename_doi}.crossref.xml")
    doc = Nokogiri::XML(File.open("#{paper.directory}/#{paper.filename_doi}.crossref.xml", "r"), &:noblanks)
    File.open("#{paper.directory}/#{paper.filename_doi}.crossref.xml", 'w') {|f| f.write(doc.to_xml(:indent => 2))}
    "#{paper.directory}/#{paper.filename_doi}.crossref.xml"
  else
    abort("Looks like we failed to compile the Crossref XML")
  end
end

#generate_crossref(paper_issue = nil, paper_volume = nil, paper_year = nil, paper_month = nil, paper_day = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/whedon/compilers.rb', line 17

def generate_crossref(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  if paper.latex_source?
    crossref_from_latex(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  elsif paper.markdown_source?
    crossref_from_markdown(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  end
end

#generate_day(date) ⇒ Object



101
102
103
104
# File 'lib/whedon/compilers.rb', line 101

def generate_day(date)
  parsed = Date.parse(date)
  return parsed.day
end

#generate_issue(date) ⇒ Object



81
82
83
84
# File 'lib/whedon/compilers.rb', line 81

def generate_issue(date)
  parsed = Date.parse(date)
  return 1 + ((parsed.year * 12 + parsed.month) - (Time.parse(ENV['JOURNAL_LAUNCH_DATE']).year * 12 + Time.parse(ENV['JOURNAL_LAUNCH_DATE']).month))
end

#generate_jats(paper_issue = nil, paper_volume = nil, paper_year = nil, paper_month = nil, paper_day = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/whedon/compilers.rb', line 25

def generate_jats(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  if paper.latex_source?
    jats_from_latex(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  elsif paper.markdown_source?
    jats_from_markdown(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  end
end

#generate_month(date) ⇒ Object



96
97
98
99
# File 'lib/whedon/compilers.rb', line 96

def generate_month(date)
  parsed = Date.parse(date)
  return parsed.month
end

#generate_pdf(custom_branch = nil, draft = true, paper_issue = nil, paper_volume = nil, paper_year = nil) ⇒ Object

Generate the paper PDF Optionally pass in a custom branch name as first param



9
10
11
12
13
14
15
# File 'lib/whedon/compilers.rb', line 9

def generate_pdf(custom_branch=nil, draft=true, paper_issue=nil, paper_volume=nil, paper_year=nil)
  if paper.latex_source?
    pdf_from_latex(custom_branch, draft, paper_issue, paper_volume, paper_year)
  elsif paper.markdown_source?
    pdf_from_markdown(custom_branch, draft, paper_issue, paper_volume, paper_year)
  end
end

#generate_volume(date) ⇒ Object



86
87
88
89
# File 'lib/whedon/compilers.rb', line 86

def generate_volume(date)
  parsed = Date.parse(date)
  return parsed.year - (Date.parse(ENV['JOURNAL_LAUNCH_DATE']).year - 1)
end

#generate_year(date) ⇒ Object



91
92
93
94
# File 'lib/whedon/compilers.rb', line 91

def generate_year(date)
  parsed = Date.parse(date)
  return parsed.year
end

#jats_from_latex(paper_issue = nil, paper_volume = nil, paper_year = nil, paper_month = nil, paper_day = nil) ⇒ Object



332
333
334
# File 'lib/whedon/compilers.rb', line 332

def jats_from_latex(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  "JATS from LaTeX"
end

#jats_from_markdown(paper_issue = nil, paper_volume = nil, paper_year = nil, paper_month = nil, paper_day = nil) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/whedon/compilers.rb', line 336

def jats_from_markdown(paper_issue=nil, paper_volume=nil, paper_year=nil, paper_month=nil, paper_day=nil)
  latex_template_path = "#{Whedon.resources}/latex.template"
  jats_template_path = "#{Whedon.resources}/jats.template"
  csl_file = "#{Whedon.resources}/jats.csl"


  url = "#{ENV['JOURNAL_URL']}/papers/lookup/#{@review_issue_id}"
  response = RestClient.get(url)
  parsed = JSON.parse(response)
   = parsed['submitted']
  published = parsed['accepted']

  # If we have already published the paper then overwrite the year, volume, issue
  if published
    paper_day = Date.parse(published).strftime('%d')
    paper_year = generate_year(published)
    paper_issue = generate_issue(published)
    paper_volume = generate_volume(published)
  else
    paper_day ||= Time.now.strftime('%d')
    paper_month ||= Time.now.strftime('%m')
    paper_year ||= Time.now.strftime('%Y')
  end

  # TODO: may eventually want to swap out the latex template
  `cd #{paper.directory} && pandoc \
  -V repository="#{repository_address}" \
  -V archive_doi="#{archive_doi}" \
  -V paper_url="#{paper.pdf_url}" \
  -V journal_name='#{ENV['JOURNAL_NAME']}' \
  -V journal_issn=#{ENV['JOURNAL_ISSN']} \
  -V journal_abbrev_title=#{ENV['JOURNAL_ALIAS']} \
  -V graphics="true" \
  -V issue="#{paper_issue}" \
  -V volume="#{paper_volume}" \
  -V page="#{paper.review_issue_id}" \
  -V logo_path="#{Whedon.resources}/#{ENV['JOURNAL_ALIAS']}-logo.png" \
  -V month=#{paper_month} \
  -V day=#{paper_day} \
  -V year="#{paper_year}" \
  -V jats_authors='#{paper.jats_authors}' \
  -V jats_affiliations='#{paper.jats_affiliations}' \
  -t jats \
  -s \
  --citeproc \
  -o #{paper.filename_doi}.jats.xml  \
  #{File.basename(paper.paper_path)} \
  --template #{jats_template_path}`

  if File.exists?("#{paper.directory}/#{paper.filename_doi}.jats.xml")
    "#{paper.directory}/#{paper.filename_doi}.jats.xml"
  else
    abort("Looks like we failed to compile the JATS XML")
  end
end

#pdf_from_latex(custom_branch = nil, draft = true, paper_issue = nil, paper_volume = nil, paper_year = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/whedon/compilers.rb', line 33

def pdf_from_latex(custom_branch=nil, draft=true, paper_issue=nil, paper_volume=nil, paper_year=nil)
  # Optionally pass a custom branch name
  `cd #{paper.directory} && git checkout #{custom_branch} --quiet` if custom_branch

   = YAML.load_file("#{paper.directory}/paper.yml")

  for k in ["title", "authors", "affiliations", "keywords"]
    raise "Key #{k} not present in metadata" unless .keys().include?(k)
  end

  # Remove everything that shouldn't be there before processing

  `cd #{paper.directory} && rm -f *.aux \
  && rm -f *.blg && rm -f *.fls && rm -f *.log\
  && rm -f *.fdb_latexmk`

  open("#{paper.directory}/header.tex", 'w') do |f|
    f << "% **************GENERATED FILE, DO NOT EDIT**************\n\n"
    f << "\\title{#{metadata["title"]}}\n\n"
    for auth in ["authors"]
      f << "\\author[#{auth["affiliation"]}]{#{auth["name"]}}\n"
    end
    for aff in ["affiliations"]
      f << "\\affil[#{aff["index"]}]{#{aff["name"]}}\n"
    end
    f << "\n\\keywords{"
    for i in 0...["keywords"].length-1
      f << "#{metadata["keywords"][i]}, "
    end
    f << ["keywords"].last
    f << "}\n"
    
    # draft mode by default.
    if draft
      f << "\\usepackage{draftwatermark}\n\n"
    end
  end

  `cd #{paper.directory} && latexmk -f -bibtex -pdf paper.tex`

  if File.exists?("#{paper.directory}/paper.pdf")
    `mv #{paper.directory}/paper.pdf #{paper.directory}/#{paper.filename_doi}.pdf`
    puts "#{paper.directory}/#{paper.filename_doi}.pdf"
  else
    abort("Looks like we failed to compile the PDF")
  end
end

#pdf_from_markdown(custom_branch = nil, draft = true, paper_issue = nil, paper_volume = nil, paper_year = nil) ⇒ Object



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/whedon/compilers.rb', line 106

def pdf_from_markdown(custom_branch=nil, draft=true, paper_issue=nil, paper_volume=nil, paper_year=nil)
  latex_template_path = "#{Whedon.resources}/#{ENV['JOURNAL_ALIAS']}/latex.template"
  csl_file = "#{Whedon.resources}/#{ENV['JOURNAL_ALIAS']}/apa.csl"

  url = "#{ENV['JOURNAL_URL']}/papers/lookup/#{@review_issue_id}"
  response = RestClient.get(url)
  parsed = JSON.parse(response)
   = parsed['submitted']
  published = parsed['accepted']

  # TODO - remove this once JOSE has their editors hooked up in the system
  if ENV['JOURNAL_ALIAS'] == "neurolibre" && !paper.editor.nil?
    editor_lookup_url = "#{ENV['JOURNAL_URL']}/editors/lookup/#{paper.editor}"
    response = RestClient.get(editor_lookup_url)
    parsed = JSON.parse(response)
    editor_name = parsed['name']
    editor_url = parsed['url']
  else
    editor_name = "Pending Editor"
    editor_url = "http://example.com"
  end

  # If we have already published the paper then overwrite the year, volume, issue
  if published
    paper_year = generate_year(published)
    paper_issue = generate_issue(published)
    paper_volume = generate_volume(published)
  else
    published = Time.now.strftime('%d %B %Y')
    paper_year ||= @current_year
    paper_issue ||= @current_issue
    paper_volume ||= @current_volume
  end

  # Optionally pass a custom branch name
  `cd #{paper.directory} && git checkout #{custom_branch} --quiet` if custom_branch

   = {
    "repository" => repository_address,
    "repository_doi" => repository_doi,
    "data_doi" => data_doi,
    "book_doi" => book_doi,
    "docker_doi" => docker_doi,
    "book_exec_url" => book_exec_url,
    "paper_url" => paper.pdf_url,
    "journal_name" => ENV['JOURNAL_NAME'],
    "review_issue_url" => paper.review_issue_url,
    "issue" => paper_issue,
    "volume" => paper_volume,
    "page" => paper.review_issue_id,
    "logo_path" => "#{Whedon.resources}/#{ENV['JOURNAL_ALIAS']}/logo.png",
    "aas_logo_path" => "#{Whedon.resources}/#{ENV['JOURNAL_ALIAS']}/aas-logo.png",
    "year" => paper_year,
    "submitted" => ,
    "published" => published,
    "formatted_doi" => paper.formatted_doi,
    "citation_author" => paper.citation_author,
    "editor_name" => editor_name,
    "reviewers" => paper.reviewers_without_handles,
    "link-citations" => true
  }

  .merge!({"draft" => true}) if draft

  File.open("#{paper.directory}/markdown-metadata.yaml", 'w') { |file| file.write(.to_yaml) }

  `cd #{paper.directory} && pandoc \
  -V repository="#{repository_address}" \
  -V repository_doi="#{repository_doi}" \
  -V data_doi="#{data_doi}" \
  -V book_doi="#{book_doi}" \
  -V docker_doi="#{docker_doi}" \
  -V book_exec_url="#{book_exec_url}" \
  -V review_issue_url="#{paper.review_issue_url}" \
  -V editor_url="#{editor_url}" \
  -V graphics="true" \
  -o #{paper.filename_doi}.pdf -V geometry:margin=1in \
  --pdf-engine=xelatex \
  --citeproc #{File.basename(paper.paper_path)} \
  --from markdown+autolink_bare_uris \
  --csl=#{csl_file} \
  --template #{latex_template_path} \
  --metadata-file=markdown-metadata.yaml`

  if File.exists?("#{paper.directory}/#{paper.filename_doi}.pdf")
    puts "#{paper.directory}/#{paper.filename_doi}.pdf"
  else
    abort("Looks like we failed to compile the PDF :/")
  end
end