本文整理汇总了Python中pynliner.Pynliner类的典型用法代码示例。如果您正苦于以下问题:Python Pynliner类的具体用法?Python Pynliner怎么用?Python Pynliner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Pynliner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_dict
def process_dict(self, input_dict):
converter = Ansi2HTMLConverter()
full = self.arg_value('full', False)
ext = self.artifact.input_ext
if input_dict.has_key('1') and not input_dict['1'] and ext == ".css":
# Special case if we get a virtual empty file, generate style file
self.artifact.final = True
self.artifact.ext = ext
output_dict = OrderedDict()
output_dict['1'] = self.generate_css()
else:
p = None
css = None
inline_css = self.arg_value('inline', False)
if inline_css:
css = "\n".join(converter.produce_headers().strip().splitlines()[1:-1])
self.log.debug(css)
try:
from pynliner import Pynliner
except ImportError:
raise UserFeedback("You must install BeautifulSoup, cssutils and pynliner in order to use 'inline' option:\n pip install BeautifulSoup cssutils pynliner\n")
output_dict = OrderedDict()
for section_name, section_text in input_dict.iteritems():
html = converter.convert(section_text, full=full)
if inline_css:
p = Pynliner(self.log).from_string(html).with_cssString(css)
html = "<pre>\n%s</pre>" % p.run()
output_dict[section_name] = html
return output_dict
示例2: CaseSensitive
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)
示例3: ansi_output_to_html
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()
示例4: LogOptions
class LogOptions(unittest.TestCase):
def setUp(self):
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
def test_no_log(self):
self.p = Pynliner()
self.assertEqual(self.p.log, None)
self.assertEqual(cssutils.log.enabled, False)
def test_custom_log(self):
self.log = logging.getLogger('testlog')
self.log.setLevel(logging.DEBUG)
self.logstream = StringIO.StringIO()
handler = logging.StreamHandler(self.logstream)
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.p = Pynliner(self.log).from_string(self.html)
self.p.run()
log_contents = self.logstream.getvalue()
self.assertTrue("DEBUG" in log_contents)
示例5: process_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 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
示例6: _test_external_url
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()
示例7: test_06_with_cssString
def test_06_with_cssString(self):
"""Test 'with_cssString' method"""
cssString = 'h1 {font-size: 2em;}'
self.p = Pynliner().from_string(self.html).with_cssString(cssString)
self.assertEqual(self.p.style_string, cssString + '\n')
output = self.p.run()
self.assertEqual(output, u'<h1 style="font-size: 2em; color: #fc0">Hello World!</h1>')
示例8: WithCustomLog
class WithCustomLog(unittest.TestCase):
def setUp(self):
self.log = logging.getLogger('testlog')
self.log.setLevel(logging.DEBUG)
self.logstream = StringIO.StringIO()
handler = logging.StreamHandler(self.logstream)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
self.p = Pynliner(self.log).from_string(self.html)
def test_custom_log(self):
self.p.run()
log_contents = self.logstream.getvalue()
assert "DEBUG" in log_contents
示例9: _render
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
示例10: setUp
def setUp(self):
self.log = logging.getLogger('testlog')
self.log.setLevel(logging.DEBUG)
self.logstream = StringIO.StringIO()
handler = logging.StreamHandler(self.logstream)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
self.p = Pynliner(self.log).from_string(self.html)
示例11: test_custom_log
def test_custom_log(self):
self.log = logging.getLogger('testlog')
self.log.setLevel(logging.DEBUG)
self.logstream = StringIO.StringIO()
handler = logging.StreamHandler(self.logstream)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.p = Pynliner(self.log).from_string(self.html)
self.p.run()
log_contents = self.logstream.getvalue()
self.assertIn("DEBUG", log_contents)
示例12: open
# Getting email.css
print "\nGetting CSS..."
cssFile = open(os.path.join(templateDir, "css/email.css"), "r")
css = cssFile.read();
cssFile.close()
for name in files:
print " Found: " + name
output = name.replace(".template.html", ".html")
if doInlining:
inputFile = open(name, 'r')
origEmail = inputFile.read()
inputFile.close()
inliner = Pynliner().from_string(origEmail).with_cssString(css)
inlinedEmail = inliner.run()
prettyEmail = BeautifulSoup(inlinedEmail).prettify()
outputFile = open(output, "w")
outputFile.write(prettyEmail)
outputFile.close()
print " Gen: " + output
print ""
return
if __name__ == '__main__':
sys.exit(main())
示例13: chr
# choose a random problem
max_set, max_prob = 427, 4
while True:
try:
prob_code = (randint(1, max_set), chr(randint(0, max_prob) + 65))
if prob_code in used_probs:
raise Exception('This code is terrible.')
if not get_problem_html(prob_code):
raise Exception('This code is terrible.')
ufile.write(str(prob_code) + '\n')
break
except:
pass
out.append('<hr>')
out.append('</body>')
out.append('</html>')
preinline_html = '\n'.join(out)
final_p = Pynliner().from_string(preinline_html).with_cssString(css_string)
final_html = final_p.run()
print final_html
示例14: setUp
def setUp(self):
self.pyn = Pynliner(case_sensitive=False)
示例15: test_08_fromURL
def test_08_fromURL(self):
"""Test 'fromURL' constructor"""
url = 'https://raw.github.com/voidfiles/pynliner/master/test_data/test.html'
p = Pynliner()
p.from_url(url)
self.assertEqual(p.root_url, 'https://raw.github.com')
self.assertEqual(p.relative_url, 'https://raw.github.com/voidfiles/pynliner/master/test_data/')
p._get_soup()
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: #ffcc00;}")
p._get_styles()
output = p.run()
desired = u"""<html><head></head><body>\n <h1 style="color: #fc0">Testing Title</h1>\n <p style="color: #999">Awesome</p>\n </body></html>"""
self.assertEqual(output, desired)