Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tag indexing #144

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tumblr_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import sys
import threading
import time
import unicodedata
import urllib
import urllib2
import urlparse
Expand Down Expand Up @@ -103,6 +104,8 @@ def test_jpg(h, f):
# bb-tumblr-backup API key
API_KEY = '8YUsKJvcJxo2MDwmWMDiXZGuMuIbeCwuQGP5ZHSEA4jBJPMnJT'

FILENAME_LIMIT = 250

# ensure the right date/time format
try:
locale.setlocale(locale.LC_TIME, '')
Expand Down Expand Up @@ -292,6 +295,18 @@ def get_style():
return


def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
value = unicode(re.sub('[-\s]+', '-', value))
# TODO: figure out what to do if there's a collision here...
return value[:FILENAME_LIMIT]


class Index:

def __init__(self, blog, body_class='index'):
Expand Down Expand Up @@ -414,7 +429,8 @@ def save_tag_index(self):
mkdir(path_to(tag_index_dir))
self.fixup_media_links()
tag_index = [self.blog.header('Tag index', 'tag-index', self.blog.title, True), '<ul>']
for tag, index in sorted(self.tags.items(), key=lambda kv: kv[1].name):
for _, index in sorted(self.tags.items(), key=lambda kv: kv[1].name):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tags.values() to get just the values instead of tuples

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I’m aware of this.

tag = slugify(index.name)
index.save_index(tag_index_dir + os.sep + tag,
u"Tag ‛%s’" % index.name
)
Expand Down