[Zope] Structured Text with Images in regular Zope?

Tres Seaver tseaver@palladion.com
Wed, 30 May 2001 07:54:00 -0400


Michel Pelletier <michel@digicool.com> wrote:
 
> On Tue, 29 May 2001, Mike Renfro wrote:
> 
> 
>> On Tue, May 29, 2001 at 11:10:36AM -0400, marc lindahl wrote:
>> 
> 
>> > I concur -- it would be nice to add an easy image tag in STX.  Just
>> > as double quotes is supposed to create a link, some other syntax to
>> > generate an OBJECT tag (or, intelligently, IMG if an image... for,
>> > ugh, NS4 compatibility), for inclusion of images, sound, video, etc.
> 
>> 
>> The syntax is already there in the code for the Zope book -- I forgot
>> to mention that earlier. My problem is in making the book's
>> StructuredText code work with regular Zope. Making it work with the
>> STX_Document ZClass would be even better.
> 
> 
> The book code is in Zope too, it's just not exposed in any way.  You would
> have to do your processing in an external method.
> 
> Zope does a really poor job of exposing stx to users.  The only interface
> is <dtml-var fmt=structured-text> and that sucks for a few reasons:
> 
>   You can't use other input parser, like for images.
> 
>   You can't use other outputs, like docbook
> 
>   The stx is turned into a dom and transformed every time, instead of
>   being cached, making it slow.
> 
> I had written a pure python product called STXDocument that exposed all of
> this functionality through an API and addable object.  Someone at DC
> however, probably mistakenly, deleted it from the CVS repository (or moved
> it to where I can't find it) without informing me, so it's gone.
> 
> I suggest using an External Method to hack you way to the
> StructuredText.HTMLWithImages.HTMLWithImages class.  The syntax is similar
> to a link in stx:
> 
> "Image alt tag contents":img:/path/to/image


Here is how we added "embedded images" in the CMF (just recently):

  - Created a utility function, '_format_stx', with two supporting classes,
    'CMFDocumentWithImages' and 'CMFHtmlWithImages'::

     #
     #   StructuredText handling.
     #
     import StructuredText
     from StructuredText.HTMLWithImages import HTMLWithImages

     _STXDWI = StructuredText.DocumentWithImages.__class__

     class CMFDocumentClass( StructuredText.DocumentWithImages.__class__ ):
         """
             Override DWI to get '_' into links.
         """
         _URL_AND_PUNC = r'([a-zA-Z0-9_\@\.\,\?\!\/\:\;\-\#\~]+)'
         def doc_href( self
                     , s
                     , expr1 = re.compile( _STXDWI._DQUOTEDTEXT
                                         + "(:)"
                                         + _URL_AND_PUNC
                                         + _STXDWI._SPACES
                                         ).search
                     , expr2 = re.compile( _STXDWI._DQUOTEDTEXT
                                         + r'(\,\s+)'
                                         + _URL_AND_PUNC
                                         + _STXDWI._SPACES
                                         ).search
                     ):
             return _STXDWI.doc_href( self, s, expr1, expr2 )

     CMFDocumentClass = CMFDocumentClass()

     class CMFHtmlWithImages( HTMLWithImages ):
         """
            Special subclass of HTMLWithImages, overriding document().
         """
         def document(self, doc, level, output):
             """\
             HTMLWithImages.document renders full HTML (head, title, 
body).  For
             CMF Purposes, we don't want that.  We just want those nice 
juicy
             body parts perfectly rendered.
             """
             for c in doc.getChildNodes():
             getattr(self, self.element_types[c.getNodeName()])(c, 
level, output)

     CMFHtmlWithImages = CMFHtmlWithImages()

     def _format_stx( text, level=1 ):
         """
             Render STX to HTML.
         """
         st = StructuredText.Basic( text )   # Creates the basic DOM
         if not st:                          # If it's an empty object
             return ""                       # return now or have errors!

         doc = CMFDocumentClass( st )
         html = CMFHtmlWithImages( doc, level )
         return html

  - Changed our Docuement class (as well as other STX clients) to call
    '_format_stx' whenever the content changes, capturing the result as
    the 'cooked' version.

  - We render the Document without using the 'fmt="strucutred-text"'
    bit,   e.g.:

     <dtml-var standard_html_header>
     <dtml-var getCookedHTML>
     <dtml-var standard_html_footer>

The code for '_format_stx' whould package OK as an ExternalMethod,
I think.  Hope this helps,

Tres.
--
========================================================
Tres Seaver                         tseaver@digicool.com
Digital Creations  "Zope Dealers"   http://www.zope.org