forked from puppetlabs/puppet-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linktree.rb
117 lines (105 loc) · 3.5 KB
/
linktree.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
#!/usr/bin/ruby
#
# Copyright 2010, Puppet Labs
# Michael DeHaan <[email protected]>
#
# Given any directory tree containing HTML files, output the list
# of files in the order that they would be best fed to htmldoc, so
# that PDF conversion of the files reads logically. NOTE: it turns
# out trying to do this by software is a BAD idea and our docs
# don't have a lot of structure. A better approach in the future
# is probably go from docbook -> both formats using something
# like Publican.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
require 'find'
require 'rubygems'
require 'nokogiri'
class Scanner
def initialize(tree,head)
@serial = 0
@serials = {} # { |hash,key| 0 }
@tree = tree
@tree = tree + "/" unless tree.match("\/$")
@head = head
pattern = /\.html$/
@candidates = get_candidates(@tree,pattern)
end
def run()
# first we sort candidates by depth so things further down in the tree appear later
sorted = get_sorted_candidates(@candidates)
serials = {}
serial = 0
# now we'll sort them by the order in which the lengths for each appear in our list
links = []
compute(sorted) { |candidate,link|
link = link.gsub("//","/")
unless link.nil? or links.include?(link) or link.include?("references")
links << link if File.exists?(link)
end
}
links = links.insert(0, @head)
links.join(' ')
end
def get_sorted_candidates(candidates)
candidates.sort { |a,b| depth_cmp(a,b,@head) }
end
def get_candidates(tree,pattern)
result = []
Find.find(tree) do |filename|
if File.file?(filename) && filename =~ pattern
result << filename
end
end
result
end
def depth_cmp(a,b,head)
if a == head
return -1
end
ca = a.count("/")
cb = b.count("/")
if ca != cb
ca <=> cb
else
a <=> b
end
end
def link_cleanup(dir, link)
if !link.match(/\.html$/)
nil
elsif link.include?("/trac") || link.include?("references") || link.include?("file::")
nil
elsif link.match(/^\./) && !link.match(/^\.\./)
dir + "/" + link.sub(".","")
elsif link.match(/^\//)
@tree + "/" + link
elsif link.match(/^\w/) && !link.match(/\@/) && !link.match(/^http:\/\//)
dir + "/" + link
else
nil
end
end
def compute(candidates)
candidates.each do |candidate|
doc = Nokogiri::HTML(open(candidate))
doc.css('a').each do |anchor|
link = link_cleanup(File.dirname(candidate),anchor['href'])
yield candidate,link unless link.nil?
end
end
end
end