Permalink
Browse files

Add a contextual description metatag for scrapers.

  • Loading branch information...
1 parent 3e5ed4d commit a86261f352f265efe4481dfb5f04eb078ad29998 @andre-d andre-d committed with spladug Aug 8, 2011
View
@@ -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
View
@@ -50,6 +50,7 @@
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 @@
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,14 +806,18 @@ def __init__(self, link = None, comment = None,
# 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)
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 @@ def __init__(self, link = None, comment = None,
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))
View
@@ -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):
@@ -819,7 +819,7 @@ def get_links(self, sort, time):
@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):
@@ -39,6 +39,7 @@
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<title>${self.Title()}</title>
<meta name="title" content="${self.Title()}" />
+ <meta name="description" content="${thing.short_description or g.short_description}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
${self.robots()}
<link rel="stylesheet" href="${static('compact.css')}" type="text/css" media="screen" />
@@ -34,6 +34,7 @@
<meta name="keywords" content="${self.keywords()}" />
<meta name="title" content="${self.Title()}" />
+ <meta name="description" content="${getattr(thing, 'short_description', None) or g.short_description}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
${self.robots()}

0 comments on commit a86261f

Please sign in to comment.