Class: RedmineWikiExporter
- Inherits:
-
Object
- Object
- RedmineWikiExporter
- Defined in:
- lib/redmine_wiki_exporter.rb
Defined Under Namespace
Classes: WikiURL
Instance Method Summary collapse
- #agent ⇒ Object
- #exporting_path ⇒ Object
- #exporting_path=(path) ⇒ Object
- #homepage_wikiname ⇒ Object
- #homepage_wikiname=(wikiname) ⇒ Object
- #html_dir_name ⇒ Object
- #html_path ⇒ Object
- #image_dir_name ⇒ Object
- #image_path ⇒ Object
- #image_urls ⇒ Object
-
#initialize(root, project) ⇒ RedmineWikiExporter
constructor
A new instance of RedmineWikiExporter.
- #login(username, password) ⇒ Object
- #mkdirs ⇒ Object
- #page_name ⇒ Object
- #project ⇒ Object
- #root_url ⇒ Object
- #save(wiki_name) ⇒ Object
- #save_images ⇒ Object
- #save_page ⇒ Object
- #scrape ⇒ Object
- #scraping_selector ⇒ Object
- #scraping_selector=(selector) ⇒ Object
- #url_to_wikiname(url) ⇒ Object
- #wiki_urls ⇒ Object
Constructor Details
#initialize(root, project) ⇒ RedmineWikiExporter
Returns a new instance of RedmineWikiExporter.
78 79 80 81 82 |
# File 'lib/redmine_wiki_exporter.rb', line 78 def initialize(root, project) @root_url = root @project = project @agent = Mechanize.new end |
Instance Method Details
#agent ⇒ Object
44 45 46 |
# File 'lib/redmine_wiki_exporter.rb', line 44 def agent @agent end |
#exporting_path ⇒ Object
55 56 57 |
# File 'lib/redmine_wiki_exporter.rb', line 55 def exporting_path @exporting_path end |
#exporting_path=(path) ⇒ Object
58 59 60 |
# File 'lib/redmine_wiki_exporter.rb', line 58 def exporting_path=(path) @exporting_path = "#{path}/" end |
#homepage_wikiname ⇒ Object
48 49 50 |
# File 'lib/redmine_wiki_exporter.rb', line 48 def homepage_wikiname @homepage_wikiname end |
#homepage_wikiname=(wikiname) ⇒ Object
51 52 53 |
# File 'lib/redmine_wiki_exporter.rb', line 51 def homepage_wikiname=(wikiname) @homepage_wikiname = wikiname end |
#html_dir_name ⇒ Object
61 62 63 |
# File 'lib/redmine_wiki_exporter.rb', line 61 def html_dir_name "html" end |
#html_path ⇒ Object
64 65 66 |
# File 'lib/redmine_wiki_exporter.rb', line 64 def html_path "#{exporting_path}#{html_dir_name}/" end |
#image_dir_name ⇒ Object
67 68 69 |
# File 'lib/redmine_wiki_exporter.rb', line 67 def image_dir_name "images" end |
#image_path ⇒ Object
70 71 72 |
# File 'lib/redmine_wiki_exporter.rb', line 70 def image_path "#{html_path}#{image_dir_name}/" end |
#image_urls ⇒ Object
92 93 94 |
# File 'lib/redmine_wiki_exporter.rb', line 92 def image_urls agent.page.images.map {|img| img.src} end |
#login(username, password) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/redmine_wiki_exporter.rb', line 84 def login(username, password) agent.get("#{root_url}/login") login_form = agent.page.form_with(:action => "/login") login_form['username'] = username login_form['password'] = password login_form.submit end |
#mkdirs ⇒ Object
144 145 146 147 148 |
# File 'lib/redmine_wiki_exporter.rb', line 144 def mkdirs Dir::mkdir("#{exporting_path}") rescue nil Dir::mkdir("#{html_path}") rescue nil Dir::mkdir("#{image_path}") rescue nil end |
#page_name ⇒ Object
74 75 76 |
# File 'lib/redmine_wiki_exporter.rb', line 74 def page_name url_to_wikiname(agent.page.uri) end |
#project ⇒ Object
40 41 42 |
# File 'lib/redmine_wiki_exporter.rb', line 40 def project @project end |
#root_url ⇒ Object
36 37 38 |
# File 'lib/redmine_wiki_exporter.rb', line 36 def root_url @root_url end |
#save(wiki_name) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/redmine_wiki_exporter.rb', line 150 def save(wiki_name) url = "#{root_url}/projects/#{project}/wiki/#{wiki_name}" agent.get(url) mkdirs save_page save_images end |
#save_images ⇒ Object
96 97 98 99 100 101 |
# File 'lib/redmine_wiki_exporter.rb', line 96 def save_images agent.page.images.map {|i| i.url}.select {|url| url.include? "/attachments/download/"}.each do |url| image = agent.get(url) image.save("#{image_path}#{url_to_wikiname(url)}.png") end end |
#save_page ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/redmine_wiki_exporter.rb', line 103 def save_page header = RedmineWikiExporterModule::Constant.header = RedmineWikiExporterModule::Constant. doc = Hpricot(agent.page.body) wiki = (doc/"div.wiki") (wiki/:img).each do |img| if img[:src].to_s.include? "http" scanned = img[:src].to_s.scan(/.*\/attachments\/(.*)\/.*\.(.*)/).first image_filename = "#{scanned[0]}.#{scanned[1]}" img[:src] = "#{image_dir_name}/#{image_filename}" else img[:src] = "#{image_dir_name}/#{url_to_wikiname(img[:src])}.png" if image_urls.include? img[:src] end end (wiki/:a).each do |link| if link[:href].to_s.include? "/wiki/" link_wikiname = url_to_wikiname(link[:href]) if link_wikiname == homepage_wikiname page_filename = "../#{link_wikiname}.html" else page_filename = "#{link_wikiname}.html" end page_filename = "#{html_dir_name}/#{link_wikiname}.html" if url_to_wikiname(agent.page.uri) == homepage_wikiname link[:href] = "#{page_filename}" elsif link[:href].to_s.include? "/attachments/" scanned = link[:href].to_s.scan(/.*\/attachments\/(.*)\/.*\.(.*)/).first image_filename = "#{scanned[0]}.#{scanned[1]}" link[:href] = "#{image_dir_name}/#{image_filename}" end end if url_to_wikiname(agent.page.uri) == homepage_wikiname filename = "#{exporting_path}#{page_name}.html" else filename = "#{html_path}#{page_name}.html" end file = open(filename, "w") file.write(header + wiki.inner_html + ) file.close end |
#scrape ⇒ Object
174 175 176 177 178 |
# File 'lib/redmine_wiki_exporter.rb', line 174 def scrape wiki_urls.select(&scraping_selector).each do |url| save(url_to_wikiname(url)) end end |
#scraping_selector ⇒ Object
170 171 172 |
# File 'lib/redmine_wiki_exporter.rb', line 170 def scraping_selector @scraping_selector end |
#scraping_selector=(selector) ⇒ Object
167 168 169 |
# File 'lib/redmine_wiki_exporter.rb', line 167 def scraping_selector=(selector) @scraping_selector = selector end |
#url_to_wikiname(url) ⇒ Object
32 33 34 |
# File 'lib/redmine_wiki_exporter.rb', line 32 def url_to_wikiname(url) WikiURL.new(url).wikiname end |
#wiki_urls ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/redmine_wiki_exporter.rb', line 158 def wiki_urls return @wiki_urls if @wiki_urls url = "#{root_url}/projects/#{project}/wiki/index" agent.get(url) agent.page.links.map {|link| link.href } end |