-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpdfexpert-import.rb
68 lines (55 loc) · 1.48 KB
/
pdfexpert-import.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
#!/usr/bin/ruby
# encoding: UTF-8
$:.push(File.dirname($0))
require 'utility-functions'
require 'wiki-lib'
require 'appscript'
include Appscript
def format(type, text, page, citekey)
highlight = case type
when "Text Note"
"::"
when "Underline-standalone"
":::"
else
""
end
text.strip!
text.gsub!(/[•]/, "\n * ")
text = text[1..-2]
return "#{highlight}#{text}#{highlight} [[skimx://#{citekey}##{page}|p. #{page}]]\n\n"
end
def import_file(filename, citekey)
a = File.read(filename)
page = 0
type = ''
out = ''
a.each_line do |line|
line = line.remove("\t").strip
case type
when 'Highlight'
out << format("Text", line, page, citekey)
when 'Underline'
out << format("Underline-standalone", line, page, citekey)
when 'Note'
out << format("Text Note", line, page, citekey)
when "and Note"
out << format("Text Note", line, page, citekey)
end
# capture page number
page = $1.to_i if line =~ /PAGE (\d+?)\:/
if ( line == "Highlight" || line == "Underline" || line == "Note" || line == "and Note")
type = line
else
type = ''
end
end
page = "ref:#{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