Class: Y::RenderRules::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/y/rendering.rb

Overview

Yielded by Y::Lexical.new / Y::ProseMirror.new: register rules one call per node type. Keyword options are the markup-as-data; a block is the logic; both together say what a callback node contains and how to render it:

Y::ProseMirror.new(doc) do |rules|
rules.node "callout", tag: "aside", contains: :blocks
rules.node "video" do |node|
  %(<video src="#{RenderRules.escape_attr(node.attrs["src"])}"></video>)
end
rules.node "columns", contains: :blocks do |node|
  %(<div class="cols--#{node.child_types.length}">#{node.content}</div>)
end
rules.mark "comment", tag: "span", attrs: { "data-comment-id" => :id }
end

It compiles to the same rule hashes the nodes:/marks: keywords take, so both forms mean the same thing; the keywords remain the data form for shipping rule sets (Y::Lexxy::NODES is one).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(marks_allowed:) ⇒ Builder

Returns a new instance of Builder.



171
172
173
174
175
# File 'lib/y/rendering.rb', line 171

def initialize(marks_allowed:)
  @nodes = {}
  @marks = {}
  @marks_allowed = marks_allowed
end

Instance Attribute Details

#marksObject (readonly)

Returns the value of attribute marks.



169
170
171
# File 'lib/y/rendering.rb', line 169

def marks
  @marks
end

#nodesObject (readonly)

Returns the value of attribute nodes.



169
170
171
# File 'lib/y/rendering.rb', line 169

def nodes
  @nodes
end

Instance Method Details

#mark(name, **options) ⇒ Object

Raises:

  • (ArgumentError)


188
189
190
191
192
# File 'lib/y/rendering.rb', line 188

def mark(name, **options)
  raise ArgumentError, "marks are ProseMirror-only" unless @marks_allowed

  @marks[name.to_s] = options
end

#node(type, **options, &block) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/y/rendering.rb', line 177

def node(type, **options, &block)
  @nodes[type.to_s] =
    if block && options.empty?
      block
    elsif block
      options.merge(render: block)
    else
      options
    end
end