Class: Wiki2Html

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/wiki2html.rb

Constant Summary collapse

DEFAULT_SETTINGS =
{
	:views_to_copy => ['view','meta','rss'],
	:extension => '.html',
	:destination_dir => '/Users/tamc2/Sites',
	:destination_url => 'http://localhost/~tamc2'
}

Instance Method Summary collapse

Constructor Details

#initialize(wiki, view, settings = {}) ⇒ Wiki2Html

Returns a new instance of Wiki2Html.



10
11
12
13
14
15
16
# File 'lib/helpers/wiki2html.rb', line 10

def initialize( wiki, view, settings = {} )
	@settings = DEFAULT_SETTINGS.merge( settings )
	@wiki, @view = wiki, view
	@wiki.watch_for( :page_created, :page_deleted ) { |event, page| new_page( page ) }
	@wiki.watch_for( :page_revised ) { |event, page| page_revised( page ) }
	update_all_pages
end

Instance Method Details

#copy_all_views(pagename) ⇒ Object



35
36
37
# File 'lib/helpers/wiki2html.rb', line 35

def copy_all_views( pagename )
	@settings[:views_to_copy].each { |view| copy_view( pagename, view ) }
end

#copy_view(pagename, view) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/helpers/wiki2html.rb', line 39

def copy_view( pagename, view )
	$stderr.puts "Copying #{pagename} #{view}"
	html =  @view.view( pagename, view )
	update_links html
	destination = "#{@settings[:destination_dir]}/#{view}/#{pagename}#{@settings[:extension]}".downcase
	File.mkpath(File.dirname(destination)) unless File.exists?(File.dirname(destination))
	File.open( destination,'w') {|file| file.puts html}
end

#new_page(page) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/helpers/wiki2html.rb', line 18

def new_page( page )
	copy_all_views( page.name )
	titleregex = Regexp.new( page.name, Regexp::IGNORECASE )
	@wiki.each { |name, linkedpage | 
		page_revised( linkedpage ) if linkedpage.textile =~ titleregex
	}
end

#page_revised(page) ⇒ Object



26
27
28
29
# File 'lib/helpers/wiki2html.rb', line 26

def page_revised( page )
	copy_all_views( page.name )
	page.inserted_into.each { |including_page| copy_all_views( including_page.name ) }
end

#update_all_pagesObject



31
32
33
# File 'lib/helpers/wiki2html.rb', line 31

def update_all_pages
	@wiki.each { |pagename, page| copy_all_views( pagename) }
end


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/helpers/wiki2html.rb', line 48

def update_links( html )
	old_link = /['"]#{$SETTINGS[:url]}\/(.*?)\/(.*?)(\.\w*)?['"]/
	html.gsub!(old_link) do |match|
		if ($1.downcase == 'attachment' || @settings[:views_to_copy].include?( $1.downcase ))
			"'#{@settings[:destination_url]}\/#{$1.downcase}\/#{$2.downcase}#{$3 || @settings[:extension]}'"
		else
			match
		end
	end
	html
end