Changeset 1639

Show
Ignore:
Timestamp:
06/05/08 16:20:04 (6 months ago)
Author:
JensDiemer
Message:

Update IncludeRemote? Plugin:

  • better charset handling
  • add preformatted text mode
Location:
trunk/pylucid
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/media/PyLucid/internal_page/IncludeRemote/IncludeRemote.html

    r1449 r1639  
    11<fieldset> 
    22{% if title %}<legend><a href="{{ url }}">{{ title }}</a></legend>{% endif %} 
     3{% if preformat %}<pre>{% endif %} 
    34{{ content }} 
     5{% if preformat %}</pre>{% endif %} 
    46</fieldset> 
    57<small class="IncludeRemote_info"> 
  • trunk/pylucid/PyLucid/plugins_internal/IncludeRemote/IncludeRemote.py

    r1634 r1639  
    2020 
    2121 
    22 import socket, urllib2, re, time 
     22import socket, urllib2, cgi, re, time 
    2323 
    2424socket.setdefaulttimeout(5) # set a timeout 
     
    2929from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    3030 
     31STRIP_CONTENT = ( 
     32    # stripe stylesheet links 
     33    re.compile('(<link.*?rel.*?stylesheet.*?>)(?is)'), 
     34    # strip javascript 
     35    re.compile("(<script.*?</script>)(?is)") 
     36) 
     37 
     38META_CHARSET = re.compile('<meta.*?charset=(.*?)"') 
     39BODY_RE = re.compile("<body.*?>(.*?)</body>(?is)") 
    3140 
    3241class IncludeRemote(PyLucidBasePlugin): 
    3342 
    34     def lucidTag(self, url, title=None, escape=True): 
    35  
     43    def lucidTag(self, url, title=None, preformat=None, escape=True): 
     44        """ 
     45        docu about the method args, look into config file! 
     46        """ 
    3647        # get the remote content 
    3748        start_time = time.time() 
     
    4051            content = f.read() 
    4152            f.close() 
    42         except Exception, e: 
    43             return ( 
    44                 "<p>IncludeRemote error! Can't get '%s'<br />" 
    45                 " error:'%s'</p>" 
    46             ) % (url, e) 
     53        except Exception, err: 
     54            if self.request.debug: 
     55                self.page_msg.red("Include remote '%s' error: %s" % (url, err)) 
     56            return "[error getting '%s'.]" % title 
     57 
    4758        duration_time = time.time() - start_time 
    4859 
     60        #______________________________________________________________________ 
     61        # GET HEADER INFO 
    4962 
    50         # cutout stylesheets 
    51         try: 
    52             content = re.sub( 
    53                 '(<link.*?rel.*?stylesheet.*?>)(?is)',"",content 
    54             ) 
    55         except: 
    56             pass 
     63        content_type = None 
     64        content_encodings = [] 
     65 
     66        # detect content type and encoding 
     67        raw_content_type = f.headers.get("content-type") 
     68        if raw_content_type: 
     69            content_type, params = cgi.parse_header(raw_content_type) 
     70            if "charset" in params: 
     71                content_encodings.append(params["charset"]) 
    5772 
    5873 
    59         # cutout JavaScripts 
     74        # Try to get content charset from html meta info 
    6075        try: 
    61             content = re.sub( 
    62                 '(<script.*?</script>)(?is)',"",content 
    63             ) 
    64         except: 
     76            charset = META_CHARSET.findall(content.lower())[0] 
     77        except IndexError: 
    6578            pass 
    66  
     79        except Exception, err: 
     80            if self.request.debug: 
     81                self.page_msg.red("Error get content charset:", err) 
     82        else: 
     83            content_encodings.append(charset) 
    6784 
    6885        # decode into unicode 
    69         try: 
    70             charset = re.findall( 
    71                 '<meta.*?content-type.*?charset=(.*?)"', content.lower() 
    72             )[0] 
    73             content = content.decode(charset) 
    74         except: 
    75             content = smart_unicode(content, errors='replace') 
     86        content = self._decode_content(content, content_encodings) 
    7687 
     88        #______________________________________________________________________ 
    7789 
    7890        # try to cut out only the body content 
    7991        try: 
    80             content = re.findall("<body.*?>(.*?)</body>(?is)", content)[0] 
    81         except: 
     92            content = BODY_RE.findall(content)[0] 
     93        except IndexError: 
    8294            pass 
     95        except Exception, err: 
     96            if self.request.debug: 
     97                self.page_msg.red("Error strip body content:", err) 
    8398 
     99        #______________________________________________________________________ 
     100        # Strip content 
     101 
     102        for regex in STRIP_CONTENT: 
     103            try: 
     104                content = regex.sub(u"",content) 
     105            except Exception, err: 
     106                if self.request.debug: 
     107                    self.page_msg.red("Error strip content:", err) 
     108 
     109        #______________________________________________________________________ 
    84110 
    85111        if not escape: 
     
    87113            content = mark_safe(content) 
    88114 
     115        # setup preformat 
     116        if preformat==None and "html" in content_type: 
     117            preformat = False 
     118        else: 
     119            preformat = True 
    89120 
    90121        context = { 
     
    93124            "title": title, 
    94125            "content": content, 
     126            "preformat": preformat, 
    95127        } 
    96128        self._render_template("IncludeRemote", context)#, debug=True) 
     129 
     130    def _decode_content(self, content, content_encodings): 
     131        """ 
     132        Try to decode content into unicode with the given encoding list. 
     133        """ 
     134        if not content_encodings: 
     135            # No charset found. 
     136            return smart_unicode(content, errors='replace') 
     137 
     138        errors = [] 
     139        for charset in content_encodings: 
     140            try: 
     141                content = content.decode(charset) 
     142            except UnicodeDecodeError, err: 
     143                errors.append(err) 
     144            else: 
     145                return content 
     146 
     147        if self.request.debug: 
     148            self.page_msg.red("Can't decode content:", errors) 
     149 
     150        return smart_unicode(content, errors='replace') 
  • trunk/pylucid/PyLucid/plugins_internal/IncludeRemote/IncludeRemote_cfg.py

    r1634 r1639  
    88__long_description__ = """ 
    99Includes a remote website into your CMS page. 
     10 
     11=== parameters 
     12 
     13 
     14==== url: 
     15 
     16The url to get. 
     17 
     18 
     19==== title: 
     20 
     21A title for the fieldset header. 
     22 
     23 
     24==== preformat: 
     25 
     26Is the content preformatted text? Should we surround it with <pre> tags? 
     27 
     28preformat="None": 
     29    Automatic modus. If content type is html, don't surround with <pre> tag, 
     30    else add <pre> tag. 
     31 
     32preformat="True": 
     33    always add <pre> tag. 
     34 
     35preformat="False": 
     36    don't add <pre> tag 
     37 
     38 
     39==== escape: 
     40 
     41Is the receaved content safe (escape=False) or should we escape all html 
     42entries (escape=True)? 
    1043""" 
    1144