[Zope-dev] StructuredText and ZPT nocall() for URL/IMG - suggestion

Brad Clements bkc@murkworks.com
Wed, 14 Nov 2001 10:01:35 -0500


Just thought I'd mention what I've done with my new website.

I'm trying, as much as possible, to make all content be based on Structured Text. I've 
created a ZClass that combines PageTemplates and stx_to_html.py together.

one thing that really annoys me is that the "heres my url":xyz  and "my picture":img:wow 
tag processing in Structured Text assumes that the "urls" are http type paths or relative.

I'd prefer that images output tag() values and that urls, when "not local", output 
absolute_url() values.

What I did was pass nocall() to stx_to_html from within my PageTemplate and modified 
the HTMLWithImages class, as follows in the code below.

It tries to use nocall() to lookup the href part of the tag. If an object is found, we use 
tag() or absolute_url() respectively. Otherwise it outputs the href tag as-is.

Currently this is a bit of a hack, but I'm curious if anyone else would like this 
functionality (in which case it's back to the Wiki).  

(code fragment from stx_to_html.py)


class HWIClass( HTMLWithImages ):
    def document(self, doc, level, output):
    # overrides HTMLWithImages.document (Zope 2.4.1b1) to leave out <html> and <body> tag when rendering
        for c in doc.getChildNodes():
          getattr(self, self.element_types[c.getNodeName()])(c, level, output)

    def image(self, doc, level, output):
        if hasattr(doc, 'key'):
            output('<a name="%s"></a>\n' % doc.key)
        if hasattr(self,'nocall'):
            try:
                obj = self.nocall("here/"+doc.href)
                alt = str(doc.getNodeValue())
                alt = alt.replace('"','')
                output(obj.tag(alt=alt))
                hr = None
            except AttributeError:
                hr = doc.href
        else:
            hr = doc.href
        if hr:
            output('<img src="%s" alt="%s">\n' % (hr, doc.getNodeValue()))
        if doc.getNodeValue() and hasattr(doc, 'key'):
            output('<p><b>Figure %s</b> %s</p>\n' % (doc.key, doc.getNodeValue()))

    def image(self, doc, level, output):
        if hasattr(doc, 'key'):
            output('<a name="%s"></a>\n' % doc.key)
        if hasattr(self,'nocall'):
            try:
                obj = self.nocall("here/"+doc.href)
                alt = str(doc.getNodeValue())
                alt = alt.replace('"','')
                output(obj.tag(alt=alt))
                return
            except AttributeError:
                hr = doc.href
        else:
            hr = doc.href 
        output('<img src="%s" alt="%s">\n' % (hr, doc.getNodeValue()))
        if doc.getNodeValue() and hasattr(doc, 'key'):
            output('<p><b>Figure %s</b> %s</p>\n' % (doc.key, doc.getNodeValue()))

    def link(self, doc, level, output):
        """override to use nocall"""
        if hasattr(self,'nocall'):
            try:
                obj = self.nocall("here/"+doc.href)
                hr = obj.absolute_url()
            except AttributeError:
                hr = doc.href
        else:
            hr = doc.href 

        output('<a href="%s">' % hr)
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()])(c, level, output)
        output('</a>')

def stx_to_html(src=None,text="", nocall=None,level=1 ):
  if src and src.meta_type == 'Rendered STX' and not text:
    text = src.document_src()
  st = StructuredText.Basic( text )
  if not st:
    return ""

  doc = DWIClass( st )
  HWICls = HWIClass()
  HWICls.nocall = nocall
  html = HWICls( doc, level )
  return html




Brad Clements,                bkc@murkworks.com   (315)268-1000
http://www.murkworks.com                          (315)268-9812 Fax
netmeeting: ils://ils.murkworks.com               AOL-IM: BKClements