Class: K4compiler::Markdown

Inherits:
Base
  • Object
show all
Defined in:
lib/k4compiler/compiler/markdown.rb

Constant Summary collapse

DEFAULT_MARKDOWN_OPTIONS =
{
  autolink: true,
  fenced_code_blocks: true,
}
DEFAULT_RENDER_OPTIONS =
{
  hard_wrap: true,
  link_attributes: {target: '_blank'}
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from K4compiler::Base

Class Method Details

.optionsHash

Returns:

  • (Hash)


38
39
40
# File 'lib/k4compiler/compiler/markdown.rb', line 38

def self.options
  return {}
end

Instance Method Details

#compile(src) ⇒ Object

compile

Parameters:

  • src (String|StringIO)

    Source of markdown.



44
45
46
47
48
49
# File 'lib/k4compiler/compiler/markdown.rb', line 44

def compile(src)
  src = src.read() if src.respond_to?(:read)
  markdown = Redcarpet::Markdown.new(renderer, markdown_options)
  rendered = markdown.render(src)
  return rendered
end

#configOpenStruct

Returns:

  • (OpenStruct)


74
75
76
# File 'lib/k4compiler/compiler/markdown.rb', line 74

def config
  return super.markdown
end

#markdown_optionsHash

Returns:

  • (Hash)


60
61
62
63
64
# File 'lib/k4compiler/compiler/markdown.rb', line 60

def markdown_options
  opt = DEFAULT_MARKDOWN_OPTIONS.dup
  opt = opt.update(config.markdown_options || {})
  return opt
end

#render_optionsHash

Returns:

  • (Hash)


67
68
69
70
71
# File 'lib/k4compiler/compiler/markdown.rb', line 67

def render_options
  opt = DEFAULT_RENDER_OPTIONS.dup
  opt = opt.update(config.render_options || {})
  return opt
end

#renderer*|MarkdownRenderer

Returns:



52
53
54
55
56
57
# File 'lib/k4compiler/compiler/markdown.rb', line 52

def renderer
  cls = config.renderer ||= MarkdownRenderer
  instance = cls.new(render_options)
  instance.config = config if instance.respond_to?(:config=)
  return instance
end