class CaseSensitive(unittest.TestCase): def setUp(self): self.pyn = Pynliner(case_sensitive=False) def test_case_sensitive_tag(self): # Test upper/lowercase tag names in style sheets html = '<style>H1 {color: #000;}</style><H1 style="color: #fff">Foo</H1><h1>Bar</h1>' desired_output = '<h1 style="color: #000; color: #fff">Foo</h1><h1 style="color: #000">Bar</h1>' output = self.pyn.from_string(html).run() self.assertEqual(output, desired_output) def test_case_sensitive_tag_class(self): # Test upper/lowercase tag names with class names html = '<style>h1.b1 { font-weight:bold; } H1.c {color: red}</style><h1 class="b1">Bold</h1><H1 class="c">Bold Red</h1>' desired_output = '<h1 class="b1" style="font-weight: bold">Bold</h1><h1 class="c" style="color: red">Bold Red</h1>' output = self.pyn.from_string(html).run() self.assertEqual(output, desired_output) def test_case_sensitive_tag_id(self): # Test case sensitivity of tags with class names html = '<style>h1#tst { font-weight:bold; } H1#c {color: red}</style><h1 id="tst">Bold</h1><H1 id="c">Bold Red</h1>' desired_output = '<h1 id="tst" style="font-weight: bold">Bold</h1><h1 id="c" style="color: red">Bold Red</h1>' output = self.pyn.from_string(html).run() self.assertEqual(output, desired_output) def test_case_sensitive_class(self): # Test case insensitivity of class names html = '<style>h1.BOLD { font-weight:bold; }</style><h1 class="bold">Bold</h1><h1 class="BOLD">Bold</h1>' desired_output = '<h1 class="bold" style="font-weight: bold">Bold</h1><h1 class="BOLD" style="font-weight: bold">Bold</h1>' output = self.pyn.from_string(html).run() self.assertEqual(output, desired_output)
def ansi_output_to_html(ansi_text, log=None): try: converter = Ansi2HTMLConverter() html = converter.convert(ansi_text) except IOError as e: if re.search("templates/header.mak", str(e)): print e raise Exception( "Your installation of ansi2html is missing some template files, please try 'pip install --upgrade ansi2html' or install from source." ) raise e try: p = Pynliner(log) if not log: # put after call to Pynliner() so it doesn't print in case of error print """a custom log has not been passed to dexy.utils.ansi_output_to_html, harmless but annoying CSS errors will appear on the console.""" except TypeError: print "========== Start of harmless but annoying CSS errors..." print "You can install pynliner from source (https://github.com/rennat/pynliner.git) or version > 0.2.1 to get rid of these" p = Pynliner() p.from_string(html) html_with_css_inline = p.run() # Ansi2HTMLConverter returns a complete HTML document, we just want body doc = BeautifulSoup(html_with_css_inline) return doc.body.renderContents()
def _test_external_url(self, url, expected_url): with mock.patch.object(Pynliner, '_get_url') as mocked: def check_url(url): self.assertEqual(url, expected_url) return ".b1,.b2 { font-weight:bold; } .c {color: red}" mocked.side_effect = check_url p = Pynliner() p.root_url = self.root_url p.relative_url = self.relative_url p.from_string(self.html_template.format(href=url)) p._get_soup() p._get_styles()
def process_dict(self, input_dict): #matches = [k for k in self.artifact.input_artifacts_dict.keys() if k.endswith(".css|dexy")] #k = matches[0] css = open("pastie.css", "r").read() output_dict = OrderedDict() for k, v in input_dict.items(): try: p = Pynliner(self.log) except TypeError: print "the pynliner filter says: please install pynliner from source (https://github.com/rennat/pynliner.git) or version > 0.2.1" p = Pynliner() p.from_string(v).with_cssString(css) output_dict[k] = p.run() return output_dict
def process_dict(self, input_dict): # matches = [k for k in self.artifact.input_artifacts_dict.keys() if k.endswith(".css|dexy")] # k = matches[0] css = open("pastie.css", "r").read() output_dict = OrderedDict() for k, v in input_dict.items(): try: p = Pynliner(self.log) except TypeError: print "the pynliner filter says: please upgrade to the latest version of pynliner (e.g. easy_install -U pynliner)" p = Pynliner() p.from_string(v).with_cssString(css) output_dict[k] = p.run() return output_dict
def _render(self, context): """ Renders the plain and html versions of a template. Return both in a tuple, where the first element is the plain text version and the second element is the html version :return: (str, str,) """ if not context: context = Context({}) plain = self.template_plain.render(context) html = self.template_html.render(context) css = get_template(self.template_style).render(Context({})) p = Pynliner() html = p.from_string(html).with_cssString(css).run() return plain, html
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;}") print( tostring( fromstring( inliner.from_string(tostring(root, pretty_print=True, encoding='unicode')).run() ) ).decode('utf-8') )
def compose(self): children = self.soup.body.children tags = [str(_) for _ in children if not isinstance(_, NavigableString)] prettify = "".join(tags) pynliner = Pynliner() return pynliner.from_string(prettify).with_cssString(self.css).run()