class GithubToCanvasQuiz::Parser::Markdown::Question

Public Instance Methods

parse() click to toggle source

Parse the frontmatter/HTML from the Markdown document and return a Question and its associated Answers

# File lib/github_to_canvas_quiz/parser/markdown/question.rb, line 10
def parse
  Model::Question.new(
    course_id: frontmatter['course_id'],
    quiz_id: frontmatter['quiz_id'],
    id: frontmatter['id'],
    type: frontmatter['type'],
    sources: frontmatter['sources'],
    name: name,
    description: description,
    answers: answers,
    distractors: distractors
  )
end

Private Instance Methods

answers() click to toggle source

Each H2 and the content before the next H2 represent an answer

# File lib/github_to_canvas_quiz/parser/markdown/question.rb, line 45
def answers
  scanner = Helpers::NodeScanner.new(html)
  scanner.scan_before('h2')
  answers = []
  while scanner.check('h2')
    title = scanner.scan('h2').content
    unless frontmatter['type'] == 'matching_question' && title == 'Incorrect'
      nodes = scanner.scan_before('h2') || scanner.scan_rest
      answers << Parser::Markdown::Answer.for(frontmatter['type'], title, nodes)
    end
  end
  answers
end
description() click to toggle source

Description - contents between H1 and first H2

# File lib/github_to_canvas_quiz/parser/markdown/question.rb, line 38
def description
  scanner = Helpers::NodeScanner.new(html)
  scanner.scan_until('h1')
  scanner.scan_before('h2').to_html.strip
end
distractors() click to toggle source

Distractors only apply to incorrect answers for the matching_question type

# File lib/github_to_canvas_quiz/parser/markdown/question.rb, line 60
def distractors
  return [] unless frontmatter['type'] == 'matching_question'

  scanner = Helpers::NodeScanner.new(html)
  scanner.scan_before('h2')
  while scanner.check('h2')
    title = scanner.scan('h2').content
    nodes = scanner.scan_before('h2') || scanner.scan_rest
    return parse_text_from_nodes(nodes, 'li') if title == 'Incorrect'
  end

  []
end
html() click to toggle source

Convert the markdown to HTML for scanning

# File lib/github_to_canvas_quiz/parser/markdown/question.rb, line 27
def html
  @html ||= MarkdownConverter.new(markdown).to_html
end
name() click to toggle source

Name - contents of first H1

# File lib/github_to_canvas_quiz/parser/markdown/question.rb, line 32
def name
  scanner = Helpers::NodeScanner.new(html)
  scanner.scan_until('h1').last.content
end