class DocuBot::Page

Constants

AUTO_ID_ELEMENTS

Attributes

bundle[R]
file[R]
folder[R]
meta[R]
nokodoc[R]
type[R]

Public Class Methods

new( bundle, source_path, title=nil ) click to toggle source
# File lib/docubot/page.rb, line 19
def initialize( bundle, source_path, title=nil )
        puts "#{self.class}.new( #{source_path.inspect}, #{title.inspect}, #{type.inspect} )" if $DEBUG
        title ||= self.class.title( source_path )
        @bundle = bundle
        @file  = source_path
        if File.directory?( @file )
                @folder = @file
                @file   = Dir[ source_path/'index.*' ][0]
                # Directories without an index.* file now have nil @file
        else
                @folder = File.dirname( @file )
        end
        @type = File.extname( @file )[ 1..-1 ] if @file
        @meta = DocuBot::MetaSection.new( {'title'=>title}, @file )
        @raw  = @meta.__contents__
        @raw = nil if @raw && @raw.empty?

        create_nokodoc
end
title( source_path ) click to toggle source
# File lib/docubot/page.rb, line 10
def self.title( source_path )
  # File#basename might return the same string
        title = File.basename( source_path ).dup
        title.sub!(/\.[^.\s]+$/,'') unless File.directory?( source_path )
        title.gsub!( '_', ' ' )
        title.sub!( /^\d+\s+/, '' )
        title
end

Public Instance Methods

[]( key ) click to toggle source
# File lib/docubot/page.rb, line 92
def []( key )
        @meta[key]
end
auto_id() click to toggle source

Add IDs to elements that don’t have them

# File lib/docubot/page.rb, line 57
def auto_id
        # ...but only if a toc entry might reference one, or requested.
        if @meta['auto-id'].as_boolean || @meta.toc.as_list.any?{ |toc| !toc['#'] }
                @nokodoc.css( AUTO_ID_ELEMENTS ).each do |node|
                        next if node.has_attribute?('id')
                        # Strip off the unwanted leading '#'
                        node['id'] = DocuBot.id_from_text(node.inner_text)[1..-1]
                end
                dirty_doc
        end
end
auto_section() click to toggle source

Wrap siblings of headers in <div class=‘section’>

# File lib/docubot/page.rb, line 70
def auto_section
        return if @meta['auto-section']==false
        
        #TODO: Make this a generic nokogiri call on any node (like body) where you can pass in a hierarchy of elements and a wrapper
        stack = []
        @nokodoc.children.each do |node|
                # non-matching nodes will get level of 0
                level = node.name[ /h([1-6])/i, 1 ].to_i
                level = 99 if level == 0

                stack.pop while (top=stack.last) && top[:level]>=level
                stack.last[:div].add_child( node ) if stack.last
                if level<99
                        div = Nokogiri::XML::Node.new('div',@nokodoc)
                        div.set_attribute( 'class', 'section' )
                        node.add_next_sibling(div)
                        stack << { :div=>div, :level=>level }
                end
        end
        dirty_doc
end
children() click to toggle source
# File lib/docubot/page.rb, line 133
def children
        @bundle.toc.children(html_path).map{ |node| node.page }.uniq.compact
end
content_html() click to toggle source
# File lib/docubot/page.rb, line 129
def content_html
        @content_html ||= nokodoc.to_html
end
create_nokodoc() click to toggle source
# File lib/docubot/page.rb, line 39
def create_nokodoc
        # Directories with no index.* file will not have any @raw
        # Pages with metasection only will also not have any @raw
        html = if @raw && !@raw.empty?
                html = DocuBot::process_snippets( self, @raw )
                html = DocuBot::convert_to_html( self, html, @type )
        end
        @nokodoc = Nokogiri::HTML::DocumentFragment.parse(html || "")
        auto_id
        auto_section
        @nokodoc
end
depth() click to toggle source
# File lib/docubot/page.rb, line 141
def depth
        @depth ||= html_path.scan('/').length
end
dirty_doc() click to toggle source

Call this after modifying the structure of the nokodoc for the page

# File lib/docubot/page.rb, line 118
def dirty_doc
        @content_html = nil
end
dirty_source() click to toggle source

Call this if the source generated by the converter would change THIS DESTROYS ANY CUSTOM CHANGES YOU HAVE MADE TO THE NOKODOC

# File lib/docubot/page.rb, line 113
def dirty_source
        @nokodoc = nil
end
dirty_template() click to toggle source

Call this if the HTML generated by the page template needs to change e.g. for a glossary page.

# File lib/docubot/page.rb, line 124
def dirty_template
        # to_html doesn't cache anything at this point
        # so nothing needs to be done here
end
html_path() click to toggle source
# File lib/docubot/page.rb, line 107
def html_path
        @html_path ||= @file ? @file.sub( /[^.]+$/, 'html' ) : ( @folder / 'index.html' )
end
inspect() click to toggle source
# File lib/docubot/page.rb, line 163
def inspect
        "<#{self.class} '#{self.title}' #{@file ? "@file=#{@file.inspect}" : "@folder=#{@folder.inspect}"}>"
end
leaf?() click to toggle source
# File lib/docubot/page.rb, line 137
def leaf?
        @leaf ||= !children.any?{ |page| page != self }
end
method_missing( method, *args ) click to toggle source
Calls superclass method
# File lib/docubot/page.rb, line 96
def method_missing( method, *args )
        key=method.to_s
        case key[-1..-1] # the last character of the method name
                when '?' then @meta.has_key?( key[0..-2] )
                when '!','=' then super
                else
                        # warn "Unknown attribute #{key.inspect} asked for on #{@file || @folder}" unless @meta.has_key?( key )
                        @meta[ key ]
        end
end
root() click to toggle source
# File lib/docubot/page.rb, line 145
def root
        @root ||= "../" * depth
end
to_html() click to toggle source

TODO: cache this is people keep calling to_html and it’s a problem

# File lib/docubot/page.rb, line 150
def to_html
        @meta.template ||= leaf? ? 'page' : 'section'

        master_templates = DocuBot::TEMPLATE_DIR
        source_templates = @bundle.source / '_templates'
        tmpl = source_templates / "#{template}.haml"
        tmpl = master_templates / "#{template}.haml" unless File.exists?( tmpl )
        tmpl = master_templates / "page.haml"        unless File.exists?( tmpl )
        tmpl = IO.read( tmpl, encoding:'utf-8' )
        haml = Haml::Engine.new( tmpl, DocuBot::Writer::HAML_OPTIONS )
        haml.render( Object.new, :contents=>content_html, :page=>self, :global=>@bundle.global, :root=>root )
end