def generate_summary(self, nchars=200): '''提出摘要, 最终返回以HTML片段. 代表插图 + 前N个文字''' orig_html = pq(self.html_content) # 优先提取带有 cover 类的图片作为封面 cover = orig_html('img.cover:first') or orig_html('img:first') if cover: try: # 清楚来自图片上传插件(ckeditor)自动添加的属性 cover.removeAttr('style').removeAttr('width').removeAttr( 'height') except KeyError: pass cover.addClass('cover') orig_src = cover.attr('src') # 如果是本地图片, 则封面img标签使用django-filebrowser生成的缩略图 if orig_src.startswith(settings.MEDIA_URL): print "更新封面" relative_path = orig_src.replace(settings.MEDIA_URL, '') if relative_path.startswith( settings.FILEBROWSER_VERSIONS_BASEDIR): # 如果已经引用的是FileBrowser生成的小尺寸图片, # 则试图推导出原图路径, 并根据原图生成缩略图. relative_path = re.sub( r'^%s' % settings.FILEBROWSER_VERSIONS_BASEDIR, settings.FILEBROWSER_DIRECTORY, relative_path) # FileBrowser生成图片的后缀模式: postfix_pat = '|'.join( ['_' + i for i in settings.FILEBROWSER_ADMIN_VERSIONS]) relative_path = re.sub(r'(%s)\.' % postfix_pat, '.', relative_path) fileobject = FileObject(relative_path) if fileobject.exists(): fileobject = fileobject.original thumbnail = fileobject.version_generate('thumbnail') cover.attr('src', thumbnail.url).attr('data-orig-src', orig_src) cover.css(height='100px', width='100px') else: print u'引用的图片不存在: %s' % fileobject.path summary_text = cgi.escape(orig_html.text()[:int(nchars)]) return (cover.outerHtml() or "") + \ (u'<span class="summary-text">%s...</span>' % summary_text) + \ (u'<a class="more" href="/%d/">阅读全文→</a>' % self.id) + \ u'<div class="clear"></div>'
def generate_summary(self, nchars=200): '''提出摘要, 最终返回以HTML片段. 代表插图 + 前N个文字''' orig_html = pq(self.html_content) # 优先提取带有 cover 类的图片作为封面 cover = orig_html('img.cover:first') or orig_html('img:first') if cover: try: # 清楚来自图片上传插件(ckeditor)自动添加的属性 cover.removeAttr('style').removeAttr('width').removeAttr('height') except KeyError: pass cover.addClass('cover') orig_src = cover.attr('src') # 如果是本地图片, 则封面img标签使用django-filebrowser生成的缩略图 if orig_src.startswith(settings.MEDIA_URL): print "更新封面" relative_path = orig_src.replace(settings.MEDIA_URL, '') if relative_path.startswith(settings.FILEBROWSER_VERSIONS_BASEDIR): # 如果已经引用的是FileBrowser生成的小尺寸图片, # 则试图推导出原图路径, 并根据原图生成缩略图. relative_path = re.sub(r'^%s' % settings.FILEBROWSER_VERSIONS_BASEDIR, settings.FILEBROWSER_DIRECTORY, relative_path) # FileBrowser生成图片的后缀模式: postfix_pat = '|'.join(['_'+i for i in settings.FILEBROWSER_ADMIN_VERSIONS]) relative_path = re.sub(r'(%s)\.' % postfix_pat, '.', relative_path) fileobject = FileObject(relative_path) if fileobject.exists(): fileobject = fileobject.original thumbnail = fileobject.version_generate('thumbnail') cover.attr('src', thumbnail.url).attr('data-orig-src', orig_src) cover.css(height='100px', width='100px') else: print u'引用的图片不存在: %s' % fileobject.path summary_text = cgi.escape(orig_html.text()[:int(nchars)]) return (cover.outerHtml() or "") + \ (u'<span class="summary-text">%s...</span>' % summary_text) + \ (u'<a class="more" href="/%d/">阅读全文→</a>' % self.id) + \ u'<div class="clear"></div>'