Module: Paru::PandocFilter::InnerMarkdown

Included in:
Caption, CodeBlock, Div, Figure, Header, Inline, MetaInlines, MetaValue, Para, Plain, RawBlock
Defined in:
lib/paru/filter/inner_markdown.rb

Overview

A mixin to add inner_markdown properties to Nodes for which it makes sense to have an inner_markdown. Only those nodes that have a clear identifiable Inline level content, have the #inner_markdown method. This are almost all Inline nodes (except Cite) and Block level nodes with Inline contents like Para or Header.

Instance Method Summary collapse

Instance Method Details

#inner_markdownString

Get the markdown representation of this Node's children.

Examples:

Replace all occurrences of “hello” by “world” in all paragraphs

Paru::Filter.run do
    with "Para" do |p|
        p.inner_markdown = p.inner_markdown.gsub "hello", "world"
    end
end         

Returns:

  • (String)

    the inner markdown representation of this Node



40
41
42
43
44
45
46
47
# File 'lib/paru/filter/inner_markdown.rb', line 40

def inner_markdown()
    if has_children?
        temp_doc = PandocFilter::Document.fragment @children
        AST2MARKDOWN << temp_doc.to_JSON
    elsif has_string?
        @string
    end
end

#inner_markdown=(markdown) ⇒ Object

Replace this Node's children with the Nodes represented by the markdown string

Examples:

Replace all occurrences of “hello” by “world” in all paragraphs

Paru::Filter.run do
    with "Para" do |p|
        p.inner_markdown = p.inner_markdown.gsub "hello", "world"
    end
end         

Parameters:

  • markdown (String)

    the markdown string to replace this Node's children



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/paru/filter/inner_markdown.rb', line 62

def inner_markdown=(markdown)
    if has_string?
        @string = markdown
    else
        if markdown.nil? or markdown.empty?
            @children = []
        else 
            json = MARKDOWN2JSON << markdown
            temp_doc = PandocFilter::Document.from_JSON json
            temp_doc.children.each {|c| c.parent = @parent}

            if has_inline?
                @children = temp_doc.children.first.children
            elsif has_block?
                @children = temp_doc.children
            else
                # Unknown; what to do here?
            end
        end
    end
end