class Basic(unittest.TestCase): def setUp(self): self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>" self.p = Pynliner().from_string(self.html) def test_fromString(self): """Test 'fromString' constructor""" self.assertEqual(self.p.source_string, self.html) def test_get_soup(self): """Test '_get_soup' method""" self.p._get_soup() self.assertEqual(unicode(self.p.soup), self.html) def test_get_styles(self): """Test '_get_styles' method""" self.p._get_soup() self.p._get_styles() self.assertEqual(self.p.style_string, u'h1 { color:#ffcc00; }\n') self.assertEqual(unicode(self.p.soup), u'<h1>Hello World!</h1>') def test_apply_styles(self): """Test '_apply_styles' method""" self.p._get_soup() self.p._get_styles() self.p._apply_styles() attr_dict = dict(self.p.soup.contents[0].attrs) self.assertTrue('style' in attr_dict) self.assertEqual(attr_dict['style'], u'color: #fc0') def test_run(self): """Test 'run' method""" output = self.p.run() self.assertEqual(output, u'<h1 style="color: #fc0">Hello World!</h1>') def test_with_cssString(self): """Test 'with_cssString' method""" cssString = 'h1 {color: #f00;}' self.p.with_cssString(cssString) output = self.p.run() self.assertEqual(output, u'<h1 style="color: #f00">Hello World!</h1>') def test_fromString_complete(self): """Test 'fromString' complete""" output = pynliner.fromString(self.html) desired = u'<h1 style="color: #fc0">Hello World!</h1>' self.assertEqual(output, desired) def test_fromURL(self): """Test 'fromURL' constructor""" url = 'http://media.tannern.com/pynliner/test.html' p = Pynliner() with mock.patch.object(Pynliner, '_get_url') as mocked: mocked.return_value = u"""<?xml version='1.0' encoding='utf-8'?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test</title> <link rel="stylesheet" type="text/css" href="test.css"/> <style type="text/css">h1 {color: #fc0;}</style> </head> <body> <h1>Hello World!</h1> <p>:)</p> </body> </html>""" p.from_url(url) self.assertEqual(p.root_url, 'http://media.tannern.com') self.assertEqual(p.relative_url, 'http://media.tannern.com/pynliner/') p._get_soup() with mock.patch.object(Pynliner, '_get_url') as mocked: mocked.return_value = 'p {color: #999}' p._get_external_styles() self.assertEqual(p.style_string, "p {color: #999}") p._get_internal_styles() self.assertEqual(p.style_string, "p {color: #999}\nh1 {color: #fc0;}\n") p._get_styles() output = p.run() desired = u"""<?xml version='1.0' encoding='utf-8'?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test</title> </head> <body> <h1 style="color: #fc0">Hello World!</h1> <p style="color: #999">:)</p> </body> </html>""" self.assertEqual(output, desired) def test_overloaded_styles(self): html = '<style>h1 { color: red; } #test { color: blue; }</style>' \ '<h1 id="test">Hello world!</h1>' expected = '<h1 id="test" style="color: blue">Hello world!</h1>' output = Pynliner().from_string(html).run() self.assertEqual(expected, output) def test_unicode_content(self): html = u"""<h1>Hello World!</h1><p>\u2022 point</p>""" css = """h1 { color: red; }""" expected = u"""<h1 style="color: red">Hello World!</h1><p>\u2022 point</p>""" output = Pynliner().from_string(html).with_cssString(css).run() self.assertEqual(output, expected) def test_conditional_comments(self): html = "<!-- <normal> --><!--[if condition]><p>special</p><![endif]-->" expected = "<!-- <normal> --><!--[if condition]><p>special</p><![endif]-->" output = Pynliner(allow_conditional_comments=True).from_string(html).run() self.assertEqual(output, expected)
class Basic(unittest.TestCase): def setUp(self): self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>" self.p = Pynliner().from_string(self.html) def test_fromString(self): """Test 'fromString' constructor""" self.assertEqual(self.p.source_string, self.html) def test_get_soup(self): """Test '_get_soup' method""" self.p._get_soup() self.assertEqual(unicode(self.p.soup), self.html) def test_get_styles(self): """Test '_get_styles' method""" self.p._get_soup() self.p._get_styles() self.assertEqual(self.p.style_string, u'h1 { color:#ffcc00; }\n') self.assertEqual(unicode(self.p.soup), u'<h1>Hello World!</h1>') def test_apply_styles(self): """Test '_apply_styles' method""" self.p._get_soup() self.p._get_styles() self.p._apply_styles() attr_dict = dict(self.p.soup.contents[0].attrs) self.assertTrue('style' in attr_dict) self.assertEqual(attr_dict['style'], u'color: #fc0') def test_run(self): """Test 'run' method""" output = self.p.run() self.assertEqual(output, u'<h1 style="color: #fc0">Hello World!</h1>') def test_with_cssString(self): """Test 'with_cssString' method""" cssString = 'h1 {color: #f00;}' self.p.with_cssString(cssString) output = self.p.run() self.assertEqual(output, u'<h1 style="color: #f00">Hello World!</h1>') def test_fromString_complete(self): """Test 'fromString' complete""" output = pynliner.fromString(self.html) desired = u'<h1 style="color: #fc0">Hello World!</h1>' self.assertEqual(output, desired) def test_fromURL(self): """Test 'fromURL' constructor""" url = 'http://media.tannern.com/pynliner/test.html' p = Pynliner() with mock.patch.object(Pynliner, '_get_url') as mocked: mocked.return_value = u"""<?xml version='1.0' encoding='utf-8'?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test</title> <link rel="stylesheet" type="text/css" href="test.css"/> <style type="text/css">h1 {color: #fc0;}</style> </head> <body> <h1>Hello World!</h1> <p>:)</p> </body> </html>""" p.from_url(url) self.assertEqual(p.root_url, 'http://media.tannern.com') self.assertEqual(p.relative_url, 'http://media.tannern.com/pynliner/') p._get_soup() with mock.patch.object(Pynliner, '_get_url') as mocked: mocked.return_value = 'p {color: #999}' p._get_external_styles() self.assertEqual(p.style_string, "p {color: #999}") p._get_internal_styles() self.assertEqual(p.style_string, "p {color: #999}\nh1 {color: #fc0;}\n") p._get_styles() output = p.run() desired = u"""<?xml version='1.0' encoding='utf-8'?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>test</title> </head> <body> <h1 style="color: #fc0">Hello World!</h1> <p style="color: #999">:)</p> </body> </html>""" self.assertEqual(output, desired) def test_overloaded_styles(self): html = '<style>h1 { color: red; } #test { color: blue; }</style>' \ '<h1 id="test">Hello world!</h1>' expected = '<h1 id="test" style="color: blue">Hello world!</h1>' output = Pynliner().from_string(html).run() self.assertEqual(expected, output) def test_unicode_content(self): html = u"""<h1>Hello World!</h1><p>\u2022 point</p>""" css = """h1 { color: red; }""" expected = u"""<h1 style="color: red">Hello World!</h1><p>\u2022 point</p>""" output = Pynliner().from_string(html).with_cssString(css).run() self.assertEqual(output, expected)
target = sys.stdin else: target = open(sys.argv[1], 'r', encoding='utf-8') with target as f: #f_contents = f.read().replace('\r', '') f_contents = f.read() inliner = Pynliner() root = fromstring(f_contents) for element in root.iter('link'): if element.attrib['rel'] == 'stylesheet' and element.attrib['type'] == 'text/css': if target is sys.stdin: with open(path.join(environ['OTHER_SHEETS'], element.attrib['href'])) as cssf: cssf_contents = cssf.read() inliner.with_cssString(cssf_contents) else: with open(path.join(path.dirname(sys.argv[1]), element.attrib['href'])) as cssf: cssf_contents = cssf.read() inliner.with_cssString(cssf_contents) element.getparent().remove(element) for element in root.iter('div'): if 'class' in element.attrib and element.attrib['class'] == 'section': del element.attrib['class'] if 'ADOC_PYNLINE_COLORS' in environ: inliner.with_cssString("body { color: none; }\n") else: inliner.with_cssString("body {color: black; background: white;}")