class Smartdown::Api::Node

Attributes

elements[R]
front_matter[R]
markers[R]
name[R]
title[R]

Public Class Methods

new(node) click to toggle source
# File lib/smartdown/api/node.rb, line 7
def initialize(node)
  node_elements = node.elements.clone
  headings = node_elements.select { |element|
    element.is_a? Smartdown::Model::Element::MarkdownHeading
  }
  markers = node_elements.select { |element|
    element.is_a? Smartdown::Model::Element::Marker
  }
  markers.each { |marker| node_elements.delete(marker) }
  nb_questions = node_elements.select{ |element|
    element.class.to_s.include?("Smartdown::Model::Element::Question")
  }.count
  if headings.count > nb_questions
    node_elements.delete(headings.first) #Remove page title
    @title = headings.first.content.to_s
  end
  @markers = markers
  @elements = node_elements
  @front_matter = node.front_matter
  @name = node.name
end

Public Instance Methods

body() click to toggle source
# File lib/smartdown/api/node.rb, line 29
def body
  elements_before_smartdown = elements.take_while{|element| !smartdown_element?(element)}
  build_govspeak(elements_before_smartdown)
end
next_nodes() click to toggle source
# File lib/smartdown/api/node.rb, line 42
def next_nodes
  elements.select{ |element| next_node_element? element }
end
permitted_next_nodes() click to toggle source
# File lib/smartdown/api/node.rb, line 46
def permitted_next_nodes
  next_nodes
end
post_body() click to toggle source
# File lib/smartdown/api/node.rb, line 34
def post_body
  elements_after_smartdown = elements.select{ |element| !next_node_element?(element) }
                                     .reverse
                                     .take_while{|element| !smartdown_element?(element)}
                                     .reverse
  build_govspeak(elements_after_smartdown)
end

Private Instance Methods

build_govspeak(elements) click to toggle source
# File lib/smartdown/api/node.rb, line 65
def build_govspeak(elements)
  elements.select { |element| markdown_element?(element) }
  return nil if elements.empty?
  elements.map(&:content).join("")
end
markdown_element?(element) click to toggle source
# File lib/smartdown/api/node.rb, line 52
def markdown_element?(element)
  (element.is_a? Smartdown::Model::Element::MarkdownLine) ||
  (element.is_a? Smartdown::Model::Element::MarkdownHeading)
end
next_node_element?(element) click to toggle source
# File lib/smartdown/api/node.rb, line 57
def next_node_element?(element)
  (element.is_a? Smartdown::Model::NextNodeRules)
end
smartdown_element?(element) click to toggle source
# File lib/smartdown/api/node.rb, line 61
def smartdown_element?(element)
  !markdown_element?(element)
end