Changeset 1632 for trunk/LeMillTool.py


Ignore:
Timestamp:
04/11/07 23:04:36 (13 years ago)
Author:
jukka
Message:

Added support for wikilike linking between objects. [Fle3] or [Jukka] in bodytext translates to link to corresponding object between when displaying text. my page is same as [Jukka my page] and same as [Jukka]. First argument is objects id or url and second is linked text. This is enhancement as hypothesis, we can roll this back if it doesn't work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LeMillTool.py

    r1630 r1632  
    3535import sre 
    3636from itertools import chain, dropwhile 
     37 
     38brackets=sre.compile('\[(.*?)\]') 
     39hreflinks=sre.compile('<a href="(.*?)">(.*?)</a>') 
     40httplinks=sre.compile('(?<!href=")(http://[^ \t\n\r<>"]+)') 
    3741 
    3842class LeMillTool(PropertyManager, UniqueObject, SimpleItem): 
     
    475479 
    476480 
    477     @staticmethod 
    478     def htmlify(s): 
     481    def htmlify(self,s): 
    479482        """ does NOT put a <p> at the beginning and </p> at the end... """ 
    480         for tag in ('<a ', '<br />', '<p>', '<object ', '<param ', '<embed '): 
     483        s=self.parse_bracket_links(s) 
     484        s=httplinks.sub('<a href="\\1">\\1</a>',s) 
     485        for tag in ('<p>','<br />', '<object ', '<param ', '<embed '): 
    481486            if s.find(tag) != -1: 
    482487                return s 
    483         return sre.sub('(http://[^ \t\n<>]+)', '<a href="\\1">\\1</a>', s.replace('<', '&gt;').replace('>', '&lt;'))\ 
     488        return s.replace('<', '&gt;').replace('>', '&lt;')\ 
    484489                .replace('\r', '').replace('\n\n', '</p><p>')\ 
    485490                .replace('\n', '<br />').replace('<p><br />', '<p>')\ 
     
    488493    @staticmethod 
    489494    def shorten_link_names(s, max_len=60): 
    490         l = sre.split('<a href="(.*?)">(.*?)</a>', s)   # [stuff, link, link_name, stuff, link, link_name, stuff,...] 
     495        l = hreflinks.split(s)   # [stuff, link, link_name, stuff, link, link_name, stuff,...] 
    491496        assert len(l) % 3 == 1 
    492497 
     
    505510 
    506511 
    507     @staticmethod 
    508     def parse_local_links(s): 
    509         return s 
     512    def parse_bracket_links(self, s): 
     513        pc=getToolByName(self, 'portal_catalog') 
     514        l = brackets.split(s) # [stuff, link, stuff..] 
     515        new = [] 
     516        for stuff, linkstr in zip(l[::2], l[1::2]): 
     517            link=linkstr.split(' ',1) # whitespace split 
     518            if len(link)==1: 
     519                link_name=link[0] 
     520            else: 
     521                link_name=link[1] 
     522            if link[0].startswith("http://"): 
     523                link=link[0] 
     524            else:                 
     525                matches=pc({'id':link[0]}) 
     526                if matches: 
     527                    link=matches[0].getURL() 
     528                else: 
     529                    link='' 
     530            if link: 
     531                new.append('%s<a href="%s">%s</a>' % (stuff, link, link_name)) 
     532            else: 
     533                new.append('%s[%s]' % (stuff,linkstr)) 
     534        return ''.join(new + [l[-1]]) 
    510535 
    511536    @staticmethod 
Note: See TracChangeset for help on using the changeset viewer.