-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpdf-xchange.rb
76 lines (61 loc) · 1.54 KB
/
pdf-xchange.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
#!/usr/bin/ruby
# encoding: UTF-8
$:.push(File.dirname($0))
require 'utility-functions'
require 'wiki-lib'
require 'iconv'
require 'appscript'
include Appscript
def format(type, text, page, citekey)
highlight = case type
when "Sticky Note"
"::"
when "Underline-standalone"
":::"
else
""
end
text.strip!
text.gsub!(/[•]/, "\n * ")
return "#{highlight}#{text}#{highlight} [[skimx://#{citekey}##{page}|p. #{page}]]\n\n"
end
def import_file(filename, citekey)
a = File.open(filename, 'rb:UTF-16LE')
c = a.read
ic = Iconv.new('UTF-8//IGNORE', 'UTF-16LE')
a = ic.iconv(c[1..-1])
a += "\n"
page = 0
type = ''
out = ''
text = ''
a.each_line do |line|
line = line.strip
if line =~ /Author: (.+?) Subject: (.+?) /
type = $2
next
end
if line =~ /^Page: (\d+?)/
page = $1.to_i
next
end
# empty line, check if content, if so, add to out
if line == '' || line == "----------------------------------------------------------------------------------------------------"
if text.size > 0 && type != ''
out << format(type, text, page, citekey)
end
text = ''
type = ''
next
end
text << line + " "
end
page = "clip:#{citekey}"
dwpage(page, out, "Automatically added text from PDF Expert")
end
puts "Looking for files to import from #{PDFExpert_path}."
Dir.glob(PDFExpert_path + "/*.txt").each do |file|
citekey = File.basename(file).remove(".txt")
import_file(file, citekey)
puts "Imported #{citekey}"
end