# A narrator can turn tips - generated by `Mumukit::Assistant` - # into a humanized text with the same information but # a bit more friendly. This text is called _explanation_. # # The narrator uses some internationalized random phrases, and it does provide # a seed as a construction argument to allow its testing. class Mumukit::Assistant::Narrator def initialize(seed) @seed = seed end # Generated a markdown explanation using the seeded phrases. Uses `I18n` to get # the appropriate locale. def compose_explanation(tips) "**#{explanation_introduction_phrase}**\n\n#{explanation_paragraphs(tips).join("\n\n")}\n\n#{retry_phrase}\n" end # Generates an html explantion. # See `compose_explanation` def compose_explanation_html(tips) Mumukit::ContentType::Markdown.to_html compose_explanation(tips) end def retry_phrase t :retry end def explanation_introduction_phrase t :introduction end def explanation_paragraphs(tips) tips.take(3).zip([:opening, :middle, :ending]).map do |tip, selector| send "explanation_#{selector}_paragraph", tip end end def explanation_opening_paragraph(tip) "#{tip.upcase_first}." end def explanation_middle_paragraph(tip) t :middle, tip: tip end def explanation_ending_paragraph(tip) t :ending, tip: tip end def self.random new seed(*5.times.map { random_index }) end def self.seed(r, i, o, m, e) { retry: r, introduction: i, opening: o, middle: m, ending: e } end private def t(key, args={}) I18n.t "narrator.#{key}_#{@seed[key]}", **args end def self.random_index (0..2).to_a.sample end end