Class: Ruql::Html
- Inherits:
-
Object
- Object
- Ruql::Html
- Defined in:
- lib/ruql/html/html.rb,
lib/ruql/html/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(quiz, options = {}) ⇒ Html
constructor
A new instance of Html.
- #quiz_header ⇒ Object
- #render_answer_for_solutions(answer, raw, is_true_false = nil) ⇒ Object
- #render_fill_in(q, idx) ⇒ Object
- #render_multiple_choice(q, index) ⇒ Object
- #render_question_text(question, index) ⇒ Object
-
#render_questions ⇒ Object
this is what’s called when the HTML template yields:.
- #render_quiz ⇒ Object
- #render_random_seed ⇒ Object
- #render_with_template ⇒ Object
Constructor Details
#initialize(quiz, options = {}) ⇒ Html
Returns a new instance of Html.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ruql/html/html.rb', line 8 def initialize(quiz,={}) @gem_root = Gem.loaded_specs['ruql-html'].full_gem_path rescue '.' @show_solutions = .delete('--solutions') @show_tags = .delete('--html-tags') || .delete('--show-tags') @template = .delete('--html-template') || File.join(@gem_root, 'templates/simple.html.erb') @output = '' @quiz = quiz @h = Builder::XmlMarkup.new(:target => @output, :indent => 2) end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
6 7 8 |
# File 'lib/ruql/html/html.rb', line 6 def output @output end |
Class Method Details
.allowed_options ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruql/html/html.rb', line 19 def self. opts = [ ['--html-tags', GetoptLong::NO_ARGUMENT], ['--html-template', GetoptLong::REQUIRED_ARGUMENT] ] help = <<eos The HTML renderer uses supports these options: --html-tags Show question's tags in HTML output, within an element <div class="tags">. --html-template=file.html.erb Use file.html.erb as HTML template, which has <%= yield %> where questions should go. Default is #{@gem_root}/templates/simple.html.erb You can use the local variables in the .erb template: <%= quiz.title %> - the quiz title <%= quiz.num_questions %> - total number of questions <%= quiz.points %> - total number of points for whole quiz NOTE: If there is more than one quiz (collection of questions) in the file, a complete <html>...</html> block is produced in the output for EACH quiz. eos return [help, opts] end |
Instance Method Details
#quiz_header ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/ruql/html/html.rb', line 167 def quiz_header @h.div(:id => 'student-name') do @h.p 'Name:' @h.p 'Student ID:' end if @quiz.[:instructions] @h.div :id => 'instructions' do @quiz.[:instructions].each_line { |p| @h.p p } end end self end |
#render_answer_for_solutions(answer, raw, is_true_false = nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ruql/html/html.rb', line 119 def render_answer_for_solutions(answer,raw,is_true_false = nil) args = {:class => (answer.correct? ? 'correct' : 'incorrect')} if is_true_false answer.answer_text.prepend( answer.correct? ? "CORRECT: " : "INCORRECT: ") end @h.li(args) do if raw then @h.p { |p| p << answer.answer_text } else @h.p answer.answer_text end if answer.has_explanation? if raw then @h.p(:class => 'explanation') { |p| p << answer.explanation } else @h.p(answer.explanation, :class => 'explanation') end end end end |
#render_fill_in(q, idx) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ruql/html/html.rb', line 93 def render_fill_in(q, idx) render_question_text(q, idx) do if @show_solutions answer = q.answers[0] if answer.has_explanation? if q.raw? then @h.p(:class => 'explanation') { |p| p << answer.explanation } else @h.p(answer.explanation, :class => 'explanation') end end answers = (answer.answer_text.kind_of?(Array) ? answer.answer_text : [answer.answer_text]) @h.ol :class => 'answers' do answers.each do |answer| if answer.kind_of?(Regexp) answer = answer.inspect if !q.case_sensitive answer += 'i' end end @h.li do if q.raw? then @h.p { |p| p << answer } else @h.p answer end end end end end end end |
#render_multiple_choice(q, index) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruql/html/html.rb', line 73 def render_multiple_choice(q,index) render_question_text(q, index) do answers = if q.class == TrueFalse then q.answers.sort.reverse # True always first elsif q.randomize then q.answers.sort_by { rand } else q.answers end @h.ol :class => 'answers' do answers.each do |answer| if @show_solutions render_answer_for_solutions(answer, q.raw?, q.class == TrueFalse) else if q.raw? then @h.li { |l| l << answer.answer_text } else @h.li answer.answer_text end end end end end self end |
#render_question_text(question, index) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ruql/html/html.rb', line 134 def render_question_text(question,index) html_args = { :id => "question-#{index}", :class => ['question', question.class.to_s.downcase, (question.multiple ? 'multiple' : '')] .join(' ') } @h.li html_args do @h.div :class => 'text' do qtext = "[#{question.points} point#{'s' if question.points>1}] " << ('Select ALL that apply: ' if question.multiple).to_s << if question.class == FillIn then question.question_text.gsub(/\-+/, '_____________________________') else question.question_text end if @show_tags @h.div(:class => 'text') do question..join(',') end end if question.raw? @h.p { |p| p << qtext } else qtext.each_line do |p| @h.p do |par| par << p # preserves HTML markup end end end end yield # render answers end self end |
#render_questions ⇒ Object
this is what’s called when the HTML template yields:
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruql/html/html.rb', line 58 def render_questions render_random_seed @h.ol :class => 'questions' do @quiz.questions.each_with_index do |q,i| case q when MultipleChoice, SelectMultiple, TrueFalse then render_multiple_choice(q,i) when FillIn then render_fill_in(q, i) else raise Ruql::QuizContentError.new "Unknown question type: #{q}" end end end end |
#render_quiz ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ruql/html/html.rb', line 41 def render_quiz render_with_template do render_questions @output end self end |
#render_random_seed ⇒ Object
180 181 182 |
# File 'lib/ruql/html/html.rb', line 180 def render_random_seed @h.comment! "Seed: #{@quiz.seed}" end |
#render_with_template ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/ruql/html/html.rb', line 49 def render_with_template # local variables that should be in scope in the template quiz = @quiz # the ERB template includes 'yield' where questions should go: output = ERB.new(IO.read(File. @template)).result(binding) @output = output end |