def test_no_formula_gets_lost_when_reparsing_external_formula_file(self): with htmlhandling.HtmlImageFormatter() as img: img.format_excluded(self.pos, "\\tau" * 999, "foo.png") with htmlhandling.HtmlImageFormatter() as img: img.format_excluded(self.pos, "\\pi" * 666, "foo_2.png") data = read(excl_filename) self.assertTrue("\\tau" in data) self.assertTrue("\\pi" in data)
def test_that_unicode_is_replaced_if_requested(self): with htmlhandling.HtmlImageFormatter("foo.html") as img: img.set_replace_nonascii(True) data = img.format(self.pos, "←", "foo.png") self.assertTrue( "\\leftarrow" in data, 'expected: "\\leftarrow" to be in "%s"' % data )
def test_that_too_long_formulas_get_outsourced_if_configured(self): with htmlhandling.HtmlImageFormatter() as img: img.set_max_formula_length(90) img.set_exclude_long_formulas(True) img.format(self.pos, "\\tau" * 999, "foo.png") self.assertTrue(os.path.exists(excl_filename)) data = read(htmlhandling.HtmlImageFormatter.EXCLUSION_FILE_NAME) self.assertTrue("\\tau\\tau" in data)
def test_that_displaymath_is_set_or_unset(self): with htmlhandling.HtmlImageFormatter("foo.html") as img: data = img.format(self.pos, r"\gamma\text{strahlung}", "foo.png", True) self.assertTrue('="displaymath' in data) data = img.format(self.pos, r"\gamma\text{strahlung}", "foo.png", False) self.assertTrue('="inlinemath' in data)
def test_that_alternative_css_class_is_set_correctly(self): with htmlhandling.HtmlImageFormatter("foo.html") as img: img.set_display_math_css_class("no1") img.set_inline_math_css_class("no2") data = img.format(self.pos, r"\gamma\text{strahlung}", "foo.png", True) self.assertTrue('="no1"' in data) data = img.format(self.pos, r"\gamma\text{strahlung}", "foo.png", False) self.assertTrue('="no2' in data)
def test_written_file_starts_and_ends_more_or_less_properly(self): with htmlhandling.HtmlImageFormatter('.') as img: img.format_excluded(self.pos, '\\tau\\tau', 'foo.png') data = read(htmlhandling.HtmlImageFormatter.EXCLUSION_FILE_NAME, 'r', encoding='utf-8') self.assertTrue('<html' in data and '</html>' in data) self.assertTrue('<body' in data and '</body>' in data) # make sure encoding is specified self.assertTrue('<meta' in data and 'charset=' in data)
def test_written_file_starts_and_ends_more_or_less_properly(self): with htmlhandling.HtmlImageFormatter(".") as img: img.format_excluded(self.pos, "\\tau\\tau", "foo.png") data = read(htmlhandling.HtmlImageFormatter.EXCLUSION_FILE_NAME, "r", encoding="utf-8") self.assertTrue("<html" in data and "</html>" in data) self.assertTrue("<body" in data and "</body>" in data) # make sure encoding is specified self.assertTrue("<meta" in data and "charset=" in data)
def test_formatting_commands_are_stripped(self): with htmlhandling.HtmlImageFormatter('foo.html') as img: data = img.format(self.pos, 'a\,b\,c\,d', 'foo.png') self.assertTrue('a b c d' in data) data = img.format(self.pos, 'a\,b\;c\ d', 'foo.png') self.assertTrue('a b c d' in data) data = img.format(self.pos, '\Big\{foo\Big\}', 'foo.png') self.assertTrue('\{foo' in data and '\}' in data) data = img.format(self.pos, r'\left\{foo\right\}', 'foo.png') self.assertTrue('\{' in data and 'foo' in data and '\}' in data)
def test_formatting_commands_are_stripped(self): with htmlhandling.HtmlImageFormatter("foo.html") as img: data = img.format(self.pos, "a\,b\,c\,d", "foo.png") self.assertTrue("a b c d" in data) data = img.format(self.pos, "a\,b\;c\ d", "foo.png") self.assertTrue("a b c d" in data) data = img.format(self.pos, "\Big\{foo\Big\}", "foo.png") self.assertTrue("\{foo" in data and "\}" in data) data = img.format(self.pos, r"\left\{foo\right\}", "foo.png") self.assertTrue("\{" in data and "foo" in data and "\}" in data)
def test_that_link_to_external_image_points_to_file_basepath_and_formula(self): os.mkdir("basepath") with htmlhandling.HtmlImageFormatter("basepath") as img: formatted_img = img.format_excluded(self.pos, "\\tau\\tau", "foo.png") expected_id = htmlhandling.gen_id("\\tau\\tau") # find linked formula path href = re.search('href="(.*?)"', formatted_img) self.assertTrue(href != None) # extract path and id from it self.assertTrue("#" in href.groups()[0]) path, id = href.groups()[0].split("#") self.assertEqual(path, "basepath/" + excl_filename) self.assertEqual(id, expected_id)
def test_that_link_to_external_image_points_to_file_and_formula(self): with htmlhandling.HtmlImageFormatter() as img: formatted_img = img.format_excluded(self.pos, "\\tau\\tau", "foo.png") expected_id = htmlhandling.gen_id("\\tau\\tau") external_file = read(excl_filename, "r", encoding="utf-8") # find linked formula path href = re.search('href="(.*?)"', formatted_img) self.assertTrue(href != None) # extract path and id from it self.assertTrue("#" in href.groups()[0]) path, id = href.groups()[0].split("#") self.assertEqual(path, excl_filename) self.assertEqual(id, expected_id) # check external file self.assertTrue("<p id" in external_file) self.assertTrue('="' + expected_id in external_file)
def test_that_unicode_is_kept_if_not_requested_to_replace(self): with htmlhandling.HtmlImageFormatter("foo.html") as img: img.set_replace_nonascii(False) data = img.format(self.pos, "←", "foo.png") self.assertTrue("←" in data)
def test_height_and_width_is_in_formatted_html_img_tag(self): data = None with htmlhandling.HtmlImageFormatter('foo.html') as img: data = img.get_html_img(self.pos, '\\tau\\tau', 'foo.png') self.assertTrue('height=' in data and str(self.pos['height']) in data) self.assertTrue('width=' in data and str(self.pos['width']) in data)
def test_that_negative_depth_results_in_positive_offset(self): self.pos["depth"] = "-999" with htmlhandling.HtmlImageFormatter("foo.html") as img: data = img.format(self.pos, r"\gamma\text{strahlung}", "foo.png") self.assertTrue("align: " + str(self.pos["depth"])[1:] in data)
def test_url_doesnt_contain_double_slashes(self): prefix = "http://crustulus.de/blog/" with htmlhandling.HtmlImageFormatter("foo.html") as img: img.set_url(prefix) data = img.format(self.pos, r"\gamma\text{strahlung}", "foo.png") self.assertFalse("//" in data.replace("http://", "ignore"))
def test_url_is_included(self): prefix = "http://crustulus.de/blog" with htmlhandling.HtmlImageFormatter("foo.html") as img: img.set_url(prefix) data = img.format(self.pos, "\epsilon<0", "foo.png") self.assertTrue(prefix in data)
def test_url_doesnt_contain_double_slashes(self): prefix = "http://crustulus.de/blog/" with htmlhandling.HtmlImageFormatter('foo.html') as img: img.set_url(prefix) data = img.format(self.pos, r'\gamma\text{strahlung}', 'foo.png') self.assertFalse('//' in data.replace('http://', 'ignore'))
def test_too_long_formulas_are_not_outsourced_if_not_configured(self): with htmlhandling.HtmlImageFormatter("foo.html") as img: img.format(self.pos, "\\tau" * 999, "foo.png") self.assertFalse(os.path.exists("foo.html"))
def test_that_negative_depth_results_in_positive_offset(self): self.pos['depth'] = '-999' with htmlhandling.HtmlImageFormatter('foo.html') as img: data = img.format(self.pos, r'\gamma\text{strahlung}', 'foo.png') self.assertTrue('align: ' + str(self.pos['depth'])[1:] in data)
def test_height_and_width_is_in_formatted_html_img_tag(self): data = None with htmlhandling.HtmlImageFormatter("foo.html") as img: data = img.get_html_img(self.pos, "\\tau\\tau", "foo.png") self.assertTrue("height=" in data and str(self.pos["height"]) in data) self.assertTrue("width=" in data and str(self.pos["width"]) in data)
def test_file_if_written_when_content_exists(self): with htmlhandling.HtmlImageFormatter() as img: img.format_excluded(self.pos, "\\tau\\tau", "foo.png") self.assertTrue(os.path.exists(excl_filename))
def test_that_no_file_is_written_if_no_content(self): with htmlhandling.HtmlImageFormatter("foo.html"): pass self.assertFalse(os.path.exists("foo.html"))