aboutsummaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorfriendica <info@friendica.com>2012-07-30 18:51:44 -0700
committerfriendica <info@friendica.com>2012-07-30 18:51:44 -0700
commit02cc436ec7cf0cfca5fc9b5166cc537bbee53671 (patch)
treed7e5eea4e22ed1befeab5706a30ce1e1ac064972 /library
parent194c52aee590a209cc38ee8fb322ff7dc4a9143d (diff)
downloadvolse-hubzilla-02cc436ec7cf0cfca5fc9b5166cc537bbee53671.tar.gz
volse-hubzilla-02cc436ec7cf0cfca5fc9b5166cc537bbee53671.tar.bz2
volse-hubzilla-02cc436ec7cf0cfca5fc9b5166cc537bbee53671.zip
Added urlify to try and create webbie auto-suggestions out of whatever unicode stuff gets thrown in as a name. Currently this will only work for latin/european/cyrillic/russian, but possible to extend to ideographic forms.
Diffstat (limited to 'library')
-rw-r--r--library/urlify/.gitignore3
-rw-r--r--library/urlify/INSTALL10
-rw-r--r--library/urlify/README.md70
-rw-r--r--library/urlify/URLify.php188
-rw-r--r--library/urlify/composer.json21
-rw-r--r--library/urlify/tests/URLifyTest.php31
-rw-r--r--library/urlify/tests/bootstrap.php9
-rw-r--r--library/urlify/tests/phpunit.xml8
8 files changed, 340 insertions, 0 deletions
diff --git a/library/urlify/.gitignore b/library/urlify/.gitignore
new file mode 100644
index 000000000..9df9b994a
--- /dev/null
+++ b/library/urlify/.gitignore
@@ -0,0 +1,3 @@
+vendor
+composer.phar
+composer.lock
diff --git a/library/urlify/INSTALL b/library/urlify/INSTALL
new file mode 100644
index 000000000..e92e55e11
--- /dev/null
+++ b/library/urlify/INSTALL
@@ -0,0 +1,10 @@
+To install URLify, you can add it as a dependency ar by downloading the composer.phar executable.
+
+$ curl -s http://getcomposer.org/installer | php
+
+and run install
+
+$ php composer.phar install
+
+For more details, see http://getcomposer.org.
+
diff --git a/library/urlify/README.md b/library/urlify/README.md
new file mode 100644
index 000000000..7c2c42a12
--- /dev/null
+++ b/library/urlify/README.md
@@ -0,0 +1,70 @@
+# URLify for PHP
+
+A PHP port of [URLify.js](https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js)
+from the Django project. Handles symbols from Latin languages, Greek, Turkish,
+Russian, Ukrainian, Czech, Polish, and Latvian. Symbols it cannot transliterate
+it will simply omit.
+
+* Author: [jbroadway](http://github.com/jbroadway)
+* License: MIT
+
+## Usage:
+
+To generate slugs for URLs:
+
+```php
+<?php
+
+echo URLify::filter (' J\'étudie le français ');
+// "jetudie-le-francais"
+
+echo URLify::filter ('Lo siento, no hablo español.');
+// "lo-siento-no-hablo-espanol"
+
+?>
+```
+
+To simply transliterate characters:
+
+```php
+<?php
+
+echo URLify::downcode ('J\'étudie le français');
+// "J'etudie le francais"
+
+echo URLify::downcode ('Lo siento, no hablo español.');
+// "Lo siento, no hablo espanol."
+
+/* Or use transliterate() alias: */
+
+echo URLify::transliterate ('Lo siento, no hablo español.');
+// "Lo siento, no hablo espanol."
+
+?>
+```
+
+To extend the character list:
+
+```php
+<?php
+
+URLify::add_chars (array (
+ '¿' => '?', '®' => '(r)', '¼' => '1/4',
+ '¼' => '1/2', '¾' => '3/4', '¶' => 'P'
+));
+
+echo URLify::downcode ('¿ ® ¼ ¼ ¾ ¶');
+// "? (r) 1/2 1/2 3/4 P"
+
+?>
+```
+
+To extend the list of words to remove:
+
+```php
+<?php
+
+URLify::remove_words (array ('remove', 'these', 'too'));
+
+?>
+```
diff --git a/library/urlify/URLify.php b/library/urlify/URLify.php
new file mode 100644
index 000000000..1337f9a91
--- /dev/null
+++ b/library/urlify/URLify.php
@@ -0,0 +1,188 @@
+<?php
+
+/**
+ * A PHP port of URLify.js from the Django project
+ * (https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js).
+ * Handles symbols from Latin languages, Greek, Turkish, Russian, Ukrainian,
+ * Czech, Polish, and Latvian. Symbols it cannot transliterate
+ * it will simply omit.
+ *
+ * Usage:
+ *
+ * echo URLify::filter (' J\'étudie le français ');
+ * // "jetudie-le-francais"
+ *
+ * echo URLify::filter ('Lo siento, no hablo español.');
+ * // "lo-siento-no-hablo-espanol"
+ */
+class URLify {
+ public static $maps = array (
+ 'latin_map' => array (
+ 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' =>
+ 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
+ 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' =>
+ 'O', 'Ő' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
+ 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' =>
+ 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
+ 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
+ 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u',
+ 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
+ ),
+ 'latin_symbols_map' => array (
+ '©' => '(c)'
+ ),
+ 'greek_map' => array (
+ 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
+ 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
+ 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
+ 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
+ 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
+ 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
+ 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
+ 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
+ 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
+ 'Ϋ' => 'Y'
+ ),
+ 'turkish_map' => array (
+ 'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ü' => 'u', 'Ü' => 'U',
+ 'ö' => 'o', 'Ö' => 'O', 'ğ' => 'g', 'Ğ' => 'G'
+ ),
+ 'russian_map' => array (
+ 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
+ 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
+ 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
+ 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
+ 'я' => 'ya',
+ 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
+ 'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
+ 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
+ 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
+ 'Я' => 'Ya'
+ ),
+ 'ukrainian_map' => array (
+ 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
+ ),
+ 'czech_map' => array (
+ 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
+ 'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
+ 'Ů' => 'U', 'Ž' => 'Z'
+ ),
+ 'polish_map' => array (
+ 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
+ 'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
+ 'Ź' => 'Z', 'Ż' => 'Z'
+ ),
+ 'latvian_map' => array (
+ 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
+ 'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i',
+ 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z'
+ )
+ );
+
+ /**
+ * List of words to remove from URLs.
+ */
+ public static $remove_list = array (
+ 'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
+ 'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
+ 'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
+ 'with'
+ );
+
+ /**
+ * The character map.
+ */
+ private static $map = array ();
+
+ /**
+ * The character list as a string.
+ */
+ private static $chars = '';
+
+ /**
+ * The character list as a regular expression.
+ */
+ private static $regex = '';
+
+ /**
+ * Initializes the character map.
+ */
+ private static function init () {
+ if (count (self::$map) > 0) {
+ return;
+ }
+
+ foreach (self::$maps as $map) {
+ foreach ($map as $orig => $conv) {
+ self::$map[$orig] = $conv;
+ self::$chars .= $orig;
+ }
+ }
+
+ self::$regex = '/[' . self::$chars . ']/u';
+ }
+
+ /**
+ * Add new characters to the list. `$map` should be a hash.
+ */
+ public static function add_chars ($map) {
+ if (! is_array ($map)) {
+ throw new LogicException ('$map must be an associative array.');
+ }
+ self::$maps[] = $map;
+ self::$map = array ();
+ self::$chars = '';
+ }
+
+ /**
+ * Append words to the remove list. Accepts either single words
+ * or an array of words.
+ */
+ public static function remove_words ($words) {
+ $words = is_array ($words) ? $words : array ($words);
+ self::$remove_list = array_merge (self::$remove_list, $words);
+ }
+
+ /**
+ * Transliterates characters to their ASCII equivalents.
+ */
+ public static function downcode ($text) {
+ self::init ();
+
+ if (preg_match_all (self::$regex, $text, $matches)) {
+ for ($i = 0; $i < count ($matches[0]); $i++) {
+ $char = $matches[0][$i];
+ if (isset (self::$map[$char])) {
+ $text = str_replace ($char, self::$map[$char], $text);
+ }
+ }
+ }
+ return $text;
+ }
+
+ /**
+ * Filters a string, e.g., "Petty theft" to "petty-theft"
+ */
+ public static function filter ($text, $length = 60) {
+ $text = self::downcode ($text);
+
+ // remove all these words from the string before urlifying
+ $text = preg_replace ('/\b(' . join ('|', self::$remove_list) . ')\b/i', '', $text);
+
+ // if downcode doesn't hit, the char will be stripped here
+ $text = preg_replace ('/[^-\w\s]/', '', $text); // remove unneeded chars
+ $text = preg_replace ('/^\s+|\s+$/', '', $text); // trim leading/trailing spaces
+ $text = preg_replace ('/[-\s]+/', '-', $text); // convert spaces to hyphens
+ $text = strtolower ($text); // convert to lowercase
+ return trim (substr ($text, 0, $length), '-'); // trim to first $length chars
+ }
+
+ /**
+ * Alias of `URLify::downcode()`.
+ */
+ public static function transliterate ($text) {
+ return self::downcode ($text);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/library/urlify/composer.json b/library/urlify/composer.json
new file mode 100644
index 000000000..2d95f6aa6
--- /dev/null
+++ b/library/urlify/composer.json
@@ -0,0 +1,21 @@
+{
+ "name": "jbroadway/urlify",
+ "type": "library",
+ "description": "PHP port of URLify.js from the Django project. Transliterates non-ascii characters for use in URLs.",
+ "keywords": ["urlify","transliterate","translit","transliteration","url","encode","slug","link","iconv"],
+ "homepage": "https://github.com/jbroadway/urlify",
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "Johnny Broadway",
+ "email": "johnny@johnnybroadway.com",
+ "homepage": "http://www.johnnybroadway.com/"
+ }
+ ],
+ "require": {
+ "php": ">=5.3.0"
+ },
+ "autoload": {
+ "psr-0": { "URLify": "" }
+ }
+}
diff --git a/library/urlify/tests/URLifyTest.php b/library/urlify/tests/URLifyTest.php
new file mode 100644
index 000000000..26ecb5dea
--- /dev/null
+++ b/library/urlify/tests/URLifyTest.php
@@ -0,0 +1,31 @@
+<?php
+class URLifyTest extends PHPUnit_Framework_TestCase {
+ function test_downcode () {
+ $this->assertEquals (' J\'etudie le francais ', URLify::downcode (' J\'étudie le français '));
+ $this->assertEquals ('Lo siento, no hablo espanol.', URLify::downcode ('Lo siento, no hablo español.'));
+ $this->assertEquals ('F3PWS', URLify::downcode ('ΦΞΠΏΣ'));
+ }
+
+ function test_filter () {
+ $this->assertEquals ('jetudie-le-francais', URLify::filter (' J\'étudie le français '));
+ $this->assertEquals ('lo-siento-no-hablo-espanol', URLify::filter ('Lo siento, no hablo español.'));
+ $this->assertEquals ('f3pws', URLify::filter ('ΦΞΠΏΣ'));
+ }
+
+ function test_add_chars () {
+ $this->assertEquals ('¿ ® ¼ ¼ ¾ ¶', URLify::downcode ('¿ ® ¼ ¼ ¾ ¶'));
+ URLify::add_chars (array (
+ '¿' => '?', '®' => '(r)', '¼' => '1/4',
+ '¼' => '1/2', '¾' => '3/4', '¶' => 'P'
+ ));
+ $this->assertEquals ('? (r) 1/2 1/2 3/4 P', URLify::downcode ('¿ ® ¼ ¼ ¾ ¶'));
+ }
+
+ function test_remove_words () {
+ $this->assertEquals ('foo-bar', URLify::filter ('foo bar'));
+ URLify::remove_words (array ('foo', 'bar'));
+ $this->assertEquals ('', URLify::filter ('foo bar'));
+ }
+}
+
+?>
diff --git a/library/urlify/tests/bootstrap.php b/library/urlify/tests/bootstrap.php
new file mode 100644
index 000000000..d56d46665
--- /dev/null
+++ b/library/urlify/tests/bootstrap.php
@@ -0,0 +1,9 @@
+<?php
+set_error_handler(function () {
+ echo file_get_contents(dirname(__DIR__).'/INSTALL');
+ exit(1);
+}, E_ALL);
+
+require_once dirname(__DIR__) . '/vendor/autoload.php';
+
+restore_error_handler();
diff --git a/library/urlify/tests/phpunit.xml b/library/urlify/tests/phpunit.xml
new file mode 100644
index 000000000..2666a3ccb
--- /dev/null
+++ b/library/urlify/tests/phpunit.xml
@@ -0,0 +1,8 @@
+<phpunit bootstrap="bootstrap.php">
+ <testsuite name="URLify Test Suite">
+ <directory>.</directory>
+ </testsuite>
+ <logging>
+ <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
+ </logging>
+</phpunit>