utils.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:tingsmen 作者: pasqu4le 项目源码 文件源码
def autolink(self, link, is_email):
        if is_email:
            return '<a href="mailto:{0}">{0}</a>'.format(link)
        # analyze url
        parsed = urlparse(link)
        if 'youtube.com' in parsed.netloc:
            vid_id = dict(item.split('=') for item in parsed.query.split('&'))['v']
            return youtube_embed(vid_id)
        if 'youtu.be' in parsed.netloc:
            vid_id = parsed.path
            return youtube_embed(vid_id)
        if parsed.path.lower().endswith(('.jpg', '.jpeg', '.jpe', '.jif', '.jfif', '.jfi', '.gif', '.png')):
            return '<a href="{0}"><img src="{0}" class="img-responsive img-thumbnail" /></a>'.format(link)
        # standard case autolink
        res = requests.get(link)
        soup = BeautifulSoup(res.text, 'html.parser')
        title = soup.title.string
        if len(title) > 50:
            title = title[:47] + '...'
        icon = soup.find("link", rel="shortcut icon")
        if icon:
            icon = icon['href']
            # might be not absolute
            if not icon.startswith('http'):
                # url might have changed
                parsed = urlparse(res.url)
                if not icon.startswith('/'):
                    icon = '/' + icon
                icon = parsed.scheme + '://' + parsed.netloc + icon
        render_autolink = get_template_attribute('macros.html', 'render_autolink')
        return render_autolink(link, title, icon).unescape()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号