*/ // must be run within Dokuwiki if (!defined('DOKU_INC')) die(); class syntax_plugin_tumblr extends DokuWiki_Syntax_Plugin { function getInfo() { return array( 'author' => 'Lee, Kwangyoung', 'email' => 'ipari@leaflette.com', 'date' => '2011-04-04', 'name' => 'Tumblr Plugin', 'desc' => 'Embed tumblr into DokuWiki', 'url' => 'http://www.dokuwiki.org/plugin:tumblr' ); } /** * @return string Syntax mode type */ function getType() { return 'substition'; } /** * @return string Paragraph type */ function getPType() { return 'normal'; } /** * https://www.dokuwiki.org/devel:parser:getsort_list * * @return int Sort order - Low numbers go before high numbers */ function getSort() { return 315; } /** * Connect lookup pattern to lexer. * * @param string $mode Parser mode */ function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{tumblr>[^}]*\}\}',$mode,'plugin_tumblr'); } // function postConnect() { // $this->Lexer->addExitPattern('','plugin_tumblr'); // } /** * Handle matches of the tumblr syntax * * @param string $match The match of the syntax * @param int $state The state of the handler * @param int $pos The position in the document * @param Doku_Handler $handler The handler * @return array Data for the renderer */ function handle($match, $state, $pos, Doku_Handler &$handler){ $match = substr($match,9,-2); //strip '{{tumblr>' and '}}' $params = explode('&',$match); $data = array(); foreach($params as $param) { $param = explode('=',$param); $data[$param[0]] = $param[1]; } return $data; } /** * Render xhtml output or metadata * * @param string $mode Renderer mode (supported modes: xhtml) * @param Doku_Renderer $renderer The renderer * @param array $data The data from the handler() function * @return bool If rendering was successful. */ function render($mode, Doku_Renderer &$renderer, $data) { if($mode != 'xhtml') return false; // prevent caching to show lastest posts $renderer->info['cache'] = false; $renderer->doc .= $this->tumblr($data); return true; } public $tumblr_url = false; function load_rss($url) { global $conf; require_once(DOKU_INC.'inc/FeedParser.php'); $feed = new FeedParser(); $feed->set_feed_url($url); $rc = $feed->init(); if (!$rc) { return false; } $posts = array(); foreach($feed->get_items() as $item) { $posts[] = array( 'title' => $item->get_title(), 'date' => $item->get_local_date($conf['dformat']), 'permalink' => $item->get_permalink(), 'description' => $item->get_description(), 'tags' => $this->tags($item->get_categories()) ); } return $posts; } function page_exists($url) { require_once(DOKU_INC.'inc/FeedParser.php'); $feed = new FeedParser(); $feed->set_feed_url($url); $feed->init(); return $feed->get_item_quantity(); } function get_url_parameters() { $parameters = array( 'page' => $_REQUEST['page'] ? $_REQUEST['page'] : 0, 'post' => $_REQUEST['post'], 'search' => $_REQUEST['search'], 'tagged' => $_REQUEST['tagged'] ); return $parameters; } function get_url($page=false) { $url = $this->tumblr_url; $params = $this->get_url_parameters(); if ($page !== false) { $params['page'] = $page; } // return rss url by URL parameter if ($params['search']) { $new_url = $url.'/search/'.$params['search'].'/page/'.$params['page'].'/rss'; } elseif ($params['tagged']) { $new_url = $url.'/tagged/'.$params['tagged'].'/page/'.$params['page'].'/rss'; } elseif ($params['post']) { $new_url = $url.'/'.$params['post'].'/rss'; } else { $new_url = $url.'/page/'.$params['page'].'/rss'; } return $new_url; } function tags($tags) { // SimplePie Object to Array if(!$tags) { return false; } $new_tags = array(); foreach($tags as $tag) { $new_tags[] = $tag->term; } return $new_tags; } function make_link($url, $ID, $inner=false) { // tumblr post url format is http://oo.tumblr.com/post/123456789012 $post_id = end(explode('/',$url)); $html .= ''; $html .= $inner ? $inner : $url; $html .= ''; return $html; } function print_tags($tags, $ID) { if(!$tags) { return false; } $html .= '
';
$html .= ''.$this->getLang('back_to_list').'
';
$html .= $this->getLang('err_shown_when');
$html .= '
'; $html .= ' | '; $html .= $this->make_link($post['permalink'], $pID, $post['title']); $html .= ' | '; $html .= '