FossilRepo

fossilrepo / assets / admin / js / urlify.js
Source Blame History 169 lines
afe42d0… ragelink 1 /*global XRegExp*/
afe42d0… ragelink 2 'use strict';
afe42d0… ragelink 3 {
afe42d0… ragelink 4 const LATIN_MAP = {
afe42d0… ragelink 5 'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE',
afe42d0… ragelink 6 'Ç': 'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I',
afe42d0… ragelink 7 'Î': 'I', 'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O',
afe42d0… ragelink 8 'Õ': 'O', 'Ö': 'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U',
afe42d0… ragelink 9 'Ü': 'U', 'Ű': 'U', 'Ý': 'Y', 'Þ': 'TH', 'Ÿ': 'Y', 'ß': 'ss', 'à': 'a',
afe42d0… ragelink 10 'á': 'a', 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'æ': 'ae', 'ç': 'c',
afe42d0… ragelink 11 'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e', 'ì': 'i', 'í': 'i', 'î': 'i',
afe42d0… ragelink 12 'ï': 'i', 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o',
afe42d0… ragelink 13 'ö': 'o', 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u',
afe42d0… ragelink 14 'ű': 'u', 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
afe42d0… ragelink 15 };
afe42d0… ragelink 16 const LATIN_SYMBOLS_MAP = {
afe42d0… ragelink 17 '©': '(c)'
afe42d0… ragelink 18 };
afe42d0… ragelink 19 const GREEK_MAP = {
afe42d0… ragelink 20 'α': 'a', 'β': 'b', 'γ': 'g', 'δ': 'd', 'ε': 'e', 'ζ': 'z', 'η': 'h',
afe42d0… ragelink 21 'θ': '8', 'ι': 'i', 'κ': 'k', 'λ': 'l', 'μ': 'm', 'ν': 'n', 'ξ': '3',
afe42d0… ragelink 22 'ο': 'o', 'π': 'p', 'ρ': 'r', 'σ': 's', 'τ': 't', 'υ': 'y', 'φ': 'f',
afe42d0… ragelink 23 'χ': 'x', 'ψ': 'ps', 'ω': 'w', 'ά': 'a', 'έ': 'e', 'ί': 'i', 'ό': 'o',
afe42d0… ragelink 24 'ύ': 'y', 'ή': 'h', 'ώ': 'w', 'ς': 's', 'ϊ': 'i', 'ΰ': 'y', 'ϋ': 'y',
afe42d0… ragelink 25 'ΐ': 'i', 'Α': 'A', 'Β': 'B', 'Γ': 'G', 'Δ': 'D', 'Ε': 'E', 'Ζ': 'Z',
afe42d0… ragelink 26 'Η': 'H', 'Θ': '8', 'Ι': 'I', 'Κ': 'K', 'Λ': 'L', 'Μ': 'M', 'Ν': 'N',
afe42d0… ragelink 27 'Ξ': '3', 'Ο': 'O', 'Π': 'P', 'Ρ': 'R', 'Σ': 'S', 'Τ': 'T', 'Υ': 'Y',
afe42d0… ragelink 28 'Φ': 'F', 'Χ': 'X', 'Ψ': 'PS', 'Ω': 'W', 'Ά': 'A', 'Έ': 'E', 'Ί': 'I',
afe42d0… ragelink 29 'Ό': 'O', 'Ύ': 'Y', 'Ή': 'H', 'Ώ': 'W', 'Ϊ': 'I', 'Ϋ': 'Y'
afe42d0… ragelink 30 };
afe42d0… ragelink 31 const TURKISH_MAP = {
afe42d0… ragelink 32 'ş': 's', 'Ş': 'S', 'ı': 'i', 'İ': 'I', 'ç': 'c', 'Ç': 'C', 'ü': 'u',
afe42d0… ragelink 33 'Ü': 'U', 'ö': 'o', 'Ö': 'O', 'ğ': 'g', 'Ğ': 'G'
afe42d0… ragelink 34 };
afe42d0… ragelink 35 const ROMANIAN_MAP = {
afe42d0… ragelink 36 'ă': 'a', 'î': 'i', 'ș': 's', 'ț': 't', 'â': 'a',
afe42d0… ragelink 37 'Ă': 'A', 'Î': 'I', 'Ș': 'S', 'Ț': 'T', 'Â': 'A'
afe42d0… ragelink 38 };
afe42d0… ragelink 39 const RUSSIAN_MAP = {
afe42d0… ragelink 40 'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'yo',
afe42d0… ragelink 41 'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'j', 'к': 'k', 'л': 'l', 'м': 'm',
afe42d0… ragelink 42 'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
afe42d0… ragelink 43 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', 'щ': 'sh', 'ъ': '',
afe42d0… ragelink 44 'ы': 'y', 'ь': '', 'э': 'e', 'ю': 'yu', 'я': 'ya',
afe42d0… ragelink 45 'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D', 'Е': 'E', 'Ё': 'Yo',
afe42d0… ragelink 46 'Ж': 'Zh', 'З': 'Z', 'И': 'I', 'Й': 'J', 'К': 'K', 'Л': 'L', 'М': 'M',
afe42d0… ragelink 47 'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T', 'У': 'U',
afe42d0… ragelink 48 'Ф': 'F', 'Х': 'H', 'Ц': 'C', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Sh', 'Ъ': '',
afe42d0… ragelink 49 'Ы': 'Y', 'Ь': '', 'Э': 'E', 'Ю': 'Yu', 'Я': 'Ya'
afe42d0… ragelink 50 };
afe42d0… ragelink 51 const UKRAINIAN_MAP = {
afe42d0… ragelink 52 'Є': 'Ye', 'І': 'I', 'Ї': 'Yi', 'Ґ': 'G', 'є': 'ye', 'і': 'i',
afe42d0… ragelink 53 'ї': 'yi', 'ґ': 'g'
afe42d0… ragelink 54 };
afe42d0… ragelink 55 const CZECH_MAP = {
afe42d0… ragelink 56 'č': 'c', 'ď': 'd', 'ě': 'e', 'ň': 'n', 'ř': 'r', 'š': 's', 'ť': 't',
afe42d0… ragelink 57 'ů': 'u', 'ž': 'z', 'Č': 'C', 'Ď': 'D', 'Ě': 'E', 'Ň': 'N', 'Ř': 'R',
afe42d0… ragelink 58 'Š': 'S', 'Ť': 'T', 'Ů': 'U', 'Ž': 'Z'
afe42d0… ragelink 59 };
afe42d0… ragelink 60 const SLOVAK_MAP = {
afe42d0… ragelink 61 'á': 'a', 'ä': 'a', 'č': 'c', 'ď': 'd', 'é': 'e', 'í': 'i', 'ľ': 'l',
afe42d0… ragelink 62 'ĺ': 'l', 'ň': 'n', 'ó': 'o', 'ô': 'o', 'ŕ': 'r', 'š': 's', 'ť': 't',
afe42d0… ragelink 63 'ú': 'u', 'ý': 'y', 'ž': 'z',
afe42d0… ragelink 64 'Á': 'a', 'Ä': 'A', 'Č': 'C', 'Ď': 'D', 'É': 'E', 'Í': 'I', 'Ľ': 'L',
afe42d0… ragelink 65 'Ĺ': 'L', 'Ň': 'N', 'Ó': 'O', 'Ô': 'O', 'Ŕ': 'R', 'Š': 'S', 'Ť': 'T',
afe42d0… ragelink 66 'Ú': 'U', 'Ý': 'Y', 'Ž': 'Z'
afe42d0… ragelink 67 };
afe42d0… ragelink 68 const POLISH_MAP = {
afe42d0… ragelink 69 'ą': 'a', 'ć': 'c', 'ę': 'e', 'ł': 'l', 'ń': 'n', 'ó': 'o', 'ś': 's',
afe42d0… ragelink 70 'ź': 'z', 'ż': 'z',
afe42d0… ragelink 71 'Ą': 'A', 'Ć': 'C', 'Ę': 'E', 'Ł': 'L', 'Ń': 'N', 'Ó': 'O', 'Ś': 'S',
afe42d0… ragelink 72 'Ź': 'Z', 'Ż': 'Z'
afe42d0… ragelink 73 };
afe42d0… ragelink 74 const LATVIAN_MAP = {
afe42d0… ragelink 75 'ā': 'a', 'č': 'c', 'ē': 'e', 'ģ': 'g', 'ī': 'i', 'ķ': 'k', 'ļ': 'l',
afe42d0… ragelink 76 'ņ': 'n', 'š': 's', 'ū': 'u', 'ž': 'z',
afe42d0… ragelink 77 'Ā': 'A', 'Č': 'C', 'Ē': 'E', 'Ģ': 'G', 'Ī': 'I', 'Ķ': 'K', 'Ļ': 'L',
afe42d0… ragelink 78 'Ņ': 'N', 'Š': 'S', 'Ū': 'U', 'Ž': 'Z'
afe42d0… ragelink 79 };
afe42d0… ragelink 80 const ARABIC_MAP = {
afe42d0… ragelink 81 'أ': 'a', 'ب': 'b', 'ت': 't', 'ث': 'th', 'ج': 'g', 'ح': 'h', 'خ': 'kh', 'د': 'd',
afe42d0… ragelink 82 'ذ': 'th', 'ر': 'r', 'ز': 'z', 'س': 's', 'ش': 'sh', 'ص': 's', 'ض': 'd', 'ط': 't',
afe42d0… ragelink 83 'ظ': 'th', 'ع': 'aa', 'غ': 'gh', 'ف': 'f', 'ق': 'k', 'ك': 'k', 'ل': 'l', 'م': 'm',
afe42d0… ragelink 84 'ن': 'n', 'ه': 'h', 'و': 'o', 'ي': 'y'
afe42d0… ragelink 85 };
afe42d0… ragelink 86 const LITHUANIAN_MAP = {
afe42d0… ragelink 87 'ą': 'a', 'č': 'c', 'ę': 'e', 'ė': 'e', 'į': 'i', 'š': 's', 'ų': 'u',
afe42d0… ragelink 88 'ū': 'u', 'ž': 'z',
afe42d0… ragelink 89 'Ą': 'A', 'Č': 'C', 'Ę': 'E', 'Ė': 'E', 'Į': 'I', 'Š': 'S', 'Ų': 'U',
afe42d0… ragelink 90 'Ū': 'U', 'Ž': 'Z'
afe42d0… ragelink 91 };
afe42d0… ragelink 92 const SERBIAN_MAP = {
afe42d0… ragelink 93 'ђ': 'dj', 'ј': 'j', 'љ': 'lj', 'њ': 'nj', 'ћ': 'c', 'џ': 'dz',
afe42d0… ragelink 94 'đ': 'dj', 'Ђ': 'Dj', 'Ј': 'j', 'Љ': 'Lj', 'Њ': 'Nj', 'Ћ': 'C',
afe42d0… ragelink 95 'Џ': 'Dz', 'Đ': 'Dj'
afe42d0… ragelink 96 };
afe42d0… ragelink 97 const AZERBAIJANI_MAP = {
afe42d0… ragelink 98 'ç': 'c', 'ə': 'e', 'ğ': 'g', 'ı': 'i', 'ö': 'o', 'ş': 's', 'ü': 'u',
afe42d0… ragelink 99 'Ç': 'C', 'Ə': 'E', 'Ğ': 'G', 'İ': 'I', 'Ö': 'O', 'Ş': 'S', 'Ü': 'U'
afe42d0… ragelink 100 };
afe42d0… ragelink 101 const GEORGIAN_MAP = {
afe42d0… ragelink 102 'ა': 'a', 'ბ': 'b', 'გ': 'g', 'დ': 'd', 'ე': 'e', 'ვ': 'v', 'ზ': 'z',
afe42d0… ragelink 103 'თ': 't', 'ი': 'i', 'კ': 'k', 'ლ': 'l', 'მ': 'm', 'ნ': 'n', 'ო': 'o',
afe42d0… ragelink 104 'პ': 'p', 'ჟ': 'j', 'რ': 'r', 'ს': 's', 'ტ': 't', 'უ': 'u', 'ფ': 'f',
afe42d0… ragelink 105 'ქ': 'q', 'ღ': 'g', 'ყ': 'y', 'შ': 'sh', 'ჩ': 'ch', 'ც': 'c', 'ძ': 'dz',
afe42d0… ragelink 106 'წ': 'w', 'ჭ': 'ch', 'ხ': 'x', 'ჯ': 'j', 'ჰ': 'h'
afe42d0… ragelink 107 };
afe42d0… ragelink 108
afe42d0… ragelink 109 const ALL_DOWNCODE_MAPS = [
afe42d0… ragelink 110 LATIN_MAP,
afe42d0… ragelink 111 LATIN_SYMBOLS_MAP,
afe42d0… ragelink 112 GREEK_MAP,
afe42d0… ragelink 113 TURKISH_MAP,
afe42d0… ragelink 114 ROMANIAN_MAP,
afe42d0… ragelink 115 RUSSIAN_MAP,
afe42d0… ragelink 116 UKRAINIAN_MAP,
afe42d0… ragelink 117 CZECH_MAP,
afe42d0… ragelink 118 SLOVAK_MAP,
afe42d0… ragelink 119 POLISH_MAP,
afe42d0… ragelink 120 LATVIAN_MAP,
afe42d0… ragelink 121 ARABIC_MAP,
afe42d0… ragelink 122 LITHUANIAN_MAP,
afe42d0… ragelink 123 SERBIAN_MAP,
afe42d0… ragelink 124 AZERBAIJANI_MAP,
afe42d0… ragelink 125 GEORGIAN_MAP
afe42d0… ragelink 126 ];
afe42d0… ragelink 127
afe42d0… ragelink 128 const Downcoder = {
afe42d0… ragelink 129 'Initialize': function() {
afe42d0… ragelink 130 if (Downcoder.map) { // already made
afe42d0… ragelink 131 return;
afe42d0… ragelink 132 }
afe42d0… ragelink 133 Downcoder.map = {};
afe42d0… ragelink 134 for (const lookup of ALL_DOWNCODE_MAPS) {
afe42d0… ragelink 135 Object.assign(Downcoder.map, lookup);
afe42d0… ragelink 136 }
afe42d0… ragelink 137 Downcoder.regex = new RegExp(Object.keys(Downcoder.map).join('|'), 'g');
afe42d0… ragelink 138 }
afe42d0… ragelink 139 };
afe42d0… ragelink 140
afe42d0… ragelink 141 function downcode(slug) {
afe42d0… ragelink 142 Downcoder.Initialize();
afe42d0… ragelink 143 return slug.replace(Downcoder.regex, function(m) {
afe42d0… ragelink 144 return Downcoder.map[m];
afe42d0… ragelink 145 });
afe42d0… ragelink 146 }
afe42d0… ragelink 147
afe42d0… ragelink 148
afe42d0… ragelink 149 function URLify(s, num_chars, allowUnicode) {
afe42d0… ragelink 150 // changes, e.g., "Petty theft" to "petty-theft"
afe42d0… ragelink 151 if (!allowUnicode) {
afe42d0… ragelink 152 s = downcode(s);
afe42d0… ragelink 153 }
afe42d0… ragelink 154 s = s.toLowerCase(); // convert to lowercase
afe42d0… ragelink 155 // if downcode doesn't hit, the char will be stripped here
afe42d0… ragelink 156 if (allowUnicode) {
afe42d0… ragelink 157 // Keep Unicode letters including both lowercase and uppercase
afe42d0… ragelink 158 // characters, whitespace, and dash; remove other characters.
afe42d0… ragelink 159 s = XRegExp.replace(s, XRegExp('[^-_\\p{L}\\p{N}\\s]', 'g'), '');
afe42d0… ragelink 160 } else {
afe42d0… ragelink 161 s = s.replace(/[^-\w\s]/g, ''); // remove unneeded chars
afe42d0… ragelink 162 }
afe42d0… ragelink 163 s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
afe42d0… ragelink 164 s = s.replace(/[-\s]+/g, '-'); // convert spaces to hyphens
afe42d0… ragelink 165 s = s.substring(0, num_chars); // trim to first num_chars chars
afe42d0… ragelink 166 return s.replace(/-+$/g, ''); // trim any trailing hyphens
afe42d0… ragelink 167 }
afe42d0… ragelink 168 window.URLify = URLify;
afe42d0… ragelink 169 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button