forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClipboard Links to Markdown.dropzone
66 lines (62 loc) · 1.64 KB
/
Clipboard Links to Markdown.dropzone
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
#!/usr/bin/ruby
# Dropzone Destination Info
# Name: Parse links to Markdown
# Description: Parses the clipboard text for http(s) links, returns a Markdown reference list
# Handles: NSStringPboardType
# Creator: Brett Terpstra
# URL: http://brettterpstra.com
# IconURL: http://brettterpstra.com/destinations/icons/LinksToMarkdown.png
# Events: Clicked, Dragged
def clicked(was_dragged = false)
if was_dragged == false
$dz.begin("Parsing clipboard for links")
$dz.determinate(true)
input = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
else
$dz.begin("Parsing text for links")
$dz.determinate(true)
input = was_dragged
end
links = input.scan /(https?:\/\/[^ \n"]+)/m
if links.empty? then
$dz.finish("No links found")
$dz.url(false)
else
percent = 10
$dz.percent(percent)
norepeat = []
output = []
inc = 90 / links.length
links.each {|url|
domain = url[0].match(/https?:\/\/([^\/]+)/)
parts = domain[1].split('.')
name = case parts.length
when 1: parts[0]
when 2: parts[0]
else parts[1]
end
while norepeat.include? name
if name =~ / [0-9]$/
name.next!
else
name = name + " 2"
end
end
output << {'title' => name, 'link' => url }
norepeat.push name
percent += inc
$dz.percent(percent)
}
output = output.sort {|a,b| a['title'] <=> b['title']}
o = ""
output.each { |x|
o += "[#{x['title']}]: #{x['link']}\n"
}
$dz.finish("#{output.length} links found")
%x{echo "#{o}"|pbcopy}
$dz.url(false)
end
end
def dragged
clicked($items.to_s)
end