Created
January 12, 2016 16:09
-
-
Save kgaughan/7e12cae0590e94162284 to your computer and use it in GitHub Desktop.
AFK event example
This file contains hidden or 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
<?php | |
function format($text) { | |
return str_replace( | |
array('<pre><code>', '<hr />', '<br />'), | |
array('<pre class="prettyprint"><code>', '<div class="hr"><hr></div>', '<br>'), | |
SmartyPants(Markdown(render_tags($text)))); | |
} | |
function render_tags($text) { | |
return preg_replace_callback( | |
"/{{(?<tag>[a-z0-9]+)(?<attrs>(?:\s+[a-z0-9]+=\"[^\"]+\")*)}}/", | |
'render_tag', $text); | |
} | |
function parse_attrs($text) { | |
$attrs = array(); | |
$matches = array(); | |
preg_match_all("/\s+(?<key>[a-z0-9]+)=\"(?<value>[^\"]+)\"/", $text, $matches, PREG_SET_ORDER); | |
foreach ($matches as $m) { | |
$attrs[$m['key']] = $m['value']; | |
} | |
return $attrs; | |
} | |
function render_tag(array $matches) { | |
list($unhandled, $result) = trigger_event( | |
'tag:' . $matches['tag'], | |
parse_attrs($matches['attrs'])); | |
return $unhandled ? $matches[0] : $result; | |
} | |
function format_line($text) { | |
return SmartyPants(e($text)); | |
} | |
function google_fonts() { | |
$fonts = func_get_args(); | |
foreach ($fonts as $font) { | |
printf('<link href="http://fonts.googleapis.com/css?family=%s" rel="stylesheet" type="text/css">', | |
htmlentities(urlencode($font))); | |
} | |
} | |
function is_naked_day($now) { | |
$y = gmdate('Y', $now); | |
// The 7th is our fallback if neither the 5th or 9th work. | |
foreach (array(5, 9, 7) as $try) { | |
$day_of_week = gmdate('N', gmmktime(0, 0, 0, 4, $try, $y)); | |
// Best if it's a Tuesday, Wednesday, or Thursday. | |
if ($day_of_week >= 2 && $day_of_week <= 4) { | |
$d = $try; | |
break; | |
} | |
} | |
$start = gmmktime(-12, 0, 0, 4, $d, $y); | |
$end = gmmktime(36, 0, 0, 4, $d, $y); | |
return $now >= $start && $now <= $end; | |
} | |
function generate_link_embed($link) { | |
list($unhandled, $result) = trigger_event('render_link', $link); | |
return $unhandled ? false : $result; | |
} | |
function ts($fmt, $ts) { | |
return gmdate($fmt, is_int($ts) ? $ts : strtotime($ts)); | |
} | |
function dbts($ts) { | |
return ts('Y-m-d H:i:s', $ts); | |
} | |
function get_via_host($url) { | |
$host = parse_url($url, PHP_URL_HOST); | |
if (substr($host, 0, 4) == 'www.') { | |
$host = substr($host, 4); | |
} | |
return $host; | |
} |
This file contains hidden or 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
<?php | |
class FlashEmbedPlugin extends Plugin { | |
protected function get_events() { | |
return array( | |
'render_link', | |
'tag:flash' => 'render_flash_tag', | |
'tag:movie' => 'render_movie_tag'); | |
} | |
public function render_link($msg, $url) { | |
$embed = $this->generate_movie_embed($url); | |
return array($embed === false, $embed === false ? $url : $embed); | |
} | |
public function render_flash_tag($msg, array $attrs) { | |
if (!isset($attrs['href'])) { | |
return array(false, ''); | |
} | |
$url = $attrs['href']; | |
$width = isset($attrs['width']) ? $attrs['width'] : 640; | |
$height = isset($attrs['height']) ? $attrs['height'] : 385; | |
unset($attrs['href'], $attrs['width'], $attrs['height']); | |
return array(false, $this->generate_flash_embed($url, $width, $height, $attrs)); | |
} | |
public function render_movie_tag($msg, array $attrs) { | |
if (!isset($attrs['href'])) { | |
return array(false, ''); | |
} | |
$url = $attrs['href']; | |
$width = isset($attrs['width']) ? $attrs['width'] : null; | |
$height = isset($attrs['height']) ? $attrs['height'] : null; | |
$embed = $this->generate_movie_embed($url, $width, $height); | |
return array($embed === false, $embed === false ? $attrs : $embed); | |
} | |
private function generate_flash_embed($movie_url, $width, $height, array $params) { | |
$params_html = '<param name="movie" value="' . e($movie_url) . '">'; | |
if (count($params) > 0) { | |
$params_html .= '<params name="FlashVars" value="' . e(http_build_query($params)) . '">'; | |
foreach ($params as $name => $value) { | |
$params_html .= sprintf('<param name="%s" value="%s">', e($name), e($value)); | |
} | |
} | |
return sprintf( | |
'<object type="application/x-shockwave-flash" data="%s" width="%s" height="%s">%s</object>', | |
e($movie_url), $width, $height, $params_html); | |
} | |
private function generate_movie_embed($link, $width=null, $height=null) { | |
static $patterns = array( | |
'https?://(?:www\.)?youtube\.com/watch\?v=([-A-Za-z0-9_]{11,})' => array( | |
'pattern' => "//www.youtube.com/v/%s&rel=1", | |
'width' => 640, | |
'height' => 385), | |
'https?://video\.google\.com/videoplay\?docid=(-?\d+)' => array( | |
'pattern' => "//video.google.com/googleplayer.swf?hl=en&docId=%s", | |
'width' => 640, | |
'height' => 385), | |
'https?://(?:www\.)?vimeo\.com/(\d+)' => array( | |
'pattern' => "//www.vimeo.com/moogaloop.swf?clip_id=%s", | |
'width' => 640, | |
'height' => 385), | |
'https?://(?:www\.)?gametrailers\.com/video/[^/]+/(\d+)' => array( | |
'pattern' => "//www.gametrailers.com/remote_wrap.php?mid=%s", | |
'width' => 640, | |
'height' => 385), | |
'https?://(?:www\.)?dailymotion\.com/video/([a-z0-9]+)' => array( | |
'pattern' => "//www.dailymotion.com/swf/%s", | |
'width' => 640, | |
'height' => 385)); | |
foreach ($patterns as $pattern => $params) { | |
if (preg_match("~^$pattern~", $link, $matches)) { | |
return $this->generate_flash_embed( | |
sprintf($params['pattern'], $matches[1]), | |
AFK::coalesce($width, $params['width']), | |
AFK::coalesce($height, $params['height']), | |
array()); | |
} | |
} | |
return false; | |
} | |
} | |
// Instantiate an instance of this particular plugin. | |
new FlashEmbedPlugin(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment