Last active
August 29, 2015 14:02
-
-
Save tondol/c9a308285a6eb30746b3 to your computer and use it in GitHub Desktop.
Twitter APIが返すJSONのlinkify処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function h(s) { | |
var map = { | |
'<': '<', | |
'>': '>', | |
'&': '&', | |
'\'' :''', | |
'"' :'"' | |
}; | |
function f(s) { return map[s]; } | |
return s.replace(/<|>|&|'|"/g, f); | |
} | |
// https://gist.github.com/xendoc/4129696 | |
function strlen(str) { | |
var ret = 0; | |
for (var i = 0; i < str.length; i++,ret++) { | |
var upper = str.charCodeAt(i); | |
var lower = str.length > (i + 1) ? str.charCodeAt(i + 1) : 0; | |
if (isSurrogatePair(upper, lower)) { | |
i++; | |
} | |
} | |
return ret; | |
} | |
function substring(str, begin, end) { | |
var ret = ''; | |
for (var i = 0, len = 0; i < str.length; i++, len++) { | |
var upper = str.charCodeAt(i); | |
var lower = str.length > (i + 1) ? str.charCodeAt(i + 1) : 0; | |
var s = ""; | |
if(isSurrogatePair(upper, lower)) { | |
i++; | |
s = String.fromCharCode(upper, lower); | |
} else { | |
s = String.fromCharCode(upper); | |
} | |
if (begin <= len && len < end) { | |
ret += s; | |
} | |
} | |
return ret; | |
} | |
function isSurrogatePair(upper, lower) { | |
return 0xD800 <= upper && upper <= 0xDBFF && 0xDC00 <= lower && lower <= 0xDFFF; | |
} | |
// https://gist.github.com/wadey/442463 | |
/* | |
* twitter-entities.js | |
* This function converts a tweet with "entity" metadata | |
* from plain text to linkified HTML. | |
* | |
* See the documentation here: http://dev.twitter.com/pages/tweet_entities | |
* Basically, add ?include_entities=true to your timeline call | |
* | |
* Copyright 2010, Wade Simmons | |
* Licensed under the MIT license | |
* http://wades.im/mons | |
* | |
* Requires jQuery | |
*/ | |
function linkify(o) { | |
var s = o['text']; | |
var map = {}; | |
$.each(o['entities']['urls'], function (i, entry) { | |
map[entry.indices[0]] = [entry.indices[1], function (s) { | |
return "<a href=\""+ h(entry.expanded_url) + "\">" + h(entry.display_url) + "</a>"; | |
}]; | |
}); | |
$.each(o['entities']['user_mentions'], function (i, entry) { | |
map[entry.indices[0]] = [entry.indices[1], function (s) { | |
return "<a href=\"https://twitter.com/" + h(entry.screen_name) + "\">@" + h(entry.screen_name) + "</a>"; | |
}]; | |
}); | |
$.each(o['entities']['hashtags'], function (i, entry) { | |
map[entry.indices[0]] = [entry.indices[1], function (s) { | |
return "<a href=\"https://twitter.com/search?q=#" + h(entry.text) + "\">#" + h(entry.text) + "</a>"; | |
}]; | |
}); | |
if ('media' in o['entities']) { | |
$.each(o['entities']['media'], function (i, entry) { | |
map[entry.indices[0]] = [entry.indices[1], function (s) { | |
return "<a href=\""+ h(entry.expanded_url) + "\">" + h(entry.display_url) + "</a>"; | |
}]; | |
}); | |
} | |
var i = 0, last = 0; | |
var result = ''; | |
for (var i=0;i<strlen(s);i++) { | |
var index = map[i]; | |
if (index != undefined) { | |
var end = index[0]; | |
var f = index[1]; | |
if (i != last) { | |
result += h(substring(s, last, i)); | |
} | |
result += f(substring(s, i, end)); | |
i = end - 1; | |
last = end; | |
} | |
} | |
if (i != last) { | |
result += h(substring(s, last, i)); | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function h($value) | |
{ | |
if (is_array($value)) { | |
foreach ($value as $k => $v) { | |
$value[$k] = htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); | |
} | |
return $value; | |
} else { | |
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); | |
} | |
} | |
// https://gist.github.com/wadey/442463 | |
/* | |
* twitter-entities.js | |
* This function converts a tweet with "entity" metadata | |
* from plain text to linkified HTML. | |
* | |
* See the documentation here: http://dev.twitter.com/pages/tweet_entities | |
* Basically, add ?include_entities=true to your timeline call | |
* | |
* Copyright 2010, Wade Simmons | |
* Licensed under the MIT license | |
* http://wades.im/mons | |
* | |
* Requires jQuery | |
*/ | |
function linkify($o) | |
{ | |
$s = $o->text; | |
$map = array(); | |
foreach ($o->entities->urls as $i => $entry) { | |
$map[$entry->indices[0]] = [$entry->indices[1], function ($s) use ($entry) { | |
return "<a href=\"" . h($entry->expanded_url) . "\">" . h($entry->display_url) . "</a>"; | |
}]; | |
} | |
foreach ($o->entities->user_mentions as $i => $entry) { | |
$map[$entry->indices[0]] = [$entry->indices[1], function ($s) use ($entry) { | |
return "<a href=\"" . $this->get_uri('user', array('screen_name' => h($entry->screen_name))) . "\">@" . h($entry->screen_name) . "</a>"; | |
}]; | |
} | |
foreach ($o->entities->hashtags as $i => $entry) { | |
$map[$entry->indices[0]] = [$entry->indices[1], function ($s) use ($entry) { | |
return "<a href=\"" . $this->get_uri('search', array('q' => "#" . h($entry->text))) . "\">#" . h($entry->text) . "</a>"; | |
}]; | |
} | |
if (!empty($o->entities->media)) { | |
foreach ($o->entities->media as $i => $entry) { | |
$map[$entry->indices[0]] = [$entry->indices[1], function ($s) use ($entry) { | |
return "<a href=\"" . h($entry->expanded_url) . "\">" . h($entry->display_url) . "</a>"; | |
}]; | |
} | |
} | |
$i = 0; $last = 0; | |
$result = ''; | |
for ($i=0;$i<mb_strlen($s, 'UTF-8');$i++) { | |
if (!empty($map[$i])) { | |
$index = $map[$i]; | |
$end = $index[0]; | |
$f = $index[1]; | |
if ($i != $last) { | |
$result .= h(mb_substr($s, $last, $i - $last, 'UTF-8')); | |
} | |
$result .= $f(mb_substr($s, $i, $end - $i, 'UTF-8')); | |
$i = $end - 1; | |
$last = $end; | |
} | |
} | |
if ($i != $last) { | |
$result .= h(mb_substr($s, $last, $i - $last, 'UTF-8')); | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment