Class: TracWiki::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/trac-export-wiki.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_title, category = nil) ⇒ Page

Returns a new instance of Page.



153
154
155
156
157
# File 'lib/trac-export-wiki.rb', line 153

def initialize(page_title, category = nil)
  self.page_title = page_title
  self.category = category
  self.filename = File.join(*[category, page_title.gsub(/([a-z])([A-Z])/,'\1-\2').split(/\?/).first.downcase + '.html'].compact)
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



151
152
153
# File 'lib/trac-export-wiki.rb', line 151

def category
  @category
end

#configObject

Returns the value of attribute config.



151
152
153
# File 'lib/trac-export-wiki.rb', line 151

def config
  @config
end

#filenameObject

Returns the value of attribute filename.



151
152
153
# File 'lib/trac-export-wiki.rb', line 151

def filename
  @filename
end

#page_titleObject

Returns the value of attribute page_title.



151
152
153
# File 'lib/trac-export-wiki.rb', line 151

def page_title
  @page_title
end

Instance Method Details

#export(config) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/trac-export-wiki.rb', line 159

def export(config)
  self.config = config
  
  # load the wiki page
 doc = Hpricot(read_asset(page_title))

  # search for each element and remove it from the doc
  Exporter::ELEMENTS_TO_REMOVE.each { |e| doc.search(e).remove }

  # set title
  doc.search("html > head").at("title").inner_html = "#{config.wiki_title_prefix} - " + page_title.gsub(/([a-z])([A-Z])/,'\1 \2')

  # add link to css
  updir = "../" * category.split(/\//).size
  css = %Q(<link rel="stylesheet" type="text/css" href="#{updir}style.css" />)
  charset = %Q(<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />)
  doc.search("html > head").append(css + charset)

  # give toc's parent ol a class
  ol = doc.search("html > body > div.wiki-toc > ol").first
  ol.raw_attributes = ol.attributes.to_hash.merge('class' => 'top-most') unless ol.nil?

  # change the toc's li's class names
  doc.search("html > body > div.wiki-toc > ol").search("li.active").set(:class => 'toc') rescue nil

  # create category directory if it does not exist
  FileUtils.mkdir_p(File.dirname(filename)) rescue nil

  # find all images
  doc.search("//img").each do |img|
      imgfile = img.attributes['src']
      short_imgfile = File.basename(imgfile).split(/\?/).first

      # change image attribute in source
      img.raw_attributes = img.attributes.to_hash.merge("src" => File.join('images', short_imgfile))

      # make image directory
      outdir = File.join(File.dirname(filename), 'images')
      FileUtils.mkdir_p(outdir)

      # write image to file
      begin
        uri = URI.parse(config.base_url)
        contents = read_asset(imgfile, "#{uri.scheme}://#{uri.host}")
        File.open(File.join(outdir, short_imgfile), "wb") do |f|
            f.write(contents)
        end
      rescue OpenURI::HTTPError
      end
  end

  # write HTML to file
  File.open(filename, "w") { |f| f.write(doc.to_html) }
  print "wrote #{filename}... "
rescue StandardError => bang
  print "(Oops! " + bang.message + ") "
end