class Hoe::ManualGen::Page
Manual page-generation class
Constants
- DEFAULT_CONFIG
The default page configuration if none is specified.
- PAGE_WITH_YAML_HEADER
Pattern to match a source page with a YAML header
- TIDY_OPTIONS
Options to pass to libtidy
Attributes
The relative path to the base directory, for prepending to page paths
The PageCatalog
to which the page belongs
The page configuration, as read from its YAML header
The filters the page will use to render itself
The configured layouts directory as a Pathname object.
The raw source of the page
The Pathname object that specifys the page source file
Public Class Methods
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.
# File lib/hoe/manualgen.rb, line 114 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
Public Instance Methods
Clean up and return the given HTML source
.
# File lib/hoe/manualgen.rb, line 217 def cleanup( source ) require 'tidy' 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 HTML output from the page and return it.
# File lib/hoe/manualgen.rb, line 163 def generate( metadata ) content = self.generate_content( @source, metadata ) 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
Run the various filters on the given input and return the transformed content.
# File lib/hoe/manualgen.rb, line 195 def generate_content( input, metadata ) return @filters.inject( input ) do |source, filter| filter.process( source, self, metadata ) end end
Get (singleton) instances of the filters named in filterlist
and return them.
# File lib/hoe/manualgen.rb, line 236 def load_filters( filterlist ) filterlist.flatten.collect do |key| raise ArgumentError, "filter '#{key}' didn't load correctly" unless Hoe::ManualGen::PageFilter.derivatives.key?( key ) Hoe::ManualGen::PageFilter.derivatives[ key ].instance end end
Build the index relative to the receiving page and return it as a String
# File lib/hoe/manualgen.rb, line 246 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
Trim the YAML header from the provided page source
, convert it to a Ruby object, and return it.
# File lib/hoe/manualgen.rb, line 204 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
Return the page title as specified in the YAML options
# File lib/hoe/manualgen.rb, line 188 def title return self.config['title'] || self.sourcefile.basename end