Class: Slippery::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/slippery/converter.rb

Overview

Main class that does the conversion from Markdown/Kramdown to Hexp. Subclass this for custom behavior.

See github.com/3/blob/master/lib/kramdown/element.rb for a list of types

Instance Method Summary collapse

Instance Method Details

#codeblockHexp::Node

Process a Kramdown :codeblock type element

Returns:

  • (Hexp::Node)


54
55
56
# File 'lib/slippery/converter.rb', line 54

def codeblock
  H[:pre, attr, H[:code, value]]
end

#codespanObject



87
88
89
# File 'lib/slippery/converter.rb', line 87

def codespan
  H[:code, value]
end

#convert(el) ⇒ Hexp::Node

Convert a Kramdown syntax tree into Hexp.

Examples:

markdown = "# Hello!\n\nChunky *bacon*!\n"
document = Kramdown::Document.new(markdown)
hexp = converter.convert(document.root)

Parameters:

  • el (Kramdown::Element)

    The root element to convert

Returns:

  • (Hexp::Node)


24
25
26
27
28
29
# File 'lib/slippery/converter.rb', line 24

def convert(el)
  #Kernel.p el ; exit
  @type, @value, @attr, @children, @options =
    el.type, el.value, el.attr, el.children, el.options
  send(type)
end

#convert_childrenArray<Hexp::Node>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert the children of the Kramdown element to Hexps

In other words, recurse down the tree. This will pass each child element into the converter.

Returns:

  • (Array<Hexp::Node>)


127
128
129
# File 'lib/slippery/converter.rb', line 127

def convert_children
  children.map {|ch| convert ch }.compact
end

#entityObject



83
84
85
# File 'lib/slippery/converter.rb', line 83

def entity
  value.char
end

#headerHexp::Node

Process a Kramdown :header type element

Returns:

  • (Hexp::Node)


45
46
47
# File 'lib/slippery/converter.rb', line 45

def header
  tag! "h#{options[:level]}".intern
end

#html_elementObject



79
80
81
# File 'lib/slippery/converter.rb', line 79

def html_element
  H[value.to_sym, attr, convert_children]
end

#rootHexp::Node

Process a Kramdown :root type element

Returns:

  • (Hexp::Node)


36
37
38
# File 'lib/slippery/converter.rb', line 36

def root
  H[:html, [H[:head], tag!(:body)]]
end

#smart_quoteObject



58
59
60
61
62
63
64
65
# File 'lib/slippery/converter.rb', line 58

def smart_quote
  {
    :lsquo => '',
    :rsquo => '',
    :ldquo => '',
    :rdquo => '',
  }[value]
end

#tag!(tag) ⇒ Hexp::Node

Create a Hexp::Node from the current element

Helper for when you want to turn the Kramdown element straight into a Hexp::Node with the same attributes, and a one-to-one mapping of the child elements.

Parameters:

  • tag (Symbol)

    The HTML tag to generate

Returns:

  • (Hexp::Node)


103
104
105
# File 'lib/slippery/converter.rb', line 103

def tag!(tag)
  H[tag, attr, convert_children]
end

#typographic_symObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/slippery/converter.rb', line 67

def typographic_sym
  {
    :hellip => '',
    :mdash  => '',
    :ndash  => '',
    :laquo  => '«',
    :raquo  => '»',
    :laquo_space => '« ',
    :raquo_space => ' »',
  }[value]
end

#xml_commentObject



91
# File 'lib/slippery/converter.rb', line 91

def xml_comment; end