def test__addhrefs(): br = '-' * 78 + '\n' print br t = "this some text http://www.peterbe.com/ with links www.peterbe.com in it" # print addhrefs(t) # print br t='this <a href="http://www.google.com">some</a> text http://www.peterbe.com/ '\ 'with links www.peterbe.com in it '\ '<a href="http://www.example.com">Example</a>' print addhrefs(t) print br # t='this <a href="http://www.google.com">some</a> text '\ # "http://www.peterbe.com/ with links www.peterbe.com in it "\ # '<a href="http://www.example.com">Example</a>' # print addhrefs(t) # print br t = "this some text http://www.peterbe.com/ with links www.peterbe.com in it" # print addhrefs(t) # print br t='Starts <a href="bajs.com">bajs.com</a> '\ "this some text http://www.peterbe.com/ with links www.peterbe.com in it "\ '<a href="http://www.example.com">Example</a>'
def test__addhrefs(): br='-'*78+'\n' print br t="this some text http://www.peterbe.com/ with links www.peterbe.com in it" # print addhrefs(t) # print br t='this <a href="http://www.google.com">some</a> text http://www.peterbe.com/ '\ 'with links www.peterbe.com in it '\ '<a href="http://www.example.com">Example</a>' print addhrefs(t) print br # t='this <a href="http://www.google.com">some</a> text '\ # "http://www.peterbe.com/ with links www.peterbe.com in it "\ # '<a href="http://www.example.com">Example</a>' # print addhrefs(t) # print br t="this some text http://www.peterbe.com/ with links www.peterbe.com in it" # print addhrefs(t) # print br t='Starts <a href="bajs.com">bajs.com</a> '\ "this some text http://www.peterbe.com/ with links www.peterbe.com in it "\ '<a href="http://www.example.com">Example</a>'
def ShowText(text, display_format='', emaillinkfunction=None, urllinkfunction=None, allowhtml=False, # only applies to plaintext and structured_text paragraphtag=True, # only applies with plaintext ): """ Display text, using harmless HTML """ if not text: # blank or None return "" if display_format == 'structuredtext': #st=_replace_special_chars(text) st=text if not allowhtml: for k,v in {'<':'<', '>':'>'}.items(): st = st.replace(k, v) st = st.replace('[','|[|') st = html_entity_fixer(st, skipchars=('"',)) st = structured_text(st) if allowhtml: for k,v in {'<':'<', '>':'>'}.items(): st = st.replace(v, k) for k,v in {'&lt;':'<', '&gt;':'>', '|[|':'['}.items(): st = st.replace(k,v) st = addhrefs(st, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) st = st.rstrip() return st elif display_format == 'html': return text else: if paragraphtag: t = '<p>%s</p>'%safe_html_quote(text) else: t = safe_html_quote(text) t = t.replace('&lt;','<').replace('&gt;','>') t = addhrefs(t) t = newline_to_br(t) return t
def ShowDescription(text, display_format=''): """ Display text, using harmless HTML """ if display_format == 'structuredtext': #st=_replace_special_chars(text) st=text for k,v in {'<':'<', '>':'>', '[':'|[|'}.items(): st = st.replace(k,v) st = html_entity_fixer(st, skipchars=('"',)) st = structured_text(st) for k,v in {'&lt;':'<', '&gt;':'>', '|[|':'['}.items(): st = st.replace(k,v) # BUG in structured_text in Zope 2.4.0 # it appends these annoying tags. #for tag in ['<html>','<body>','</body>','</html>']: # st = st.replace(tag, '') st = addhrefs(st) return st elif display_format == 'html': return text else: t = '<p>%s</p>'%html_quote(text) t = t.replace('&lt;','<').replace('&gt;','>') t = addhrefs(t) t = newline_to_br(t) return t
def addhrefstesting(): x = '''http://www.frosp.com/pin555.gif Boo Yah....555 ft''' print addhrefs(x)
def ShowDescription(text, display_format='', nofollow_rel=False): """ Display text, using harmless HTML """ text = SplitRegEx.sub('<!--split-->', text) codesyntax = '' if same_type(display_format, ()) or same_type(display_format, []): display_format, codesyntax = display_format if display_format == 'structuredtext': #st=_replace_special_chars(text) st = text for k, v in { #'<':'<', '>':'>', '[': '|[|' }.items(): st = st.replace(k, v) try: # my structured text st = nice_structured_text(st) except: st = structured_text(st) for k, v in { '&lt;': '<', '&gt;': '>', '|[|': '[' }.items(): st = st.replace(k, v) # BUG in structured_text in Zope 2.4.0 # it appends these annoying tags. for tag in ['<html>', '<body>', '</body>', '</html>']: st = st.replace(tag, '') pre_whole_tags = re.compile(r'<pre>.*?</pre>', re.I | re.DOTALL) pre_tags = pre_whole_tags.findall(st) mem = {} for pre_tag in pre_tags: randstr = '__%s__' % getRandomString() mem[randstr] = pre_tag st = st.replace(pre_tag, randstr) ### NEEDS TO BE FIXED! #st = addhrefs(st, urllinkfunction=mylinker) for key, tag in mem.items(): st = st.replace(key, tag) # preserve look of '<!--split-->' st = st.replace('<p><!--split--></p>', '<!--split-->') # syntax highlighting of code if str(codesyntax).lower() in ['c++', 'cpp']: st = SyntaxHighlight(st, CPP_SYNTAX) elif str(codesyntax).lower() in ['py', 'python']: st = SyntaxHighlight(st, PYTHON_SYNTAX) elif str(codesyntax).lower() in ['sql']: st = SyntaxHighlight(st, SQL_SYNTAX) elif str(codesyntax).lower() in ['xml/html', 'xml', 'html']: st = SyntaxHighlight(st, XML_SYNTAX) elif str(codesyntax).lower() in ['css', 'stylesheet']: st = SyntaxHighlight(st, CSS_SYNTAX) st = sole_ampersand_regex.sub('&', st) return st elif display_format == 'html': return text elif display_format == 'texmix': texes = getTexes(text, 1) count = 1 for tagused, texstring in texes: imageid = 'texjpeg-%s.jpg' % count imagepath = 'texcache/%s' % imageid imagetag = '<img src="%s" alt="%s" ' % (imagepath, "*generated jpeg*") if tagused.lower().find('inline') > -1: imagetag += 'class="texmix-inline"' else: imagetag += 'class="texmix"' imagetag += ' />' text = text.replace(texstring, imagetag) count += 1 text = text.replace('<texmix>', '<span class="texmix">') text = text.replace('<texmix inline="1">', '<span class="texmix-inline">') text = text.replace('</texmix>', '</span>') format = 'structuredtext' if codesyntax: format = [format, codesyntax] return ShowDescription(text, format) else: t = html_quote(text) t = t.replace('&lt;', '<').replace('&gt;', '>') t = t.replace('<!--split-->', '<!--split-->') if nofollow_rel: def nofollower(url): template = '<a href="%s" rel="nofollow">%s</a>' return template % (url, url) t = addhrefs(t, urllinkfunction=nofollower) else: t = addhrefs(t) t = newline_to_br(t) return t
def ShowDescription(text, display_format='', emaillinkfunction=None, urllinkfunction=None): """ Display text, using harmless HTML """ if not text: # blank or None return "" if urllinkfunction is None: # add one that is able to truncate really long URLs def urllinkfunction(url, maxlength=70): if len(url) > maxlength: title = url[:42] + '...' + url[-25:] tooltip = 'Right click to copy the whole URL' return '<a href="%s" title="%s">%s</a>' % \ (improveURL(url), tooltip, title) else: return '<a href="%s">%s</a>' % (improveURL(url), url) if display_format == 'structuredtext': #st=_replace_special_chars(text) st = text # if the text is just a number (and a full stop), then # structured_text is going to make this the first of a numbered # HTML list. Prevent that with this "hack". found_only_number = re.compile('\d[\d \.]+').findall(st) if found_only_number: if found_only_number[0] == st: return st for k, v in {'<': '<', '>': '>', '[': '|[|'}.items(): st = st.replace(k, v) if isinstance(st, str): try: st = html_entity_fixer(st, skipchars=('"', )) except UnicodeDecodeError: # badly stored string. Legacy problem. pass st = structured_text(st) for k, v in { '&lt;': '<', '&gt;': '>', '|[|': '[' }.items(): st = st.replace(k, v) st = addhrefs(st, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) return st elif display_format == 'markdown': if not markdown_converter: import warnings warnings.warn("Markdown is not installed") return text text = markdown_converter.convert(text) text = addhrefs(text, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) return text elif display_format == 'html': return text else: t = '<p>%s</p>' % safe_html_quote(text) t = t.replace('&lt;', '<').replace('&gt;', '>') t = addhrefs(t, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) t = newline_to_br(t) return t
def addhrefstesting(): x='''http://www.frosp.com/pin555.gif Boo Yah....555 ft''' print addhrefs(x)
def ShowDescription(text, display_format='', nofollow_rel=False): """ Display text, using harmless HTML """ text = SplitRegEx.sub('<!--split-->', text) codesyntax = '' if same_type(display_format, ()) or same_type(display_format, []): display_format, codesyntax = display_format if display_format == 'structuredtext': #st=_replace_special_chars(text) st=text for k,v in {#'<':'<', '>':'>', '[':'|[|'}.items(): st = st.replace(k,v) try: # my structured text st = nice_structured_text(st) except: st = structured_text(st) for k,v in {'&lt;':'<', '&gt;':'>', '|[|':'['}.items(): st = st.replace(k,v) # BUG in structured_text in Zope 2.4.0 # it appends these annoying tags. for tag in ['<html>','<body>','</body>','</html>']: st = st.replace(tag, '') pre_whole_tags = re.compile(r'<pre>.*?</pre>', re.I|re.DOTALL) pre_tags = pre_whole_tags.findall(st) mem = {} for pre_tag in pre_tags: randstr = '__%s__'%getRandomString() mem[randstr] = pre_tag st = st.replace(pre_tag, randstr) ### NEEDS TO BE FIXED! #st = addhrefs(st, urllinkfunction=mylinker) for key, tag in mem.items(): st = st.replace(key, tag) # preserve look of '<!--split-->' st = st.replace('<p><!--split--></p>','<!--split-->') # syntax highlighting of code if str(codesyntax).lower() in ['c++','cpp']: st = SyntaxHighlight(st, CPP_SYNTAX) elif str(codesyntax).lower() in ['py','python']: st = SyntaxHighlight(st, PYTHON_SYNTAX) elif str(codesyntax).lower() in ['sql']: st = SyntaxHighlight(st, SQL_SYNTAX) elif str(codesyntax).lower() in ['xml/html','xml','html']: st = SyntaxHighlight(st, XML_SYNTAX) elif str(codesyntax).lower() in ['css','stylesheet']: st = SyntaxHighlight(st, CSS_SYNTAX) st = sole_ampersand_regex.sub('&', st) return st elif display_format == 'html': return text elif display_format == 'texmix': texes = getTexes(text, 1) count = 1 for tagused, texstring in texes: imageid = 'texjpeg-%s.jpg'%count imagepath = 'texcache/%s'%imageid imagetag = '<img src="%s" alt="%s" '%(imagepath, "*generated jpeg*") if tagused.lower().find('inline') > -1: imagetag += 'class="texmix-inline"' else: imagetag += 'class="texmix"' imagetag += ' />' text = text.replace(texstring, imagetag) count += 1 text = text.replace('<texmix>','<span class="texmix">') text = text.replace('<texmix inline="1">','<span class="texmix-inline">') text = text.replace('</texmix>','</span>') format = 'structuredtext' if codesyntax: format = [format, codesyntax] return ShowDescription(text, format) else: t = html_quote(text) t = t.replace('&lt;','<').replace('&gt;','>') t = t.replace('<!--split-->', '<!--split-->') if nofollow_rel: def nofollower(url): template = '<a href="%s" rel="nofollow">%s</a>' return template % (url, url) t = addhrefs(t, urllinkfunction=nofollower) else: t = addhrefs(t) t = newline_to_br(t) return t
def ShowDescription(text, display_format='', emaillinkfunction=None, urllinkfunction=None): """ Display text, using harmless HTML """ if not text: # blank or None return "" if urllinkfunction is None: # add one that is able to truncate really long URLs def urllinkfunction(url, maxlength=70): if len(url) > maxlength: title = url[:42] + '...' + url[-25:] tooltip = 'Right click to copy the whole URL' return '<a href="%s" title="%s">%s</a>' % \ (improveURL(url), tooltip, title) else: return '<a href="%s">%s</a>' % (improveURL(url), url) if display_format == 'structuredtext': #st=_replace_special_chars(text) st=text # if the text is just a number (and a full stop), then # structured_text is going to make this the first of a numbered # HTML list. Prevent that with this "hack". found_only_number = re.compile('\d[\d \.]+').findall(st) if found_only_number: if found_only_number[0] == st: return st for k,v in {'<':'<', '>':'>', '[':'|[|'}.items(): st = st.replace(k,v) if isinstance(st, str): try: st = html_entity_fixer(st, skipchars=('"',)) except UnicodeDecodeError: # badly stored string. Legacy problem. pass st = structured_text(st) for k,v in {'&lt;':'<', '&gt;':'>', '|[|':'['}.items(): st = st.replace(k,v) st = addhrefs(st, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) return st elif display_format == 'markdown': if not markdown_converter: import warnings warnings.warn("Markdown is not installed") return text text = markdown_converter.convert(text) text = addhrefs(text, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) return text elif display_format == 'html': return text else: t = '<p>%s</p>'%safe_html_quote(text) t = t.replace('&lt;','<').replace('&gt;','>') t = addhrefs(t, emaillinkfunction=emaillinkfunction, urllinkfunction=urllinkfunction) t = newline_to_br(t) return t
def wall_fmt(text): text = '\n<br/>\n'.join(cgi.escape(text).split("\n")) text = addhrefs(text) return literal(text)