-
Notifications
You must be signed in to change notification settings - Fork 14
/
skim.rb
135 lines (110 loc) · 4.35 KB
/
skim.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
31
32
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
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
126
127
128
129
130
131
132
133
134
135
# encoding: UTF-8
$:.push(File.dirname($0))
require 'utility-functions'
require 'wiki-lib'
require 'appscript'
include Appscript
# grabs the name of the currently open Skim file, uses skimnotes to extract notes and highlights to a text file,
# and inserts this as a page, using the filename as a page name, in DokuWiki. intended to be turned into a service.
def process(type, text, page)
if type == "Underline"
if @out[-1] && @out[-1].index(text.strip)
@out << @out.pop.gsub(text.strip,"::::#{text.strip}::::")
else
type = "Underline-standalone"
end
end
return type == "Underline" ? nil : format(type, text, page)
end
def format(type, text, page)
highlight = case type
when "Text Note"
"::"
when "Underline-standalone"
":::"
else
""
end
text.strip!
text.gsub!(/[•]/, "\n * ")
return "#{highlight}#{text}#{highlight} [[skimx://#{Citekey}##{page}|p. #{page}]]\n\n"
end
##########################################################################################
skimdoc = app('Skim').documents[0]
skimdoc.save(skimdoc)
Citekey = skimdoc.name.get[0..-5]
filename = skimdoc.file.get.to_s
# ensure that the PDF is named the same as the cite key (inspired by Cresencia)
bibdesk_publication = try { app("BibDesk").document.search({:for =>Citekey})[0] }
unless try { bibdesk_publication.cite_key.get.to_s == Citekey }
fail "PDF filename doesn't match any citekeys in the BibDesk database. The name of a PDF file must be exactly " +
"the same as the citekey in the database + the .PDF extension, for export to function properly. Aborting."
end
fname = "/tmp/#{Citekey}.txt"
skimdoc.save(skimdoc, {:as => "Notes as Text", :in => fname})
#skimdoc.save(skimdoc, {:as => "Skim Notes", :in => "/tmp/skimnotes-binary-pdf"})
# `/Applications/Skim.app/Contents/SharedSupport/skimnotes get -format text #{filename}`
# make sure the metadata page is written
ensure_refpage(Citekey)
# if no annotations, we're done
unless File.size(fname) > 0
growl "No clippings","Skim did not export any data. Either you have not made any highlights, or there is "+
"an error (check the paths in settings.rb). Just creating the ref: page with metadata."
else
# process clippings and images
a = File.readlines(fname)
page = nil
@out = Array.new
type = ''
text=''
alltext = ''
a.each do |line|
if line =~ /^\* (Highlight|Text Note|Underline), page (.+?)$/
if page != nil # ie. don't execute the first time, only when there is a previous annotation in the system
p = process(type, text, page)
@out << p if p
end
page = $2.to_i
type = $1
text = ''
else
text << line.gsub(/([a-zA-Z])\- ([a-zA-Z])/, '\1\2') # just add the text (mend split words)
alltext << line
end
end
# calculate percentage of notes
File.write("/tmp/skimnote-tmp", alltext)
ntlines = `wc "/tmp/skimnote-tmp"`.split(" ")[1].to_f
`rm "/tmp/skimnote-tmp"`
`/usr/local/bin/pdftotext "#{filename}"`
ftlines = `wc "#{PDF_path}/#{Citekey}.txt"`.split(" ")[1].to_f
`rm "#{PDF_path}/#{Citekey}.txt"`
@out << process(type, text, page) # pick up the last annotation
percentage_text = ftlines.to_i > 0 ? " (#{(ntlines/ftlines*100).to_i}%)" : ""
outfinal = "h2. Highlights#{percentage_text}\n\n" + @out.join('')
File.write("/tmp/skimtmp", outfinal)
`#{Wiki_path}/bin/dwpage.php -m 'Automatically extracted from Skim' commit /tmp/skimtmp 'clip:#{Citekey}'`
if File.exists?("/tmp/skim-#{Citekey}-tmp")
a = File.readlines("/tmp/skim-#{Citekey}-tmp")
@out = "h2. Images\n\n"
c = 0
a.each do |line|
f,pg = line.split(",")
`mv "#{f.strip}" "#{Wiki_path}/data/media/skim/#{Citekey}#{c.to_s}.png"`
@out << "{{skim:#{Citekey}#{c.to_s}.png}}\n\n[[skimx://#{Citekey}##{pg.strip}|p. #{pg.strip}]]\n----\n\n"
c += 1
end
# `rm "/tmp/skim-#{Citekey}-tmp"`
File.open("/tmp/skimtmp", "w") {|f| f << @out}
`#{Wiki_path}/bin/dwpage.php -m 'Automatically extracted from Skim' commit /tmp/skimtmp 'skimg:#{Citekey}'`
end
#make_newimports_page([Citekey])
end
# open ref page in browser
`open #{Internet_path}/ref:#{Citekey}`
# update bibdesk fields
bibdesk_publication.fields["Read"].value.set("1")
bibdesk_publication.fields["Date-read"].value.set(Time.now.to_s)
# add to json cache and send to server if possible (if online)
add_to_jsonbib(Citekey)
try { scrobble(Citekey) }