def test_html2latex(self): """ We correctly translate a question to the latex equivalent. """ translation = Question2Tex.html2latex("<filetype> ?") self.assertEqual("<filetype> ?", translation) translation = Question2Tex.html2latex("Is <strong>42</strong> true ?") self.assertEqual("Is \\textbf{42} true ?", translation) translation = Question2Tex.html2latex("<code>is(this).sparta</code>?") self.assertEqual("$is(this).sparta$?", translation)
def get_caption_specifics(self): caption = "%s '%s' (%s) " % ( _("for the question"), Question2Tex.html2latex(self.question.text), _("left"), ) caption += "%s '%s' (%s) " % ( _("in relation with the question"), Question2Tex.html2latex(self.other_question.text), _("right"), ) return caption
def treat_question(self, question): LOGGER.info("Treating, %s %s", question.pk, question.text) options = self.tconf.get(survey_name=self.survey.name, question_text=question.text) multiple_charts = options.get("multiple_charts") if not multiple_charts: multiple_charts = {"": options.get("chart")} question_synthesis = "" i = 0 for chart_title, opts in list(multiple_charts.items()): i += 1 if chart_title: # "" is False, by default we do not add section or anything mct = options["multiple_chart_type"] question_synthesis += "\\%s{%s}" % (mct, chart_title) tex_type = opts.get("type") if tex_type == "raw": question_synthesis += Question2TexRaw(question, **opts).tex() elif tex_type == "sankey": other_question_text = opts["question"] other_question = Question.objects.get(text=other_question_text) q2tex = Question2TexSankey(question, other_question=other_question) question_synthesis += q2tex.tex() elif tex_type in ["pie", "cloud", "square", "polar"]: q2tex = Question2TexChart(question, latex_label=i, **opts) question_synthesis += q2tex.tex() elif locate(tex_type) is None: msg = "{} '{}' {}".format( _("We could not render a chart because the type"), tex_type, _( "is not a standard type nor the path to an " "importable valid Question2Tex child class. " "Choose between 'raw', 'sankey', 'pie', 'cloud', " "'square', 'polar' or 'package.path.MyQuestion2Tex" "CustomClass'" ), ) LOGGER.error(msg) question_synthesis += msg else: q2tex_class = locate(tex_type) # The use will probably know what type he should use in his # custom class opts["type"] = None q2tex = q2tex_class(question, latex_label=i, **opts) question_synthesis += q2tex.tex() section_title = Question2Tex.html2latex(question.text) return """ \\clearpage{} \\section{%s} \\label{sec:%s} %s """ % ( section_title, question.pk, question_synthesis, )
def treat_question(self, question, survey): LOGGER.info("Treating, %s %s", question.pk, question.text) options = self.tconf.get(survey_name=self.survey.name, question_text=question.text) multiple_charts = options.get("multiple_charts") if not multiple_charts: multiple_charts = {"": options.get("chart")} question_synthesis = "" i = 0 for chart_title, opts in multiple_charts.items(): i += 1 if chart_title: # "" is False, by default we do not add section or anything mct = options["multiple_chart_type"] question_synthesis += "\%s{%s}" % (mct, chart_title) tex_type = opts.get("type") if tex_type == "raw": question_synthesis += Question2TexRaw(question, **opts).tex() elif tex_type == "sankey": other_question_text = opts["question"] other_question = Question.objects.get(text=other_question_text) q2tex = Question2TexSankey(question) question_synthesis += q2tex.tex(other_question) elif tex_type in ["pie", "cloud", "square", "polar"]: q2tex = Question2TexChart(question, latex_label=i, **opts) question_synthesis += q2tex.tex() elif locate(tex_type) is None: msg = "{} '{}' {}".format( _("We could not render a chart because the type"), tex_type, _("is not a standard type nor the path to an " "importable valid Question2Tex child class. " "Choose between 'raw', 'sankey', 'pie', 'cloud', " "'square', 'polar' or 'package.path.MyQuestion2Tex" "CustomClass'") ) LOGGER.error(msg) question_synthesis += msg else: q2tex_class = locate(tex_type) # The use will probably know what type he should use in his # custom class opts["type"] = None q2tex = q2tex_class(question, latex_label=i, **opts) question_synthesis += q2tex.tex() section_title = Question2Tex.html2latex(question.text) return u""" \\clearpage{} \\section{%s} \label{sec:%s} %s """ % (section_title, question.pk, question_synthesis)
def get_colors(self): """ Return a formatted string for a tikz pgf-pie chart.""" colors = [] for answer in self.cardinality: answer = Question2Tex.get_clean_answer(answer) try: colors.append(self.color[answer]) except (KeyError, ValueError): msg = "Color for '%s' not provided. You could " % answer msg += "add '%s: \"red!50\"', in your color config." % answer LOGGER.warning(msg) colors.append(settings.SURVEY_DEFAULT_PIE_COLOR) return "{%s}" % ", ".join(colors)
def get_results(self): """ Return a formated string for a tikz pgf-pie chart. """ pie = u"" for answer, cardinality in self.cardinality.items(): if not answer: ans = _("Left blank") ans = Question2Tex.get_clean_answer(answer) pie += "{}/{},".format(cardinality, ans) if not pie: return u"" final_answers = [] for answer in pie.split(","): if answer: final_answers.append(answer) return u" {}".format(u",\n ".join(final_answers))
def get_colors(self): """ Return a formated string for a tikz pgf-pie chart. :param Question question: The question.. :param Dict colors_dict: Color to use (String answer: String color) """ colors = [] for answer in self.cardinality: answer = Question2Tex.get_clean_answer(answer) try: colors.append(self.color[answer]) except (KeyError, ValueError): msg = u"Color for '%s' not provided. You could " % answer msg += "add '%s: \"red!50\"', in your color config." % answer LOGGER.warning(msg) colors.append(settings.SURVEY_DEFAULT_PIE_COLOR) return "{%s}" % ", ".join(colors)
def get_caption_specifics(self): return "%s '%s' " % (_("for the question"), Question2Tex.html2latex(self.question.text))
def test_tex(self): """ Question2Tex.tex() is abstract. """ question = self.survey.questions.get(text="Aèbc?") self.assertRaises(NotImplementedError, Question2Tex(question).tex)