module Webgen::SourceHandler # Source handler for handling content files in Webgen Page Format. class Page include Webgen::WebsiteAccess include Base def initialize #:nodoc: website.blackboard.add_listener(:node_meta_info_changed?, method(:meta_info_changed?)) end # Create a page file from +parent+ and +path+. def create_node(parent, path) page = page_from_path(path) path.meta_info['lang'] ||= website.config['website.lang'] path.ext = 'html' if path.ext == 'page' super(parent, path) do |node| node.node_info[:sh_page_node_mi] = Webgen::Page.meta_info_from_data(path.io.data) node.node_info[:page] = page tmp_logger = website.logger website.logger = nil # disabling logging whiling creating fragment nodes website.cache.permanent[:page_sections] ||= {} sections = if path.changed? || !website.cache.permanent[:page_sections][node.absolute_lcn] website.blackboard.invoke(:parse_html_headers, render_node(node, 'content', [])) else website.cache.permanent[:page_sections][node.absolute_lcn] end website.cache.permanent[:page_sections][node.absolute_lcn] = sections website.blackboard.invoke(:create_fragment_nodes, sections, node, website.blackboard.invoke(:source_paths)[path.source_path], node.meta_info['fragments_in_menu']) website.logger = tmp_logger end end # Render the block called +block_name+ of the given +node+. The parameter +templates+ is set to # the default template chain for the given +node+ but you can assign a custom template chain (an # array of template nodes) if need arises. Return +nil+ if an error occurred. def render_node(node, block_name = 'content', templates = website.blackboard.invoke(:templates_for_node, node)) chain = [templates, node].flatten if chain.first.node_info[:page].blocks.has_key?(block_name) node.node_info[:used_nodes] << chain.first.absolute_lcn context = chain.first.node_info[:page].blocks[block_name].render(Webgen::ContentProcessor::Context.new(:chain => chain)) context.content else raise "Error rendering <#{node.absolute_lcn}>: no block named '#{block_name}' in <#{chain.first.absolute_lcn}>" end end alias_method :content, :render_node ####### private ####### # Checks if the meta information provided by the file in Webgen Page Format changed. def meta_info_changed?(node) path = website.blackboard.invoke(:source_paths)[node.node_info[:src]] return if node.node_info[:processor] != self.class.name || !path.changed? old_mi = node.node_info[:sh_page_node_mi] new_mi = Webgen::Page.meta_info_from_data(path.io.data) node.flag(:dirty_meta_info) if old_mi && old_mi != new_mi end end end