class Spirit::Render::Processors::ProblemsProcessor

Pre-processes problem markup in YAML. Dependent on renderer#problems and renderer#nesting.

Constants

MARKER
REGEX

Paragraphs that start and end with +“”“+ are treated as embedded YAML and are parsed for questions/answers.

Attributes

problems[R]
renderer[R]
solutions[R]

Public Class Methods

new(renderer, *args) click to toggle source
# File lib/spirit/render/processors/problems_processor.rb, line 20
def initialize(renderer, *args)
  @renderer  = renderer
  @problems  = []
  @solutions = []
  renderer.problems = self
end

Public Instance Methods

filter(document) click to toggle source

Replaces YAML markup in document with <!– %%index%% –> @return [String] document

# File lib/spirit/render/processors/problems_processor.rb, line 29
def filter(document)
  document.gsub(REGEX) { problem $1 }
end
replace(html) click to toggle source
# File lib/spirit/render/processors/problems_processor.rb, line 33
def replace(html)
  return html unless is_marker? html
  replace_nesting html, renderer.nesting
  ''
end

Private Instance Methods

is_marker?(html) click to toggle source

@return [Boolean] true iff the given html corresponds to a problem

marker
# File lib/spirit/render/processors/problems_processor.rb, line 53
def is_marker?(html)
  html.strip =~ MARKER and problems[$1.to_i]
end
problem(text) click to toggle source

If the given text contains valid YAML, returns a marker. Otherwise, returns the original text. @param [String] text candidate YAML markup @return [String] text or marker

# File lib/spirit/render/processors/problems_processor.rb, line 61
def problem(text)
  p = Problem.parse(text)
  p.id = problems.size
  self.problems  << p
  self.solutions << {digest: p.digest, solution: Marshal.dump(p.answer)}
  Spirit.logger.record :problem, "ID: #{p.id}"
rescue RenderError
  text
else "<!-- %%#{p.id}%% -->"
end
replace_nesting(html, nesting) click to toggle source

Update associated problem with nesting information. @return [void]

# File lib/spirit/render/processors/problems_processor.rb, line 45
def replace_nesting(html, nesting)
  match = html.strip.match MARKER
  prob  = problems[match[1].to_i]
  prob.nesting = nesting.dup
end