Class: Hoe::ManualGen::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/hoe/manualgen.rb

Overview

Manual page-generation class

Constant Summary collapse

DEFAULT_CONFIG =

The default page configuration if none is specified.

{
			'filters' => [ 'erb', 'textile' ],
			'layout'  => 'default.erb',
			'cleanup' => false,
}.freeze
PAGE_WITH_YAML_HEADER =

Pattern to match a source page with a YAML header

/
			\A---\s*$	# It should should start with three hyphens
			(.*?)		# ...have some YAML stuff
			^---\s*$	# then have another three-hyphen line,
			(.*)\Z		# then the rest of the document
/xm
TIDY_OPTIONS =

Options to pass to libtidy

{
			:show_warnings     => true,
			:indent            => true,
			:indent_attributes => false,
			:indent_spaces     => 4,
			:vertical_space    => true,
			:tab_size          => 4,
			:wrap_attributes   => true,
			:wrap              => 100,
			:char_encoding     => 'utf8'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog, sourcefile, layouts_dir, basepath = '.') ⇒ Page

Create a new page-generator for the given sourcefile, which will use ones of the templates in layouts_dir as a wrapper. The basepath is the path to the base output directory, and the catalog is the PageCatalog to which the page belongs.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hoe/manualgen.rb', line 99

def initialize( catalog, sourcefile, layouts_dir, basepath='.' )
	@catalog     = catalog
	@sourcefile  = Pathname.new( sourcefile )
	@layouts_dir = Pathname.new( layouts_dir )
	@basepath    = basepath

	rawsource = nil
	if Object.const_defined?( :Encoding )
		rawsource = @sourcefile.read( :encoding => 'UTF-8' )
	else
		rawsource = @sourcefile.read
	end
	@config, @source = self.read_page_config( rawsource )

	# $stderr.puts "Config is: %p" % [@config],
	# 	"Source is: %p" % [ @source[0,100] ]
	@filters = self.load_filters( @config['filters'] )

	super()
end

Instance Attribute Details

#basepathObject (readonly)

The relative path to the base directory, for prepending to page paths



129
130
131
# File 'lib/hoe/manualgen.rb', line 129

def basepath
  @basepath
end

#catalogObject (readonly)

The PageCatalog to which the page belongs



126
127
128
# File 'lib/hoe/manualgen.rb', line 126

def catalog
  @catalog
end

#configObject (readonly)

The page configuration, as read from its YAML header



138
139
140
# File 'lib/hoe/manualgen.rb', line 138

def config
  @config
end

#filtersObject (readonly)

The filters the page will use to render itself



144
145
146
# File 'lib/hoe/manualgen.rb', line 144

def filters
  @filters
end

#layouts_dirObject (readonly)

The configured layouts directory as a Pathname object.



135
136
137
# File 'lib/hoe/manualgen.rb', line 135

def layouts_dir
  @layouts_dir
end

#sourceObject (readonly)

The raw source of the page



141
142
143
# File 'lib/hoe/manualgen.rb', line 141

def source
  @source
end

#sourcefileObject (readonly)

The Pathname object that specifys the page source file



132
133
134
# File 'lib/hoe/manualgen.rb', line 132

def sourcefile
  @sourcefile
end

Instance Method Details

#cleanup(source) ⇒ Object

Clean up and return the given HTML source.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/hoe/manualgen.rb', line 202

def cleanup( source )
	require 'tidy'

	Tidy.path = '/usr/lib/libtidy.dylib'
	Tidy.open( TIDY_OPTIONS ) do |tidy|
		tidy.options.output_xhtml = true

		xml = tidy.clean( source )
		errors = tidy.errors
		error_message( errors.join ) unless errors.empty?
		warn tidy.diagnostics if $DEBUG
		return xml
	end
rescue LoadError => err
	$stderr.puts "No cleanup: " + err.message
	return source
end

#generate(metadata) ⇒ Object

Generate HTML output from the page and return it.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/hoe/manualgen.rb', line 148

def generate(  )
	content = self.generate_content( @source,  )

	layout = self.config['layout'].sub( /\.erb$/, '' )
	templatepath = @layouts_dir + "#{layout}.erb"
	template = nil
	if Object.const_defined?( :Encoding )
		template = ERB.new( templatepath.read(:encoding => 'UTF-8') )
	else
		template = ERB.new( templatepath.read )
	end

	page = self
	html = template.result( binding() )

	# Use Tidy to clean up the html if 'cleanup' is turned on, but remove the Tidy
	# meta-generator propaganda/advertising.
	html = self.cleanup( html ).sub( %r:<meta name="generator"[^>]*tidy[^>]*/>:im, '' ) if
		self.config['cleanup']

	return html
end

#generate_content(input, metadata) ⇒ Object

Run the various filters on the given input and return the transformed content.



180
181
182
183
184
# File 'lib/hoe/manualgen.rb', line 180

def generate_content( input,  )
	return @filters.inject( input ) do |source, filter|
		filter.process( source, self,  )
	end
end

#load_filters(filterlist) ⇒ Object

Get (singleton) instances of the filters named in filterlist and return them.



222
223
224
225
226
227
228
# File 'lib/hoe/manualgen.rb', line 222

def load_filters( filterlist )
	filterlist.flatten.collect do |key|
		raise ArgumentError, "filter '#{key}' could not be loaded" unless
			Hoe::ManualGen::PageFilter.derivatives.key?( key )
		Hoe::ManualGen::PageFilter.derivatives[ key ].instance
	end
end

#make_index_htmlObject

Build the index relative to the receiving page and return it as a String



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/hoe/manualgen.rb', line 232

def make_index_html
	items = [ '<div class="index">' ]

	@catalog.traverse_page_hierarchy( self ) do |type, title, path|
		case type
		when :section
			items << %Q{<div class="section">}
			items << %Q{<h3><a href="#{self.basepath + path}/">#{title}</a></h3>}
			items << '<ul class="index-section">'

		when :current_section
			items << %Q{<div class="section current-section">}
			items << %Q{<h3><a href="#{self.basepath + path}/">#{title}</a></h3>}
			items << '<ul class="index-section current-index-section">'

		when :section_end, :current_section_end
			items << '</ul></div>'

		when :entry
			items << %Q{<li><a href="#{self.basepath + path}.html">#{title}</a></li>}

		when :current_entry
			items << %Q{<li class="current-entry">#{title}</li>}

		else
			raise "Unknown index entry type %p" % [ type ]
		end

	end

	items << '</div>'

	return items.join("\n")
end

#read_page_config(source) ⇒ Object

Trim the YAML header from the provided page source, convert it to a Ruby object, and return it.



189
190
191
192
193
194
195
196
197
198
# File 'lib/hoe/manualgen.rb', line 189

def read_page_config( source )
	unless source =~ PAGE_WITH_YAML_HEADER
		return DEFAULT_CONFIG.dup, source
	end

	pageconfig = YAML.load( $1 )
	source = $2

	return DEFAULT_CONFIG.merge( pageconfig ), source
end

#titleObject

Return the page title as specified in the YAML options



173
174
175
# File 'lib/hoe/manualgen.rb', line 173

def title
	return self.config['title'] || self.sourcefile.basename
end