import re import time from paste import wsgilib from paste.response import header_value, remove_header from wareweb.funcs import * from commentary import store from paste.deploy import CONFIG from commentary import dumbpath, bodysplit from lxml.html import fromstring, tostring def filter_app(app): def replacement_app(environ, start_response): # @@: I have to rewrite content-length path_info = environ.get('PATH_INFO', '') script_name = environ.get('SCRIPT_NAME', '') status, headers, body = wsgilib.intercept_output( environ, app) if int(status.split(None, 1)[0]) != 200: start_response(status, headers) return [body] content_type = header_value(headers, 'content-type') if (not content_type or not content_type.startswith('text/html')): start_response(status, headers) return [body] body = filter_body(environ, script_name, path_info, body) remove_header(headers, 'content-length') # Fix up the encoding: remove_header(headers, 'content-type') headers.append(('content-type', 'text/html; charset=utf8')) headers.append(('content-length', str(len(body)))) start_response(status, headers) return [body] return replacement_app def filter_body(environ, script_name, path_info, body): base = environ['commentary.base_href'] start_re = CONFIG.get('body_start_re') if start_re: start_re = re.compile(start_re) end_re = CONFIG.get('body_end_re') if end_re: end_re = re.compile(end_re) # Clearly I should be reading a header for this or something... if CONFIG.get('input_encoding'): body = body.decode(CONFIG.get('input_encoding')) (head, body_tag, header, body, footer, end_body_tag) = bodysplit.split_body( body, start_re, end_re) vars = {'base': base, 'mochi_base': CONFIG.get('mochikit_uri', base+'/mochilib/MochiKit/'), 'static_base': CONFIG.get('static_uri', base), 'scriptaculous_base': CONFIG.get('scriptaculous_uri', base+'/scriptaculous'), 'script_name': script_name, 'path_info': path_info, 'path_info_html': html_quote(path_info), 'commentary_id': int(time.time()), 'comment_body_html': '', 'page_html': '', 'action_name': 'add_comment', } content = filter_content(environ, base, path_info, body, vars) parts = [head, filter_head % vars, body_tag, filter_page_head % vars, header, filter_content_head % vars, content, filter_content_foot % vars, footer, filter_foot % vars, end_body_tag] return ''.join(map(_encode, parts)) def _encode(s): if isinstance(s, unicode): return s.encode('utf8') return s def filter_content(environ, base, path_info, body, vars): body = '' + body doc = fromstring(body) doc_comment = store.DocumentComments(path_info, CONFIG['storage']) found_nodes = {} for position_id, comments in doc_comment.iter_comments(): if position_id.startswith('__'): #special continue found_nodes[position_id] = dumbpath.find_path( doc, position_id) for position_id, comments in doc_comment.iter_comments(): if position_id.startswith('__'): #special continue node = found_nodes[position_id] node.attrib['class'] = node.attrib.get('class', '')+' commentary-commented-element' html = ['
Filtered for Commentary
''' % comment_form_template.replace('[[ID]]', comment_form_position_id)