Commit d8c780be authored by Fabien Morin's avatar Fabien Morin

modify grep method to be xhtml valid and to highlight what is grepped

highlight is enable by default, but it's possible to disable it passing the parameter highlight with value 0 to the url


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@23821 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ac3103af
...@@ -34,22 +34,43 @@ def traverse(ob, r, result, command_line_arguments): ...@@ -34,22 +34,43 @@ def traverse(ob, r, result, command_line_arguments):
result.append((ob.absolute_url(), path, "\n".join(context))) result.append((ob.absolute_url(), path, "\n".join(context)))
break break
def grep(self, pattern, A=0, B=0, r=1, i=0): def grep(self, pattern, A=0, B=0, r=1, i=0, highlight=1):
if not _checkPermission(Permissions.ManagePortal, self): if not _checkPermission(Permissions.ManagePortal, self):
raise Unauthorized(self) raise Unauthorized(self)
command_line_arguments = {} # emulate grep command line args command_line_arguments = {} # emulate grep command line args
command_line_arguments['A'] = int(A) command_line_arguments['A'] = int(A)
command_line_arguments['B'] = int(B) command_line_arguments['B'] = int(B)
command_line_arguments['r'] = int(r) command_line_arguments['r'] = int(r)
highlight = int(highlight)
re_flags = 0 re_flags = 0
if int(i) : if int(i) :
re_flags = re.IGNORECASE re_flags = re.IGNORECASE
result = [] result = []
traverse(self, re.compile(pattern, re_flags), result, command_line_arguments) traverse(self, re.compile(pattern, re_flags), result, command_line_arguments)
html_element_list = ['<html>', '<body>']
doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
html = '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'
head = '''<head>
<title>Grep result</title>
<style type="text/css">
body{
background-color: #F9F9F9;
font-family: Verdana,Tahoma,Georgia,Geneva,Arial,Sans,sans-serif;
}
img{
border:0;
}
.highlight{
background-color: #8DFF6F;
}
</style>
</head>'''
html_element_list = [doctype, html, head, '<body>' '<p>']
for url, path, line in result: for url, path, line in result:
path = cgi.escape(path) path = cgi.escape(path)
line = cgi.escape(line) line = cgi.escape(line)
if highlight:
line = line.replace(pattern, '<span class="highlight">%s</span>' % pattern)
if ExternalEditor is None: if ExternalEditor is None:
html_element_list.append( html_element_list.append(
'<a href="%s/manage_workspace">%s</a>: %s<br/>' % '<a href="%s/manage_workspace">%s</a>: %s<br/>' %
...@@ -61,11 +82,11 @@ def grep(self, pattern, A=0, B=0, r=1, i=0): ...@@ -61,11 +82,11 @@ def grep(self, pattern, A=0, B=0, r=1, i=0):
'/'.join(path_element_list[:-1]), path_element_list[-1]) '/'.join(path_element_list[:-1]), path_element_list[-1])
html_element_list.append( html_element_list.append(
'<a href="%s/manage_workspace">%s</a>&nbsp;<a href="%s">' '<a href="%s/manage_workspace">%s</a>&nbsp;<a href="%s">'
'<img border="0" src="misc_/ExternalEditor/edit_icon" '\ '<img src="misc_/ExternalEditor/edit_icon" '\
'alt="externalEditor Icon"/></a> %s<br/>' 'alt="externalEditor Icon"/></a> %s<br/>'
% (url, path, external_editor_link, line.replace('\n', '<br/>'))) % (url, path, external_editor_link, line.replace('\n', '<br/>')))
html_element_list.extend(['</body>', '</html>']) html_element_list.extend(['</p>', '</body>', '</html>'])
self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html') self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html')
return '\n'.join(html_element_list) return '\n'.join(html_element_list)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment