Class: Paru::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/paru/selector.rb

Overview

A Selector models a relationship between Pandoc AST nodes, such as parent-child or sibling. Selectors in paru are like CSS selectors, but more limited because the Pandoc AST is quite simple.

Given a selector expression, Selector determines if a node complies with that selector expression or not.

Constant Summary collapse

ANY_SELECTOR =

Pseudo selector to select any inline and block node

"*"
PSEUDO_SELECTORS =

All pseudo selectors

[ANY_SELECTOR]

Instance Method Summary collapse

Constructor Details

#initialize(selector) ⇒ Selector

Create a new Selector based on the selector string

Parameters:

  • selector (String)

    the selector string



44
45
46
47
48
# File 'lib/paru/selector.rb', line 44

def initialize(selector)
    @type = 'Unknown'
    @relations = []
    parse selector
end

Instance Method Details

#matches?(node, filtered_nodes) ⇒ Boolean

Does node get selected by this Selector in the context of the already filtered nodes?

Parameters:

  • node (Node)

    the node to check against this Selector

  • filtered_nodes (Array<Node>)

    the context of filtered nodes to take into account as well

Returns:

  • (Boolean)

    True if the node in the context of the filtered_nodes is selected by this Selector



59
60
61
62
63
64
65
66
67
68
# File 'lib/paru/selector.rb', line 59

def matches? node, filtered_nodes
    case @type 
    when ANY_SELECTOR
      Paru::PANDOC_TYPES.include? node.type
    else
      node.type == @type and 
          @classes.all? {|c| node.has_class? c } and    
          @relations.all? {|r| r.matches? node, filtered_nodes}
    end
end