[Zope] Re: STX and images

Tres Seaver tseaver@palladion.com
Wed, 22 Aug 2001 08:19:49 -0400


Danny William Adair wrote:

> Hi!
> 
> I want to be able to insert images in an stx document using the
 > 
> "alttext":img:/path/to/image
> 
> input style of StructuredText.DocumentWithImages and then when needed 
> render them to HTML using StructuredText.HTMLWithImages. For this 
> purpose I tried to adopt code of the CMF that Tres Seaver posted to 
> zope@zope.org (almost three months ago), to make it work as an external 
> method that converts stx input (with the a/m image insertions) to html. 
> I have the following code in my "Extensions" folder and the function 
> stx_to_html (now available as an External Method) works _almost_ like a 
> charm:
> 
> --------------------------------------------------------------------------------------------------- 
> 
> import StructuredText, re
> from StructuredText.HTMLWithImages import HTMLWithImages
> 
> _STXDWI = StructuredText.DocumentWithImages.__class__
> 
> class DWIClass( StructuredText.DocumentWithImages.__class__ ):
>   _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 )
> 
> DWIClass = DWIClass()
> 
> class HWIClass( HTMLWithImages ):
>   def document(self, doc, level, output):
>     for c in doc.getChildNodes():
>       getattr(self, self.element_types[c.getNodeName()])(c, level, output)
> 
> HWIClass = HWIClass()
> 
> def stx_to_html( text, level=1 ):
>   st = StructuredText.Basic( text )
>   if not st:
>     return ""
> 
>   doc = DWIClass( st )
>   html = HWIClass( doc, level )
>   return html
> --------------------------------------------------------------------------------------------------- 
> 
> 
> Problem is, as soon as I have an underscore in the image path, the 
> rendering will stop at that point, and continue spitting out the rest of 
> the URL as raw text. What am I doing wrong? Is it DocumentWithImages or 
> HTMLWithImages themselves, or is it the above subclass (with 
> _URL_AND_PUNC) that lead to this incorrect rendering?
> (btw, Zope Version is 2.4.1b1)

Hmm, the code I posted was to work around the lack of underscore
in Zope 2.3's version of StructuredText.DocumentWithImages.doc_href;
in Zope 2.4, you shouldn't need my hack to fix 'doc_href'.  OTOH,
'doc_img' is still broken in Zope 2.4.

The workaround should be to hack in 'doc_img' into your 'DWIClass',
such that the patterns it passes to the base class' 'doc_img' include
underscores.

Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com