PageRenderTime 315ms CodeModel.GetById 287ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/media/js/urlify.js

https://code.google.com/p/mango-py/
JavaScript | 140 lines | 129 code | 8 blank | 3 comment | 9 complexity | 11f885692a3b65de7f75581e1c2a5a1c MD5 | raw file
  1var LATIN_MAP = {
  2    'Ŕ': 'A', 'Á': 'A', 'Â': 'A', 'Ă': 'A', 'Ä': 'A', 'Ĺ': 'A', 'Ć': 'AE', 'Ç':
  3    'C', 'Č': 'E', 'É': 'E', 'Ę': 'E', 'Ë': 'E', 'Ě': 'I', 'Í': 'I', 'Î': 'I',
  4    'Ď': 'I', 'Đ': 'D', 'Ń': 'N', 'Ň': 'O', 'Ó': 'O', 'Ô': 'O', 'Ő': 'O', 'Ö':
  5    'O', '?': 'O', 'Ř': 'O', 'Ů': 'U', 'Ú': 'U', 'Ű': 'U', 'Ü': 'U', '?': 'U',
  6    'Ý': 'Y', 'Ţ': 'TH', 'ß': 'ss', 'ŕ':'a', 'á':'a', 'â': 'a', 'ă': 'a', 'ä':
  7    'a', 'ĺ': 'a', 'ć': 'ae', 'ç': 'c', 'č': 'e', 'é': 'e', 'ę': 'e', 'ë': 'e',
  8    'ě': 'i', 'í': 'i', 'î': 'i', 'ď': 'i', 'đ': 'd', 'ń': 'n', 'ň': 'o', 'ó':
  9    'o', 'ô': 'o', 'ő': 'o', 'ö': 'o', '?': 'o', 'ř': 'o', 'ů': 'u', 'ú': 'u',
 10    'ű': 'u', 'ü': 'u', '?': 'u', 'ý': 'y', 'ţ': 'th', '˙': 'y'
 11}
 12var LATIN_SYMBOLS_MAP = {
 13    'Š':'(c)'
 14}
 15var GREEK_MAP = {
 16    '?':'a', '?':'b', '?':'g', '?':'d', '?':'e', '?':'z', '?':'h', '?':'8',
 17    '?':'i', '?':'k', '?':'l', '?':'m', '?':'n', '?':'3', '?':'o', '?':'p',
 18    '?':'r', '?':'s', '?':'t', '?':'y', '?':'f', '?':'x', '?':'ps', '?':'w',
 19    '?':'a', '?':'e', '?':'i', '?':'o', '?':'y', '?':'h', '?':'w', '?':'s',
 20    '?':'i', '?':'y', '?':'y', '?':'i',
 21    '?':'A', '?':'B', '?':'G', '?':'D', '?':'E', '?':'Z', '?':'H', '?':'8',
 22    '?':'I', '?':'K', '?':'L', '?':'M', '?':'N', '?':'3', '?':'O', '?':'P',
 23    '?':'R', '?':'S', '?':'T', '?':'Y', '?':'F', '?':'X', '?':'PS', '?':'W',
 24    '?':'A', '?':'E', '?':'I', '?':'O', '?':'Y', '?':'H', '?':'W', '?':'I',
 25    '?':'Y'
 26}
 27var TURKISH_MAP = {
 28    '?':'s', '?':'S', '?':'i', '?':'I', 'ç':'c', 'Ç':'C', 'ü':'u', 'Ü':'U',
 29    'ö':'o', 'Ö':'O', '?':'g', '?':'G'
 30}
 31var RUSSIAN_MAP = {
 32    '?':'a', '?':'b', '?':'v', '?':'g', '?':'d', '?':'e', '?':'yo', '?':'zh',
 33    '?':'z', '?':'i', '?':'j', '?':'k', '?':'l', '?':'m', '?':'n', '?':'o',
 34    '?':'p', '?':'r', '?':'s', '?':'t', '?':'u', '?':'f', '?':'h', '?':'c',
 35    '?':'ch', '?':'sh', '?':'sh', '?':'', '?':'y', '?':'', '?':'e', '?':'yu',
 36    '?':'ya',
 37    '?':'A', '?':'B', '?':'V', '?':'G', '?':'D', '?':'E', '?':'Yo', '?':'Zh',
 38    '?':'Z', '?':'I', '?':'J', '?':'K', '?':'L', '?':'M', '?':'N', '?':'O',
 39    '?':'P', '?':'R', '?':'S', '?':'T', '?':'U', '?':'F', '?':'H', '?':'C',
 40    '?':'Ch', '?':'Sh', '?':'Sh', '?':'', '?':'Y', '?':'', '?':'E', '?':'Yu',
 41    '?':'Ya'
 42}
 43var UKRAINIAN_MAP = {
 44    '?':'Ye', '?':'I', '?':'Yi', '?':'G', '?':'ye', '?':'i', '?':'yi', '?':'g'
 45}
 46var CZECH_MAP = {
 47    '?':'c', '?':'d', '?':'e', '?': 'n', '?':'r', 'š':'s', '?':'t', '?':'u',
 48    'ž':'z', '?':'C', '?':'D', '?':'E', '?': 'N', '?':'R', 'Š':'S', '?':'T',
 49    '?':'U', 'Ž':'Z'
 50}
 51
 52var POLISH_MAP = {
 53    '?':'a', '?':'c', '?':'e', '?':'l', '?':'n', 'ó':'o', '?':'s', '?':'z',
 54    '?':'z', '?':'A', '?':'C', '?':'e', '?':'L', '?':'N', 'Ó':'o', '?':'S',
 55    '?':'Z', '?':'Z'
 56}
 57
 58var LATVIAN_MAP = {
 59    '?':'a', '?':'c', '?':'e', '?':'g', '?':'i', '?':'k', '?':'l', '?':'n',
 60    'š':'s', '?':'u', 'ž':'z', '?':'A', '?':'C', '?':'E', '?':'G', '?':'i',
 61    '?':'k', '?':'L', '?':'N', 'Š':'S', '?':'u', 'Ž':'Z'
 62}
 63
 64var ALL_DOWNCODE_MAPS=new Array()
 65ALL_DOWNCODE_MAPS[0]=LATIN_MAP
 66ALL_DOWNCODE_MAPS[1]=LATIN_SYMBOLS_MAP
 67ALL_DOWNCODE_MAPS[2]=GREEK_MAP
 68ALL_DOWNCODE_MAPS[3]=TURKISH_MAP
 69ALL_DOWNCODE_MAPS[4]=RUSSIAN_MAP
 70ALL_DOWNCODE_MAPS[5]=UKRAINIAN_MAP
 71ALL_DOWNCODE_MAPS[6]=CZECH_MAP
 72ALL_DOWNCODE_MAPS[7]=POLISH_MAP
 73ALL_DOWNCODE_MAPS[8]=LATVIAN_MAP
 74
 75var Downcoder = new Object();
 76Downcoder.Initialize = function()
 77{
 78    if (Downcoder.map) // already made
 79        return ;
 80    Downcoder.map ={}
 81    Downcoder.chars = '' ;
 82    for(var i in ALL_DOWNCODE_MAPS)
 83    {
 84        var lookup = ALL_DOWNCODE_MAPS[i]
 85        for (var c in lookup)
 86        {
 87            Downcoder.map[c] = lookup[c] ;
 88            Downcoder.chars += c ;
 89        }
 90     }
 91    Downcoder.regex = new RegExp('[' + Downcoder.chars + ']|[^' + Downcoder.chars + ']+','g') ;
 92}
 93
 94downcode= function( slug )
 95{
 96    Downcoder.Initialize() ;
 97    var downcoded =""
 98    var pieces = slug.match(Downcoder.regex);
 99    if(pieces)
100    {
101        for (var i = 0 ; i < pieces.length ; i++)
102        {
103            if (pieces[i].length == 1)
104            {
105                var mapped = Downcoder.map[pieces[i]] ;
106                if (mapped != null)
107                {
108                    downcoded+=mapped;
109                    continue ;
110                }
111            }
112            downcoded+=pieces[i];
113        }
114    }
115    else
116    {
117        downcoded = slug;
118    }
119    return downcoded;
120}
121
122
123function URLify(s, num_chars) {
124    // changes, e.g., "Petty theft" to "petty_theft"
125    // remove all these words from the string before urlifying
126    s = downcode(s);
127    removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from",
128                  "is", "in", "into", "like", "of", "off", "on", "onto", "per",
129                  "since", "than", "the", "this", "that", "to", "up", "via",
130                  "with"];
131    r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi');
132    s = s.replace(r, '');
133    // if downcode doesn't hit, the char will be stripped here
134    s = s.replace(/[^-\w\s]/g, '');  // remove unneeded chars
135    s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
136    s = s.replace(/[-\s]+/g, '-');   // convert spaces to hyphens
137    s = s.toLowerCase();             // convert to lowercase
138    return s.substring(0, num_chars);// trim to first num_chars chars
139}
140