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 nice_structured_text(st): """ run structured_text() but recognized text like: "google"n:http://google.com and attached style="text-decoration:none" on those. """ letters = string.letters hrefn = re.compile( "(\"[ %s0-9\n\-\.\,\;\(\)\/\:\/\*\']+\")n(:)([a-zA-Z0-9\@\.\,\?\!\/\:\;\=\&\%%\-\#\~]+)([,]*\s*)" % letters) found_ununderlined = hrefn.findall(st) _href_mem = {} for each in found_ununderlined: randstr = getRandomString(5) _href_mem[randstr] = each[0] for each in found_ununderlined: each = list(each) basiceach = [each[0], 'n'] + list(each[1:-1]) for mem, title in _href_mem.items(): if title == each[0]: each[0] = '"%s"' % mem st = st.replace(''.join(basiceach), ''.join(each)) st = structured_text(st) for each in found_ununderlined: e2 = each[2] if e2[-1] in ['.', ',']: e2 = e2[:-1] e0 = each[0] # title for mem, title in _href_mem.items(): if title == each[0]: e0 = mem orig_href = '<a href="%s">%s</a>' % (e2, e0) _good = _href_mem[e0].replace('"', '') new_href = '<a href="%s" style="text-decoration:none">%s</a>' new_href = new_href % (e2, _good) st = st.replace(orig_href, new_href) return st
def nice_structured_text(st): """ run structured_text() but recognized text like: "google"n:http://google.com and attached style="text-decoration:none" on those. """ letters = string.letters hrefn = re.compile("(\"[ %s0-9\n\-\.\,\;\(\)\/\:\/\*\']+\")n(:)([a-zA-Z0-9\@\.\,\?\!\/\:\;\=\&\%%\-\#\~]+)([,]*\s*)" % letters) found_ununderlined = hrefn.findall(st) _href_mem = {} for each in found_ununderlined: randstr = getRandomString(5) _href_mem[randstr] = each[0] for each in found_ununderlined: each = list(each) basiceach = [each[0],'n'] + list(each[1:-1]) for mem, title in _href_mem.items(): if title == each[0]: each[0] = '"%s"'%mem st = st.replace(''.join(basiceach), ''.join(each)) st = structured_text(st) for each in found_ununderlined: e2 = each[2] if e2[-1] in ['.',',']: e2 = e2[:-1] e0 = each[0] # title for mem, title in _href_mem.items(): if title == each[0]: e0 = mem orig_href = '<a href="%s">%s</a>'%(e2, e0) _good = _href_mem[e0].replace('"','') new_href = '<a href="%s" style="text-decoration:none">%s</a>' new_href = new_href%(e2, _good) st = st.replace(orig_href, new_href) return st
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
from ZTUtils import Batch from Products.CMFCore.utils import getToolByInterfaceName from Products.CMFDefault.utils import decode ctool = getToolByInterfaceName(script, 'Products.CMFCore.interfaces.ICatalogTool') utool = getToolByInterfaceName(script, 'Products.CMFCore.interfaces.IURLTool') portal_url = utool() options = {} target = '%s/recent_news' % portal_url items = ctool.searchResults(portal_type='News Item', sort_on='Date', sort_order='reverse', review_state='published') batch_obj = Batch(items, 10, b_start, orphan=1) items = [{ 'creators': item.listCreators, 'date': item.Date, 'description': structured_text(item.Description), 'title': item.Title, 'url': item.getURL() } for item in batch_obj] navigation = context.getBatchNavigation(batch_obj, target) options['batch'] = {'listItemInfos': items, 'navigation': navigation} return context.recent_news_template(**decode(options, script))
## from Products.PythonScripts.standard import structured_text from ZTUtils import Batch from Products.CMFCore.utils import getToolByInterfaceName from Products.CMFDefault.utils import decode ctool = getToolByInterfaceName('Products.CMFCore.interfaces.ICatalogTool') utool = getToolByInterfaceName('Products.CMFCore.interfaces.IURLTool') portal_url = utool() options = {} target = '%s/recent_news' % portal_url items = ctool.searchResults(portal_type='News Item', sort_on='Date', sort_order='reverse', review_state='published') batch_obj = Batch(items, 10, b_start, orphan=1) items = [ {'creators': item.listCreators, 'date': item.Date, 'description': structured_text(item.Description), 'title': item.Title, 'url': item.getURL()} for item in batch_obj ] navigation = context.getBatchNavigation(batch_obj, target) options['batch'] = {'listItemInfos': items, 'navigation': navigation} return context.recent_news_template(**decode(options, script))
if add and \ context.validateHTML(**form) and \ context.discussion_reply(**form): return elif preview and \ context.validateHTML(**form): is_preview = True options = {} title = form.get('title', context.Title()) text = form.get('text', '') options['is_preview'] = is_preview options['title'] = title options['text'] = text options['cooked_text'] = structured_text(text) if is_preview: hidden_vars = [{ 'name': n, 'value': v } for n, v in html_marshal(title=title, text=text)] else: hidden_vars = [] buttons = [] target = atool.getActionInfo('object/reply', context)['url'] buttons.append({'name': 'add', 'value': _(u'Add')}) if is_preview: buttons.append({'name': 'edit', 'value': _(u'Edit')}) else: buttons.append({'name': 'preview', 'value': _(u'Preview')})
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
context.validateHTML(**form) and \ context.discussion_reply(**form): return elif preview and \ context.validateHTML(**form): is_preview = True options = {} title = form.get('title', context.Title()) text = form.get('text', '') options['is_preview'] = is_preview options['title'] = title options['text'] = text options['cooked_text'] = structured_text(text) if is_preview: hidden_vars = [ {'name': n, 'value': v} for n, v in html_marshal(title=title, text=text) ] else: hidden_vars = [] buttons = [] target = atool.getActionInfo('object/reply', context)['url'] buttons.append( {'name': 'add', 'value': _(u'Add')} ) if is_preview: buttons.append( {'name': 'edit', 'value': _(u'Edit')} ) else: buttons.append( {'name': 'preview', 'value': _(u'Preview')} ) options['form'] = { 'action': target, 'listHiddenVarInfos': tuple(hidden_vars),
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