Class: Coradoc::Markdown::Extension

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/markdown/model/extension.rb

Overview

Represents a kramdown extension

Extension syntax: options / Common extensions:

{::toc} - table of contents
{::options key="value" /} - parser options
{::comment}content{:/} - comment
{::nomarkdown}content{:/} - raw HTML passthrough

Constant Summary collapse

TYPES =

Known extension types

{
  toc: :toc,
  options: :options,
  comment: :comment,
  nomarkdown: :nomarkdown,
  ignore: :ignore,
  if: :conditional,
  endif: :conditional
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#serialize_content, #to_h, visit, #visit

Class Method Details

.comment(content = '') ⇒ Extension

Create a comment extension

Parameters:

  • content (String) (defaults to: '')

    Comment content

Returns:



48
49
50
# File 'lib/coradoc/markdown/model/extension.rb', line 48

def self.comment(content = '')
  new(name: :comment, content: content)
end

.nomarkdown(content) ⇒ Extension

Create a nomarkdown extension (passthrough)

Parameters:

  • content (String)

    Raw content to pass through

Returns:



55
56
57
# File 'lib/coradoc/markdown/model/extension.rb', line 55

def self.nomarkdown(content)
  new(name: :nomarkdown, content: content)
end

.options(options = {}) ⇒ Extension

Create an options extension

Parameters:

  • options (Hash) (defaults to: {})

    Parser options

Returns:



41
42
43
# File 'lib/coradoc/markdown/model/extension.rb', line 41

def self.options(options = {})
  new(name: :options, options: options)
end

.toc(options = {}) ⇒ Extension

Create a TOC extension

Parameters:

  • options (Hash) (defaults to: {})

    TOC options (levels, etc.)

Returns:



34
35
36
# File 'lib/coradoc/markdown/model/extension.rb', line 34

def self.toc(options = {})
  new(name: :toc, options: options)
end

Instance Method Details

#self_closing?Boolean

Check if this is a self-closing extension

Returns:

  • (Boolean)


68
69
70
# File 'lib/coradoc/markdown/model/extension.rb', line 68

def self_closing?
  content.nil? || content.empty?
end

#to_mdString

Convert to Markdown

Returns:

  • (String)


74
75
76
77
78
79
80
81
# File 'lib/coradoc/markdown/model/extension.rb', line 74

def to_md
  opts = options.empty? ? '' : " #{options_to_s}"
  if self_closing?
    "{::#{name}#{opts} /}"
  else
    "{::#{name}#{opts}}#{content}{:/}"
  end
end

#type?(type) ⇒ Boolean

Check if this is a specific extension type

Parameters:

  • type (Symbol)

    The extension type

Returns:

  • (Boolean)


62
63
64
# File 'lib/coradoc/markdown/model/extension.rb', line 62

def type?(type)
  name.to_sym == type
end