From a86261f352f265efe4481dfb5f04eb078ad29998 Mon Sep 17 00:00:00 2001 From: Andre D Date: Mon, 8 Aug 2011 13:12:36 -0700 Subject: [PATCH] Add a contextual description metatag for scrapers. --- r2/example.ini | 3 +++ r2/r2/lib/pages/pages.py | 14 +++++++++++--- r2/r2/lib/utils/utils.py | 3 +++ r2/r2/models/subreddit.py | 2 +- r2/r2/templates/base.compact | 1 + r2/r2/templates/base.html | 1 + 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/r2/example.ini b/r2/example.ini index 39d6dc29b..8db844978 100755 --- a/r2/example.ini +++ b/r2/example.ini @@ -28,6 +28,9 @@ allow_wiki_editing = true # a modified read only mode used for cache shown during heavy load 503s heavy_load_mode = false +# the site's tagline, used in the title and description +short_description = open source is awesome + # -- SECRETS! <-- update these first! -- # global secret SECRET = abcdefghijklmnopqrstuvwxyz0123456789 diff --git a/r2/r2/lib/pages/pages.py b/r2/r2/lib/pages/pages.py index c6c9134ec..9becd8238 100644 --- a/r2/r2/lib/pages/pages.py +++ b/r2/r2/lib/pages/pages.py @@ -50,6 +50,7 @@ from r2.lib.subreddit_search import popular_searches from r2.lib.scraper import get_media_embed from r2.lib.log import log_text from r2.lib.memoize import memoize +from r2.lib.utils import trunc_string as _truncate import sys, random, datetime, locale, calendar, simplejson, re, time import graph, pycountry, time @@ -60,6 +61,8 @@ from things import wrap_links, default_thing_wrapper datefmt = _force_utf8(_('%d %b %Y')) +MAX_DESCRIPTION_LENGTH = 150 + def get_captcha(): if not c.user_is_loggedin or c.user.needs_captcha(): return get_iden() @@ -113,11 +116,12 @@ class Reddit(Templated): additional_css = None def __init__(self, space_compress = True, nav_menus = None, loginbox = True, - infotext = '', content = None, title = '', robots = None, + infotext = '', content = None, short_description='', title = '', robots = None, show_sidebar = True, footer = True, srbar = True, **context): Templated.__init__(self, **context) self.title = title + self.short_description = short_description self.robots = robots self.infotext = infotext self.loginbox = True @@ -802,7 +806,9 @@ class LinkInfoPage(Reddit): # defaults whether or not there is a comment params = {'title':_force_unicode(link_title), 'site' : c.site.name} title = strings.link_info_title % params - + short_description = None + if link and link.selftext: + short_description = _truncate(link.selftext.strip(), MAX_DESCRIPTION_LENGTH) # only modify the title if the comment/author are neither deleted nor spam if comment and not comment._deleted and not comment._spam: author = Account._byID(comment.author_id, data=True) @@ -810,6 +816,8 @@ class LinkInfoPage(Reddit): if not author._deleted and not author._spam: params = {'author' : author.name, 'title' : _force_unicode(link_title)} title = strings.permalink_title % params + short_description = _truncate(comment.body.strip(), MAX_DESCRIPTION_LENGTH) if comment.body else None + self.subtitle = subtitle @@ -823,7 +831,7 @@ class LinkInfoPage(Reddit): else: self.duplicates = duplicates - Reddit.__init__(self, title = title, *a, **kw) + Reddit.__init__(self, title = title, short_description=short_description, *a, **kw) def build_toolbars(self): base_path = "/%s/%s/" % (self.link._id36, title_to_url(self.link.title)) diff --git a/r2/r2/lib/utils/utils.py b/r2/r2/lib/utils/utils.py index dc1712c57..23d56b961 100644 --- a/r2/r2/lib/utils/utils.py +++ b/r2/r2/lib/utils/utils.py @@ -304,6 +304,9 @@ def sanitize_url(url, require_scheme = False): return return url +def trunc_string(text, length): + return text[0:length]+'...' if len(text)>length else text + # Truncate a time to a certain number of minutes # e.g, trunc_time(5:52, 30) == 5:30 def trunc_time(time, mins, hours=None): diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 5336ac36e..dfb0d2d8f 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -819,7 +819,7 @@ class _DefaultSR(FakeSubreddit): @property def title(self): - return _("reddit: the front page of the internet") + return _(g.short_description) # This is the base class for the instantiated front page reddit class DefaultSR(_DefaultSR): diff --git a/r2/r2/templates/base.compact b/r2/r2/templates/base.compact index a2aeb3a3b..65badead0 100644 --- a/r2/r2/templates/base.compact +++ b/r2/r2/templates/base.compact @@ -39,6 +39,7 @@ ${self.Title()} + ${self.robots()} diff --git a/r2/r2/templates/base.html b/r2/r2/templates/base.html index 6ed38831e..0d641edc4 100644 --- a/r2/r2/templates/base.html +++ b/r2/r2/templates/base.html @@ -34,6 +34,7 @@ + ${self.robots()}