Sha256: b4a456380f15c205ad3e4f9e99a09d4f3b638783ea2e1c436a372fe04c4f724e

Contents?: true

Size: 1.82 KB

Versions: 18

Compression:

Stored size: 1.82 KB

Contents

require 'awestruct/handler_chain'
require 'awestruct/handlers/base_handler'
require 'awestruct/handlers/file_handler'
require 'awestruct/handlers/front_matter_handler'
require 'awestruct/handlers/interpolation_handler'
require 'awestruct/handlers/layout_handler'

require 'hpricot'

module Awestruct
  module Handlers
    class RestructuredtextHandler < BaseHandler

      CHAIN = Awestruct::HandlerChain.new( /\.(adoc|asciidoc)$/,
        Awestruct::Handlers::FileHandler,
        Awestruct::Handlers::FrontMatterHandler,
        Awestruct::Handlers::InterpolationHandler,
        Awestruct::Handlers::RestructuredtextHandler,
        Awestruct::Handlers::LayoutHandler
      )

      def initialize(site, delegate)
        super( site, delegate )
      end

      def simple_name
        File.basename( relative_source_path, '.rst' )
      end

      def output_filename
        simple_name + output_extension
      end

      def output_extension
        '.html' 
      end

      def content_syntax
        :rst
      end

      def rendered_content(context, with_layouts=true)
        content = delegate.rendered_content( context, with_layouts )

        hl = 1
        if front_matter['initial_header_level'].to_s =~ /^[1-6]$/
          hl = front_matter['initial_header_level']
        end
        rendered = ''
        begin
          doc = execute_shell( [ "rst2html",
                                 "--strip-comments",
                                 "--no-doc-title",
                                 " --initial-header-level=#{hl}" ].join(' '), 
                                 content )
          content = Hpricot( doc ).at( '/html/body/div[@class="document"]' ).inner_html.strip
          content = content.gsub( "\r", '' )
        rescue => e
          puts e
          puts e.backtrace
        end
        content
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
awestruct-0.4.8 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.7 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.6 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.5 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.4 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.3 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x10 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x9 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x8 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x7 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x6 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x5 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x4 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x3 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x2 lib/awestruct/handlers/restructuredtext_handler.rb
awestruct-0.4.2.x1 lib/awestruct/handlers/restructuredtext_handler.rb
awestructx-0.4.1.x1 lib/awestruct/handlers/restructuredtext_handler.rb