Slugifying German string with Python
I'm working on this project that has some German titles, and a lot of irregularities inside them
(question marks, underscores, etc.)
Because I have to refer the object (in this case product) by some sort of understandable name, I've decided to slugify the title
(basically turn "THIS EXAMPLE!!" to "this-example") While I'm already doing that, I wanted to cleverly switch the "ä,ö,ü,ß" to "ae,oe,ue,ss" accordingly.
I'm pretty sure this might be useful to someone, and it's pretty harmless to share it so...
this gist was born, and the code is:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# -*- coding: utf-8 -*- def replace_all(text, dic): for replace_with, replace_what in dic.iteritems(): if type(replace_what) is list: for letter in replace_what: text = text.replace(letter,replace_with) else: text = text.replace(replace_what, replace_with) return text #example input: ssdfSERFS - ^#$%43 GSDG ____ :VKbm sdF öüoäßßEF_____-___ d a s d _e <>DFGDFG???? #example output: ssdfserfs-43-gsdg-vkbm-sdf-oeueoaessssef-d-a-s-d-e-dfgdfg def slugify(string,params = []): import re #unescape html entity signs (e.g. &amp; -> &) & lowercase string import HTMLParser string = HTMLParser.HTMLParser().unescape(string).lower() translation_dic = { 'ae' : ['Ä','ä','&Auml;','&auml;'], 'oe' : ['Ö','ö','&Ouml;','&ouml;'], 'ue' : ['Ü','ü','&Uuml;','&uuml;'], 'ss' : ['ß','&szlig;'], } string = replace_all(string,translation_dic) string = re.sub(r'[^a-zA-Z0-9\_\-\s]','',string) string = re.sub(r'[\s]{2,}',' ',string) string = string.replace(' ','-').replace('_','-').strip(' -') string = re.sub(r'[\-]{2,}','-',string) return string
Posted in TechnologyRunning Django under a Shared Hosting Environment
I've been developing with Django on my local machine,
and I was wondering if there was a way for me to
deploy it on a shared hosting server,
after playing around with it for a few hours, it actually works!Posted in TechnologyEmmet is an awesome web developer's plugin
I've recently came across a nice little plugin called Emmet.This plugin is basically an awesome Snippets style plugin,
that lets you generate some highly customizable HTML templates in seconds.Posted in TechnologyNever trust file_get_contents without error catching
The function file_get_contents could get uncomfortable when it comes to error handling.The best (if you could call it that) way to handle errors from this function is to suppress the error
while calling the function, and checking if the result is false (of course one could always catch E_WARNING errors with a handler, but I don't like that).Posted in TechnologyDropbox - source control for lazy developers
Sometimes when I start a new project, the work is pretty intense.
The first few weeks usually involve major changes to the directory structure.Posted in Technology