Class: Hoe::ManualGen::Page
- Inherits:
-
Object
- Object
- Hoe::ManualGen::Page
- 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
-
#basepath ⇒ Object
readonly
The relative path to the base directory, for prepending to page paths.
-
#catalog ⇒ Object
readonly
The PageCatalog to which the page belongs.
-
#config ⇒ Object
readonly
The page configuration, as read from its YAML header.
-
#filters ⇒ Object
readonly
The filters the page will use to render itself.
-
#layouts_dir ⇒ Object
readonly
The configured layouts directory as a Pathname object.
-
#source ⇒ Object
readonly
The raw source of the page.
-
#sourcefile ⇒ Object
readonly
The Pathname object that specifys the page source file.
Instance Method Summary collapse
-
#cleanup(source) ⇒ Object
Clean up and return the given HTML
source
. -
#generate(metadata) ⇒ Object
Generate HTML output from the page and return it.
-
#generate_content(input, metadata) ⇒ Object
Run the various filters on the given input and return the transformed content.
-
#initialize(catalog, sourcefile, layouts_dir, basepath = '.') ⇒ Page
constructor
Create a new page-generator for the given
sourcefile
, which will use ones of the templates inlayouts_dir
as a wrapper. -
#load_filters(filterlist) ⇒ Object
Get (singleton) instances of the filters named in
filterlist
and return them. -
#make_index_html ⇒ Object
Build the index relative to the receiving page and return it as a String.
-
#read_page_config(source) ⇒ Object
Trim the YAML header from the provided page
source
, convert it to a Ruby object, and return it. -
#title ⇒ Object
Return the page title as specified in the YAML options.
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.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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 |
Instance Attribute Details
#basepath ⇒ Object (readonly)
The relative path to the base directory, for prepending to page paths
144 145 146 |
# File 'lib/hoe/manualgen.rb', line 144 def basepath @basepath end |
#catalog ⇒ Object (readonly)
The PageCatalog to which the page belongs
141 142 143 |
# File 'lib/hoe/manualgen.rb', line 141 def catalog @catalog end |
#config ⇒ Object (readonly)
The page configuration, as read from its YAML header
153 154 155 |
# File 'lib/hoe/manualgen.rb', line 153 def config @config end |
#filters ⇒ Object (readonly)
The filters the page will use to render itself
159 160 161 |
# File 'lib/hoe/manualgen.rb', line 159 def filters @filters end |
#layouts_dir ⇒ Object (readonly)
The configured layouts directory as a Pathname object.
150 151 152 |
# File 'lib/hoe/manualgen.rb', line 150 def layouts_dir @layouts_dir end |
#source ⇒ Object (readonly)
The raw source of the page
156 157 158 |
# File 'lib/hoe/manualgen.rb', line 156 def source @source end |
#sourcefile ⇒ Object (readonly)
The Pathname object that specifys the page source file
147 148 149 |
# File 'lib/hoe/manualgen.rb', line 147 def sourcefile @sourcefile end |
Instance Method Details
#cleanup(source) ⇒ Object
Clean up and return the given HTML source
.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/hoe/manualgen.rb', line 217 def cleanup( source ) require 'tidy' Tidy.open( TIDY_OPTIONS ) do |tidy| tidy..output_xhtml = true xml = tidy.clean( source ) errors = tidy.errors ( errors.join ) unless errors.empty? warn tidy.diagnostics if $DEBUG return xml end rescue LoadError => err $stderr.puts "No cleanup: " + err. return source end |
#generate(metadata) ⇒ Object
Generate HTML output from the page and return it.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/hoe/manualgen.rb', line 163 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.
195 196 197 198 199 |
# File 'lib/hoe/manualgen.rb', line 195 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.
236 237 238 239 240 241 242 |
# 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 |
#make_index_html ⇒ Object
Build the index relative to the receiving page and return it as a String
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# 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 |
#read_page_config(source) ⇒ Object
Trim the YAML header from the provided page source
, convert it to a Ruby object, and return it.
204 205 206 207 208 209 210 211 212 213 |
# 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 |
#title ⇒ Object
Return the page title as specified in the YAML options
188 189 190 |
# File 'lib/hoe/manualgen.rb', line 188 def title return self.config['title'] || self.sourcefile.basename end |