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) Thank you very much in advance, Danny
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
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.
Thanks Tres, I got it working! All I did was include the underscore in the regex, after the ":img:". Original: ------------------------------------------------------------------------------ def doc_img( self, s, expr1 = re.compile('\"([ _a-zA-Z0-9*.:/;,\-\n\~]+)\":img:([a-zA-Z0-9\-.:/;,\n\~]+)').search, expr2 = re.compile('\"([ _a-zA-Z0-9*.:/;,\-\n\~]+)\":img:([a-zA-Z0-9\-.:/;,\n\~]+):([a-zA-Z0-9\-.:/;,\n\~]+)').search): ------------------------------------------------------------------------------ New: ------------------------------------------------------------------------------ def doc_img( self, s, expr1 = re.compile('\"([ _a-zA-Z0-9*.:/;,\-\n\~]+)\":img:([_a-zA-Z0-9\-.:/;,\n\~]+)').search, expr2 = re.compile('\"([ _a-zA-Z0-9*.:/;,\-\n\~]+)\":img:([_a-zA-Z0-9\-.:/;,\n\~]+):([a-zA-Z0-9\-.:/;,\n\~]+)').search): ------------------------------------------------------------------------------ So it was actually just two characters missing, hoho. After rewriting this using the easier to read _ABS_AND_RELATIVE_URL notation of DocumentClass.py I will post the external method for others to use. I have the feeling that when DocumentClass.py (CVS 2 weeks) was updated, DocumentWithImages.py wasn't (CVS 2 months)... so copy goes to Andreas... Thanks for the help, Danny
participants (2)
-
Danny William Adair -
Tres Seaver