def transform_text(request, text, html=True, tag_func=tags2links): if html: text = auto_link(text) text = nl2br(text) text = tag_func(unicode(text)) text = at_userid_link(request, text) return text
def transform(self, text): """ Transform text. Base for any tags is current context. """ text = sanitize(text) text = auto_link(text, link='urls') text = nl2br(text) text = tags2links(unicode(text)) text = at_userid_link(text, self.context, self.request) return text
def render_email(subject='', content_text=None, content_html=None, html_template='/email/base_email_content.mako', **kwargs): # TODO!!! AllanC - This needs to be made thead safe and not use pylons libs # Check paramiters for validity and throw exception if needed if content_text == None and content_html == None: raise EmailContentError( 'email content for plain text or HTML not specifyed') # Convert plain text into html by: # -autolinking any links # -putting inside html header if content_text != None and content_html == None: content_html = auto_link(content_text).replace( '\n', literal('<br/>\n')) # .decode('UTF-8') # Convert html emails into a plain text equivlent elif content_html != None and content_text == None: content_text = convert_html_to_plain_text(content_html) # If not already wrapped in HTML header # If content HTML is not a complete document with a head and body - put it in the standard email template if not re.search(r'<body.*</body>', content_html, re.DOTALL + re.IGNORECASE): from pylons.templating import render_mako content_html = render_mako(html_template, extra_vars={ "kwargs": kwargs, "content_html": content_html }) # Subject - append site name to subject #from pylons.i18n.translation import _ #if not subject: # subject = _("_site_name") #else: # subject = _("_site_name")+': '+subject return dict( subject=subject, content_text=content_text, content_html=content_html, )
def test_auto_link_parsing(self): urls = [ literal("http://www.pylonshq.com"), literal("http://www.pylonshq.com:80"), literal("http://www.pylonshq.com/~minam"), literal("https://www.pylonshq.com/~minam"), literal("http://www.pylonshq.com/~minam/url%20with%20spaces"), literal("http://www.pylonshq.com/foo.cgi?something=here"), literal("http://www.pylonshq.com/foo.cgi?something=here&and=here"), literal("http://www.pylonshq.com/contact;new"), literal("http://www.pylonshq.com/contact;new%20with%20spaces"), literal("http://www.pylonshq.com/contact;new?with=query&string=params"), literal("http://www.pylonshq.com/~minam/contact;new?with=query&string=params"), literal( "http://en.wikipedia.org/wiki/Wikipedia:Today%27s_featured_picture_%28animation%29/January_20%2C_2007" ), literal("http://www.pylonshq.com/foo.cgi?date=01/01/01"), ] for url in urls: self.assertEqual('<a href="%s">%s</a>' % (url, url), auto_link(url))
def _transform_text(text): text = auto_link(text) text = nl2br(text) return tags2links(unicode(text))
def test_auto_linking(self): raw_values = { "email_raw": literal("*****@*****.**"), "link_raw": literal("http://www.pylonshq.com"), "link2_raw": literal("www.pylonshq.com"), "link3_raw": literal("http://manuals.we-love-the-moon.com/read/chapter.need_a-period/103#page281"), "link4_raw": literal("http://foo.example.com/controller/action?parm=value&p2=v2#anchor123"), "link5_raw": literal("http://foo.example.com:3000/controller/action"), "link6_raw": literal("http://foo.example.com:3000/controller/action+pack"), "link7_raw": literal("http://foo.example.com/controller/action?parm=value&p2=v2#anchor-123"), "link8_raw": literal("http://foo.example.com:3000/controller/action.html"), "link9_raw": literal("http://business.timesonline.co.uk/article/0,,9065-2473189,00.html"), } result_values_templates = { "email_result": '<a href="mailto:${email_raw}">${email_raw}</a>', "link_result": '<a href="${link_raw}">${link_raw}</a>', "link_result_with_options": '<a href="${link_raw}" target="_blank">${link_raw}</a>', "link2_result": '<a href="http://${link2_raw}">${link2_raw}</a>', "link3_result": '<a href="${link3_raw}">${link3_raw}</a>', "link4_result": '<a href="${link4_raw}">${link4_raw}</a>', "link5_result": '<a href="${link5_raw}">${link5_raw}</a>', "link6_result": '<a href="${link6_raw}">${link6_raw}</a>', "link7_result": '<a href="${link7_raw}">${link7_raw}</a>', "link8_result": '<a href="${link8_raw}">${link8_raw}</a>', "link9_result": '<a href="${link9_raw}">${link9_raw}</a>', } result_values = {} for k, v in result_values_templates.iteritems(): result_values[k] = Template(v).substitute(raw_values) self.assertEqual(result_values["email_result"], auto_link(raw_values["email_raw"], "email_addresses")) self.assertEqual( u"hello %(email_result)s" % result_values, auto_link("hello %(email_raw)s" % raw_values, "email_addresses") ) self.assertEqual(u"Go to %(link_result)s" % result_values, auto_link("Go to %(link_raw)s" % raw_values, "urls")) self.assertEqual( u"Go to %(link_raw)s" % raw_values, auto_link("Go to %(link_raw)s" % raw_values, "email_addresses") ) self.assertEqual( u"Go to %(link_result)s and say hello to %(email_result)s" % result_values, auto_link("Go to %(link_raw)s and say hello to %(email_raw)s" % raw_values), ) self.assertEqual( u"<p>Link %(link_result)s</p>" % result_values, auto_link(literal("<p>Link %(link_raw)s</p>") % raw_values) ) self.assertEqual( u"<p>%(link_result)s Link</p>" % result_values, auto_link(literal("<p>%(link_raw)s Link</p>") % raw_values) ) self.assertEqual( u"<p>Link %(link_result_with_options)s</p>" % result_values, auto_link(literal("<p>Link %(link_raw)s</p>") % raw_values, "all", target="_blank"), ) self.assertEqual(u"Go to %(link_result)s." % result_values, auto_link("Go to %(link_raw)s." % raw_values)) self.assertEqual( u"<p>Go to %(link_result)s, then say hello to %(email_result)s.</p>" % result_values, auto_link(literal("<p>Go to %(link_raw)s, then say hello to %(email_raw)s.</p>") % raw_values), ) self.assertEqual( u"Go to %(link2_result)s" % result_values, auto_link("Go to %(link2_raw)s" % raw_values, "urls") ) self.assertEqual( u"Go to %(link2_raw)s" % raw_values, auto_link("Go to %(link2_raw)s" % raw_values, "email_addresses") ) self.assertEqual( u"<p>Link %(link2_result)s</p>" % result_values, auto_link(literal("<p>Link %(link2_raw)s</p>") % raw_values), ) self.assertEqual( u"<p>%(link2_result)s Link</p>" % result_values, auto_link(literal("<p>%(link2_raw)s Link</p>") % raw_values), ) self.assertEqual( u"Go to %(link2_result)s." % result_values, auto_link(literal("Go to %(link2_raw)s.") % raw_values) ) self.assertEqual( u"<p>Say hello to %(email_result)s, then go to %(link2_result)s.</p>" % result_values, auto_link(literal("<p>Say hello to %(email_raw)s, then go to %(link2_raw)s.</p>") % raw_values), ) self.assertEqual( u"Go to %(link3_result)s" % result_values, auto_link("Go to %(link3_raw)s" % raw_values, "urls") ) self.assertEqual( u"Go to %(link3_raw)s" % raw_values, auto_link("Go to %(link3_raw)s" % raw_values, "email_addresses") ) self.assertEqual( u"<p>Link %(link3_result)s</p>" % result_values, auto_link(literal("<p>Link %(link3_raw)s</p>") % raw_values), ) self.assertEqual( u"<p>%(link3_result)s Link</p>" % result_values, auto_link(literal("<p>%(link3_raw)s Link</p>") % raw_values), ) self.assertEqual(u"Go to %(link3_result)s." % result_values, auto_link("Go to %(link3_raw)s." % raw_values)) self.assertEqual( u"<p>Go to %(link3_result)s. seriously, %(link3_result)s? i think I'll say hello to %(email_result)s. instead.</p>" % result_values, auto_link( literal( "<p>Go to %(link3_raw)s. seriously, %(link3_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>" ) % raw_values ), ) self.assertEqual( u"<p>Link %(link4_result)s</p>" % result_values, auto_link(literal("<p>Link %(link4_raw)s</p>") % raw_values), ) self.assertEqual( u"<p>%(link4_result)s Link</p>" % result_values, auto_link(literal("<p>%(link4_raw)s Link</p>") % raw_values), ) self.assertEqual( u"<p>%(link5_result)s Link</p>" % result_values, auto_link(literal("<p>%(link5_raw)s Link</p>") % raw_values), ) self.assertEqual( u"<p>%(link6_result)s Link</p>" % result_values, auto_link(literal("<p>%(link6_raw)s Link</p>") % raw_values), ) self.assertEqual( u"<p>%(link7_result)s Link</p>" % result_values, auto_link(literal("<p>%(link7_raw)s Link</p>") % raw_values), ) self.assertEqual( u"Go to %(link8_result)s" % result_values, auto_link("Go to %(link8_raw)s" % raw_values, "urls") ) self.assertEqual( u"Go to %(link8_raw)s" % raw_values, auto_link("Go to %(link8_raw)s" % raw_values, "email_addresses") ) self.assertEqual( u"<p>Link %(link8_result)s</p>" % result_values, auto_link(literal("<p>Link %(link8_raw)s</p>") % raw_values), ) self.assertEqual( u"<p>%(link8_result)s Link</p>" % result_values, auto_link(literal("<p>%(link8_raw)s Link</p>") % raw_values), ) self.assertEqual(u"Go to %(link8_result)s." % result_values, auto_link("Go to %(link8_raw)s." % raw_values)) self.assertEqual( u"<p>Go to %(link8_result)s. seriously, %(link8_result)s? i think I'll say hello to %(email_result)s. instead.</p>" % result_values, auto_link( literal( "<p>Go to %(link8_raw)s. seriously, %(link8_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>" ) % raw_values ), ) self.assertEqual( u"Go to %(link9_result)s" % result_values, auto_link("Go to %(link9_raw)s" % raw_values, "urls") ) self.assertEqual( u"Go to %(link9_raw)s" % raw_values, auto_link("Go to %(link9_raw)s" % raw_values, "email_addresses") ) self.assertEqual( u"<p>Link %(link9_result)s</p>" % result_values, auto_link(literal("<p>Link %(link9_raw)s</p>") % raw_values), ) self.assertEqual( u"<p>%(link9_result)s Link</p>" % result_values, auto_link(literal("<p>%(link9_raw)s Link</p>") % raw_values), ) self.assertEqual(u"Go to %(link9_result)s." % result_values, auto_link("Go to %(link9_raw)s." % raw_values)) self.assertEqual( u"<p>Go to %(link9_result)s. seriously, %(link9_result)s? i think I'll say hello to %(email_result)s. instead.</p>" % result_values, auto_link( literal( "<p>Go to %(link9_raw)s. seriously, %(link9_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>" ) % raw_values ), ) self.assertEqual(u"", auto_link(None)) self.assertEqual(u"", auto_link(""))
def test_auto_link_with_brackets(self): self.assertEqual( '[<a href="http://www.example.com">http://www.example.com</a>]', auto_link("[http://www.example.com]") )
def format_text(self, text): text = sanitize(text) text = auto_link(text) return nl2br(text)