5 URL = 'http://ardour.org/book/export/html/5848'
6 FILENAME = 'drupal-export.html'
10 GET_ARDOUR_ORG_IMAGES = false
11 HANDLE_OTHER_IMAGES = false
13 OUTPUT_DIR = '_manual'
18 'working_with_sessions' => 'sessions',
19 'export_stem' => 'export',
20 'track_groups' => 'track_bus_groups',
21 'vst_support' => 'windows_vst',
22 'kbd_default' => 'default_bindings',
23 'midistep_entry' => 'midi_step_entry',
24 'midi_stepentry' => 'midi_step_entry'
39 '/files/a3_mnemonic_cheatsheet.pdf' => '/files/ardour-2.8.3-bindings-x.pdf',
40 '/files/a3_mnemonic_cheat_sheet_osx.pdf' => '/files/ardour-2.8.3-bindings-osx-a4.pdf'
43 LINK_SLUG_TO_NODE_ID = {}
45 def link_slug_to_node_id(slug)
47 slug = SLUG_MAPPINGS[slug] || slug
49 return nil if MISSING_SLUGS.include? slug
51 LINK_SLUG_TO_NODE_ID[slug] ||= begin
52 filename = "tmp/slug-to-node/#{slug}"
54 if File.exists? filename
55 File.read(filename).to_i
57 url = "http://ardour.org/manual/#{slug}"
59 node_id = Nokogiri(open(url)).at('#content .node')['id'].sub(/^node\-/,'').to_i
60 File.open(filename,'w+') { |f| f << node_id }
67 def register_node(node_id, path)
68 filename = "tmp/node-to-path/#{node_id}"
69 File.open(filename,'w+') { |f| f << path } unless File.exists? filename
72 def node_id_to_path!(node_id)
73 filename = "tmp/node-to-path/#{node_id}"
74 return '' unless File.exists? filename
75 #raise "no path for node-id #{node_id}" unless File.exists? filename
79 def process(html, level = 1, path = [], numbered_path = [])
80 html.search("div.section-#{level}").each_with_index do |child, i|
82 title = child.at('h1.book-heading').inner_text
84 node_id = child['id'].sub(/^node\-/,'')
87 slug = title.downcase.gsub(' ','-').gsub(/[^a-z0-9\-]/, '')
89 root = slug == 'the-ardour3-manual'
96 this_numbered_path = []
98 numbered_slug = "%02d_%s" % [i + 1, slug, node_id]
100 this_path = path + [slug]
101 this_numbered_path = numbered_path + [numbered_slug]
104 register_node node_id, this_path.join('/')
106 indent = ' ' * level * 3
108 has_children = child.search("div.section-#{level + 1}").length > 0 #&& possible_children.any? { |child| child.search('div').length > 0 }
110 output_dir = "#{OUTPUT_DIR}/#{this_numbered_path.join('/')}"
114 "#{OUTPUT_DIR}/blah.html"
116 # "#{output_dir}/index.html"
123 content.search('h1.book-heading').remove
124 content.search("div.section-#{level + 1}").remove
126 if heading = content.at('h2') and heading.inner_text == title
130 #puts "processing links in [#{this_path.join('/')}]"
132 content.search('a').each do |a|
135 when /^\/manual\/(.*)/
137 if node_id = link_slug_to_node_id(slug)
138 link_path = node_id_to_path! node_id
139 #puts " link slug [#{slug}] -> #{node_id} -> #{link_path}"
140 a['href'] = "/#{link_path}"
142 a['href'] = "/missing"
145 when /^(\/files\/.*)/
151 if FILES_MAPPINGS[file_path]
152 file_path = FILES_MAPPINGS[file_path]
153 a['href'] = file_path
156 puts "downloading [#{file_path}] (for #{this_path.join('/')})"
158 filename = "#{FILES_DIR}/#{file_path}"
159 FileUtils.mkdir_p File.dirname(filename)
160 File.open(filename,'w+') { |f| f << open("http://ardour.org/#{file_path}").read }
165 content.search('img').each do |img|
171 if GET_ARDOUR_ORG_IMAGES
172 url = "http://ardour.org#{src}"
173 puts "getting #{url}"
174 img_path = "#{FILES_DIR}#{src}"
175 FileUtils.mkdir_p File.dirname(img_path)
176 File.open(img_path, 'w+') { |f| f << open(url).read }
179 new_src = '/' + src.sub(/^http:\/\/[^\/]+\//,'')
182 if HANDLE_OTHER_IMAGES
183 puts "new_src: #{new_src}"
184 img_path = "#{FILES_DIR}#{new_src}"
185 FileUtils.mkdir_p File.dirname(img_path)
186 puts "getting #{src}"
187 File.open(img_path, 'w+') { |f| f << open(src).read }
194 FileUtils.mkdir_p output_dir if has_children
195 File.open(output_file, 'w:UTF-8') do |f|
202 #{content.inner_html}
215 process(child, level + 1, this_path, this_numbered_path)
220 unless File.exists?(FILENAME)
221 puts "downloading #{URL} to #{FILENAME}"
222 File.open(FILENAME,'w+') { |f| f << open(URL).read }
225 FileUtils.mkdir_p('tmp/node-to-path')
226 FileUtils.mkdir_p('tmp/slug-to-node')
228 process Nokogiri(File.read(FILENAME))