Class: DocuBot::HTMLWriter

Inherits:
Writer
  • Object
show all
Defined in:
lib/docubot/writers/html.rb

Direct Known Subclasses

CHMWriter

Constant Summary

Constants inherited from Writer

Writer::DIR, Writer::HAML_OPTIONS, Writer::INSTALLED_WRITERS

Instance Method Summary collapse

Methods inherited from Writer

by_type, handles_type, #initialize

Constructor Details

This class inherits a constructor from DocuBot::Writer

Instance Method Details

#write(destination = nil) ⇒ Object

Specify nil for destination to place “<source>_html” next to the source.



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
# File 'lib/docubot/writers/html.rb', line 5

def write( destination=nil )
	start = Time.now
	
	source = @bundle.source
	@html_path = destination || File.dirname(source)/"#{File.basename source}_html"
	FileUtils.rm_rf(@html_path) if File.exists?(@html_path)
	FileUtils.mkdir(@html_path)
	
	master_templates = DocuBot::TEMPLATE_DIR
	source_templates = source/'_templates'
	master_root = master_templates/'_root'
	source_root = source_templates/'_root'
	
	# Copy any files found in the source directory that weren't made into pages
	@bundle.extras.each do |file|
		FileUtils.mkdir_p( @html_path / File.dirname( file ) )
		FileUtils.cp( source / file, @html_path / file )
	end
	
	# Copy files from template to root of destination
	# Record these as extras so that the CHMWriter can access them
	Dir.chdir @html_path do
		existing_files = Dir[ '*' ]
		FileUtils.copy( Dir[ master_templates/'_root'/'*' ], '.' )
		FileUtils.copy( Dir[ source_templates/'_root'/'*' ], '.' )
		new_files = Dir[ '*' ] - existing_files
		@bundle.extras.concat( new_files )
	end
	
	Dir.chdir @html_path do
		o = Object.new
		
		# Write out every page
		top = File.exists?( source_templates/'top.haml' ) ? source_templates/'top.haml' : master_templates/'top.haml'
		top = Haml::Engine.new( IO.read( top, encoding:'utf-8' ), HAML_OPTIONS )
		@bundle.toc.descendants.each do |node|
			next if node.anchor
			
			contents = node.page.to_html
			template = node.page.template # Call page.to_html first to ensure page.template is set

			custom_js = "#{template}.js"
			custom_js = nil unless File.exists?( source_root/custom_js ) || File.exists?( master_root/custom_js )
			
			custom_css = "#{template}.css"
			custom_css = nil unless File.exists?( source_root/custom_css ) || File.exists?( master_root/custom_css )
			
			variables = {
				:page       => node.page,
				:contents   => contents,
				:global     => @bundle.global,
				:root       => node.page.root,
				:breadcrumb => node.ancestors,
				:custom_js  => custom_js,
				:custom_css => custom_css
			}				
			html = top.render( o, variables )
			FileUtils.mkdir_p( File.dirname( node.file ) )
			File.open( node.file, 'w' ){ |f| f << html }
		end

		File.open( 'glossary-terms.js', 'w' ){ |f| f << @bundle.glossary.to_js }
	end
	
	puts "...%.2fs to write the HTML" % (Time.now - start)
end