forked from houshuang/folders2web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-text.rb
30 lines (27 loc) · 881 Bytes
/
convert-text.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# encoding: UTF-8
$:.push(File.dirname($0))
require 'utility-functions'
# grabs currently selected text, and transforms [@scardamalia2006knowledge] citations to APA citations. with
# onlylist, it only generates a bibliography, without it it creates in-text citations (Scardamalia, 2006) and
# a bibliography
onlylist = (ARGV[0] == 'onlylist')
bib = json_bib
citations = Hash.new
doc = utf8safe(pbpaste)
doc.scan( /(\[?\@[a-zA-Z]+[0-9]+[a-zA-Z]+\]?)/ ).each do |hit|
hit = hit[0]
hitnobraces = hit.gsub(/[\@\[\]]/,"")
if bib[hitnobraces]
citations[hitnobraces] = bib[hitnobraces]
if onlylist
doc.gsub!(hit, "")
else
doc.gsub!(hit, citations[hitnobraces][0] + ", " + citations[hitnobraces][1])
end
end
end
doc << "\n\n\References\n" unless onlylist
citations.sort.each do |item|
doc << item[1][2].gsub(/[\{\}]/,'') + "\n"
end
puts doc.strip