Esempio n. 1
0
    def preview_chemcalc(self, get):
        """
        Render an html preview of a chemical formula or equation.  get should
        contain a key 'formula' and value 'some formula string'.

        Returns a json dictionary:
        {
           'preview' : 'the-preview-html' or ''
           'error' : 'the-error' or ''
        }
        """

        result = {'preview': '',
                  'error': ''}
        formula = get['formula']
        if formula is None:
            result['error'] = "No formula specified."
            return result

        try:
            result['preview'] = chemcalc.render_to_html(formula)
        except pyparsing.ParseException as p:
            result['error'] = "Couldn't parse formula: {0}".format(p)
        except Exception:
            # this is unexpected, so log
            log.warning(
                "Error while previewing chemical formula", exc_info=True)
            result['error'] = "Error while rendering preview"

        return result
Esempio n. 2
0
    def preview_chemcalc(self, get):
        """
        Render an html preview of a chemical formula or equation.  get should
        contain a key 'formula' and value 'some formula string'.

        Returns a json dictionary:
        {
           'preview' : 'the-preview-html' or ''
           'error' : 'the-error' or ''
        }
        """

        result = {'preview': '', 'error': ''}
        formula = get['formula']
        if formula is None:
            result['error'] = "No formula specified."
            return result

        try:
            result['preview'] = chemcalc.render_to_html(formula)
        except pyparsing.ParseException as p:
            result['error'] = "Couldn't parse formula: {0}".format(p)
        except Exception:
            # this is unexpected, so log
            log.warning("Error while previewing chemical formula",
                        exc_info=True)
            result['error'] = "Error while rendering preview"

        return result
Esempio n. 3
0
 def test_escaping(self):
     """
     Tests that invalid input is escaped.
     """
     test_string = "<script>f()</script>"
     out = render_to_html(test_string)
     correct = u'<span class="math"><span class="inline-error inline">&lt;script&gt;f()&lt;/script&gt;</span></span>'
     self.assertEqual(out, correct)
Esempio n. 4
0
 def test_render_eq4(self):
     test_string = "[H^+] + OH^- <-> (H2O)"  # with brackets
     out = render_to_html(test_string)
     correct = u'<span class="math">[H<sup>+</sup>]+OH<sup>-</sup>\u2194(H<sub>2</sub>O)</span>'
     self.assertEqual(out, correct)
Esempio n. 5
0
 def test_render_eq3(self):
     test_string = "H^+ + OH^- <= H2O"  # unsupported arrow
     out = render_to_html(test_string)
     correct = u'<span class="math"><span class="inline-error inline">H^+ + OH^- &lt;= H2O</span></span>'
     self.assertEqual(out, correct)
Esempio n. 6
0
 def test_render_eq2(self):
     test_string = "H^+ + OH^- <-> H2O"
     out = render_to_html(test_string)
     correct = u'<span class="math">H<sup>+</sup>+OH<sup>-</sup>\u2194H<sub>2</sub>O</span>'
     self.assertEqual(out, correct)
Esempio n. 7
0
 def test_render_simple_square_brackets(self):
     test_string = "[Ar]"
     out = render_to_html(test_string)
     correct = u'<span class="math">[Ar]</span>'
     self.assertEqual(out, correct)
Esempio n. 8
0
 def test_render_simple_round_brackets(self):
     test_string = "(Ar)"
     out = render_to_html(test_string)
     correct = u'<span class="math">(Ar)</span>'
     self.assertEqual(out, correct)
Esempio n. 9
0
 def test_render2(self):
     test_string = "CO2 + H2O + Fe(OH)3"
     out = render_to_html(test_string)
     correct = u'<span class="math">CO<sub>2</sub>+H<sub>2</sub>O+Fe(OH)<sub>3</sub></span>'
     self.assertEqual(out, correct)
Esempio n. 10
0
 def test_render9(self):
     test_string = "5[Ni(NH3)4]^2+ + 5/2SO4^2-"
     out = render_to_html(test_string)
     correct = u'<span class="math">5[Ni(NH<sub>3</sub>)<sub>4</sub>]<sup>2+</sup>+<sup>5</sup>&frasl;<sub>2</sub>SO<sub>4</sub><sup>2-</sup></span>'
     self.assertEqual(out, correct)
Esempio n. 11
0
 def test_render8(self):
     test_string = "H2O(s) + CO2"
     out = render_to_html(test_string)
     correct = u'<span class="math">H<sub>2</sub>O(s)+CO<sub>2</sub></span>'
     self.assertEqual(out, correct)
Esempio n. 12
0
 def test_render7(self):
     test_string = "5(H1H212)^70010- + 2H2O + 7/2HCl + H2O"
     out = render_to_html(test_string)
     correct = u'<span class="math">5(H<sub>1</sub>H<sub>212</sub>)<sup>70010-</sup>+2H<sub>2</sub>O+<sup>7</sup>&frasl;<sub>2</sub>HCl+H<sub>2</sub>O</span>'
     self.assertEqual(out, correct)
Esempio n. 13
0
 def test_render6(self):
     test_string = "7/2H^+ + 3/5OH^-"
     out = render_to_html(test_string)
     correct = u'<span class="math"><sup>7</sup>&frasl;<sub>2</sub>H<sup>+</sup>+<sup>3</sup>&frasl;<sub>5</sub>OH<sup>-</sup></span>'
     self.assertEqual(out, correct)
Esempio n. 14
0
 def test_render5(self):
     test_string = "Fe(OH)^2- + (OH)^-"
     out = render_to_html(test_string)
     correct = u'<span class="math">Fe(OH)<sup>2-</sup>+(OH)<sup>-</sup></span>'
     self.assertEqual(out, correct)
Esempio n. 15
0
 def test_render4(self):
     test_string = "H^+ + OH^-"
     out = render_to_html(test_string)
     correct = u'<span class="math">H<sup>+</sup>+OH<sup>-</sup></span>'
     self.assertEqual(out, correct)
Esempio n. 16
0
 def test_render_error(self):
     test_string = "5.2H20"
     out = render_to_html(test_string)
     correct = u'<span class="math"><span class="inline-error inline">5.2H20</span></span>'
     self.assertEqual(out, correct)
Esempio n. 17
0
 def test_render_uncorrect_reaction(self):
     test_string = "O2C + OH2"
     out = render_to_html(test_string)
     correct = u'<span class="math">O<sub>2</sub>C+OH<sub>2</sub></span>'
     self.assertEqual(out, correct)