Class: Awestruct::Handlers::RestructuredtextHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/awestruct/handlers/restructuredtext_handler.rb

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from BaseHandler

#delegate, #site

Instance Method Summary collapse

Methods inherited from BaseHandler

#content_line_offset, #dependencies, #execute_shell, #front_matter, #inherit_front_matter, #input_mtime, #output_path, #path, #raw_content, #relative_source_path, #stale?, #to_chain

Constructor Details

#initialize(site, delegate) ⇒ RestructuredtextHandler

Returns a new instance of RestructuredtextHandler.



23
24
25
# File 'lib/awestruct/handlers/restructuredtext_handler.rb', line 23

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

Instance Method Details

#content_syntaxObject



39
40
41
# File 'lib/awestruct/handlers/restructuredtext_handler.rb', line 39

def content_syntax
  :rst
end

#output_extensionObject



35
36
37
# File 'lib/awestruct/handlers/restructuredtext_handler.rb', line 35

def output_extension
  '.html' 
end

#output_filenameObject



31
32
33
# File 'lib/awestruct/handlers/restructuredtext_handler.rb', line 31

def output_filename
  simple_name + output_extension
end

#rendered_content(context, with_layouts = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/awestruct/handlers/restructuredtext_handler.rb', line 43

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 = Nokogiri::HTML( doc ).at( '/html/body/div[@class="document"]' ).inner_html.strip
    content = content.gsub( "\r", '' )
  rescue => e
    $LOG.error e if $LOG.error?
    $LOG.error e.backtrace.join( "\n" ) if $LOG.error?
  end
  content
end

#simple_nameObject



27
28
29
# File 'lib/awestruct/handlers/restructuredtext_handler.rb', line 27

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