You are not logged in Log in Join
You are here: Home » Members » andym » view_source » View File

Log in
Name

Password

 

view_source

File details
Size
2 K
File type
text/plain

File contents

##############################################################################
# Copyright Sean Treadway 2001 (c) [email protected]
# Licensed under the Zope public license
# http://www.zope.org/Resources/License
# 

import re
import string
import Acquisition

re_format = re.compile('<dtml-(?:var|with) ([^"][a-z0-9_.-~{}|+]*?[^"])>|<|>| |\t', re.I)

class Formatter(Acquisition.Implicit):

    tab_stop = '4'
    
    def __call__(self, match):
        m = match.group(0)
        if m == '<': return '&lt;'
        elif m == '>': return '&gt;'
        elif m == ' ': return '&nbsp;'
        elif m == '\t': return '&nbsp;' * self.tab_stop
        else:
            id = match.group(1)
            if hasattr(self, id):
                ob = getattr(self, id)
                url = '..'
                if hasattr(ob, 'absolute_url'):
                    url = ob.absolute_url()
                return '<a href="%s/view_source">&lt;%s&gt;</a>' % (url, m[1:-1])
            return '&lt;%s&gt;' % m[1:-1]

def view_source(self, REQUEST=None):
    "Render the document_src with links to included variables"

    # TODO make sure we don't get crawled
    
    res = []
    a = res.append
    a(self.standard_html_header(self, REQUEST=REQUEST))
    path = self.absolute_url()
    a('<h2><a href="%s">%s</a></h2>' % (path, path))
    a('<a href="javascript:history.go(-1)">Back...</a>')
    a('<hr>')

    a('<pre>')
    try:
        if self.meta_type == 'Folder':
            # Display a link to our contents instead
            for id in self.objectIds(['DTML Method', 'DTML Document', 'Folder']):
                ob = self._getOb(id)
                a('<a href="%s/view_source"><img src="%s" border="0">%s</a>' % (
                    id, ob.icon, id))
        else:
            # Create a linker and have it aquire us
            formatter = Formatter()
            formatter = formatter.__of__(self.aq_parent)

            # Highlight the links
            src = self.document_src(REQUEST, REQUEST.RESPONSE)
            src = re_format.sub(formatter, src)
            a(src)
    except IOError:
        if hasattr(self, 'index.html') and REQUEST:
            REQUEST.RESPONSE.redirect('index.html/view_source')
        else:
            a('Cannot render the source for this ' + self.meta_type)

    a('</pre>')
    a(self.standard_html_footer(self, REQUEST))

    if REQUEST:
        REQUEST.RESPONSE.setHeader('Content-Type', 'text/html')

    return string.join(res, '\n')