Class: Y::RenderRules::Builder
- Inherits:
-
Object
- Object
- Y::RenderRules::Builder
- 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
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
-
#initialize(marks_allowed:) ⇒ Builder
constructor
A new instance of Builder.
- #mark(name, **options) ⇒ Object
- #node(type, **options, &block) ⇒ Object
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
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
169 170 171 |
# File 'lib/y/rendering.rb', line 169 def marks @marks end |
#nodes ⇒ Object (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
188 189 190 191 192 |
# File 'lib/y/rendering.rb', line 188 def mark(name, **) raise ArgumentError, "marks are ProseMirror-only" unless @marks_allowed @marks[name.to_s] = 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, **, &block) @nodes[type.to_s] = if block && .empty? block elsif block .merge(render: block) else end end |