-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromark.rb
86 lines (70 loc) · 1.62 KB
/
chromark.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
#!/usr/bin/env ruby
class Bookmark
class Category
attr_reader :added, :moded, :name
def initialize(line)
m = line.match(/.*ADD_DATE="(.*)" LAST_MODIFIED="(.*)">(.*)<\/H3>/)
@added = m[1]
@moded = m[2]
@name = m[3]
@entries = []
end
def <<(entry)
@entries << entry
end
end
class Entry
attr_reader :href, :added, :name, :path
def initialize(line, path)
m = line.match(/<.*HREF="(.*)" ADD_DATE="(.*)".*>(.*)<\/A>/)
@href = m[1]
@added = m[2]
@name = m[3]
@path = path.dup
path.last << self
end
def path_str
@path_str ||= @path.size == 1 ? '/' : ('/' + @path[1..-1].map(&:name).join('/'))
end
def host
@host ||= URI.parse(href).host rescue nil
end
end
def initialize(argf)
@entries = []
@cates = []
@root = nil
argf.each do |line|
case line
when /\s*<DT><A/
e = Entry.new(line, @cates)
@entries << e
when /\s*<DT><H3/
c = Category.new(line)
@root ||= c
@cates << c
when /\s*<\/DL><p>/
@cates.pop
end
end
end
def dup_names
return @dup_names if @dup_names
@dup_names = {}
@entries.group_by(&:name).each do |name, es|
next if es.size == 1
@dup_names[name] = es.map(&:path_str)
end
@dup_names
end
def favorite_hosts
@favorite_hosts ||= @entries.group_by(&:host).
map{ |host, entries| [host, entries.size] }.
select{ |h| h[1] > 1 }.
sort{ |h1, h2| h2[1] <=> h1[1] }.
to_h
end
end
bm = Bookmark.new(ARGF)
pp bm.dup_names
pp bm.favorite_hosts