import os.path
import textwrap
import types
+from urllib import quote_plus
import yaml
self.url = url
self.tags = tags
- def clean_name(self):
+ def clean_name(self, ascii=False):
name = self.name
- for from_,to in [(' ','_'), ('/', '_'),
- (',', ''), (u'\xe2\x80\x99', ''),
- ('&', 'and')]:
+ for from_,to in [(' ','_'), ('/', '_'), (',', ''), ('&', 'and')]:
name = name.replace(from_, to)
+ if ascii == True:
+ return quote_plus(name.encode('utf-8'))
return name
def matches_tags(self, tags):
for recipe in self:
self.index[recipe.name] = recipe
self.index[recipe.clean_name()] = recipe
+ self.index[recipe.clean_name(ascii=True)] = recipe
def tags(self):
"""List all tags used in this cookbook.
if name == None:
recipe = random.choice(self.cookbook)
else:
- if type(name) == types.StringType:
- name = unicode(name, encoding='utf-8')
+ if isinstance(name, types.StringType):
+ name = unicode(name, 'utf-8')
recipe = self.cookbook.index[name]
template = self.env.get_template('recipe.html')
return template.render(cookbook=self.cookbook, recipe=recipe)
@cherrypy.expose
def add_tag(self, name, tag):
"""Add a tag to a single recipe."""
- if type(name) == types.StringType:
- name = unicode(name, encoding='utf-8')
+ if isinstance(name, types.StringType):
+ name = unicode(name, 'utf-8')
recipe = self.cookbook.index[name]
if recipe.tags == None:
recipe.tags = []
with open(recipe.path, 'w') as f:
recipe.save(f)
raise cherrypy.HTTPRedirect(
- 'recipe?name=%s' % recipe.clean_name(), status=302)
+ u'recipe?name=%s' % recipe.clean_name(ascii=True), status=302)
@cherrypy.expose
def remove_tag(self, name, tag):
"""Remove a tag from a single recipe."""
- if type(name) == types.StringType:
- name = unicode(name, encoding='utf-8')
+ if isinstance(name, types.StringType):
+ name = unicode(name, 'utf-8')
recipe = self.cookbook.index[name]
if recipe.tags == None:
return
with open(recipe.path, 'w') as f:
recipe.save(f)
raise cherrypy.HTTPRedirect(
- 'recipe?name=%s' % recipe.clean_name(), status=302)
+ u'recipe?name=%s' % recipe.clean_name(ascii=True), status=302)
def _clean_tag(self, tag):
"""Sanitize tag."""