/** * convert * * @param string $text * @return string */ public static function convert($text) { static $parser; if (empty($parser)) { $parser = new MarkdownExtraExtended(); } return $parser->transform($text); }
/** * 获取文章内容 * * @access protected * @return string */ protected function ___content() { if ($this->hidden) { return $this->text; } $content = $this->pluginHandle(__CLASS__)->trigger($plugged)->content($this->text, $this); if (!$plugged) { $content = $this->isMarkdown ? MarkdownExtraExtended::defaultTransform($content) : Typecho_Common::cutParagraph($content); } return $this->pluginHandle(__CLASS__)->contentEx($content, $this); }
public static function defaultTransform($text) { # try to take parser from the static parser list static $parser; # create the parser it not already set if (!$parser) { $parser = new MarkdownExtraExtended(); } # Transform text using parser. return $parser->transform($text); }
public function action() { $get_payload = function () { /** get added files GitHub webhooks https://help.github.com/articles/post-receive-hooks **/ $payload = ltrim(urldecode(file_get_contents('php://input')), 'payload='); $data = json_decode($payload, true); //without `true`, json_decode will return object instead of array return $data; }; $data = $get_payload(); $get_added_files = function ($data) { $commits = $data['commits']; foreach ($commits as $commit) { foreach ($commit['added'] as $added_file) { if (preg_match('#^_posts/#', $added_file)) { $added_files[] = $added_file; } } } return $added_files; }; $added_files = $get_added_files($data); $get_repository_url = function ($data) { return $repository_url = $data['repository']['url']; }; $repository_prefix = preg_replace('#https://#', 'https://raw.', $get_repository_url($data)); $login = function () { $master = $this->db->fetchRow($this->db->select()->from('table.users')->where('group = ?', 'administrator')->order('uid', Typecho_Db::SORT_ASC)->limit(1)); if (empty($master)) { return false; } else { if (!$this->user->simpleLogin($master['uid'])) { return false; } } }; $prepare_post = function ($to_post_file) use($login, $repository_prefix) { $input = array('do' => 'publish', 'allowComment' => $this->options->defaultAllowComment, 'allowPing' => $this->options->defaultAllowPing, 'allowFeed' => $this->options->defaultAllowFeed); list($slug) = explode('.', basename($to_post_file)); $input['slug'] = $slug; $post = $this->db->fetchRow($this->db->select()->from('table.contents')->where('slug = ?', $slug)->limit(1)); if (!empty($post)) { if ('post' != $post['type']) { return false; } else { $input['cid'] = $post['cid']; } } $url = $repository_prefix . '/master/' . $to_post_file; $post_text = file_get_contents($url); $post_text_array = Spyc::YAMLLoad($post_text); $post_sections = preg_split('/^---$/m', $post_text, 2, PREG_SPLIT_NO_EMPTY); if (sizeof($post_sections) == 2) { $post_body = $post_sections[1]; } else { $post_body = $post_sections[0]; } $input['title'] = $post_text_array['title'] ?: pathinfo($to_post_file)['filename']; $input['category'] = $post_text_array['category'] ?: 'default'; $input['tags'] = implode(',', explode(' ', $post_text_array['tags'])) ?: ''; $input['text'] = MarkdownExtraExtended::defaultTransform($post_body); return $input; }; $post_to_typecho = function ($input) { if ($input) { // It seems that only the first added file get published. $this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action(); } }; if (isset($added_files) && is_array($added_files)) { $login(); foreach ($added_files as $to_post_file) { $post_to_typecho($prepare_post($to_post_file)); } } }
/** * 获取当前评论内容 * * @access protected * @return string */ protected function ___content() { $text = $this->parentContent['hidden'] ? _t('内容被隐藏') : $this->text; $text = $this->pluginHandle(__CLASS__)->trigger($plugged)->content($text, $this); if (!$plugged) { $text = $this->options->commentsMarkdown ? MarkdownExtraExtended::defaultTransform($text) : Typecho_Common::cutParagraph($text); } $text = $this->pluginHandle(__CLASS__)->contentEx($text, $this); return Typecho_Common::stripTags($text, '<p><br>' . $this->options->commentsHTMLTagAllowed); }
/** * 稿件预览 * * @access public * @return void */ public function preview() { $cid = $this->request->filter('int')->cid; $resource = $this->_db->query($this->_db->select('text')->from('table.contribute')->where('cid = ?', $cid)->limit(1)); $result = $this->_db->fetchRow($resource); $content = $result['text']; $isMarkdown = strpos($content, '<!--markdown-->'); if (false !== $isMarkdown) { $content = str_replace('<!--markdown-->', '', $content); $content = MarkdownExtraExtended::defaultTransform($content); } else { $content = Typecho_Common::cutParagraph($content); } echo $content; }
public static function loadPostCache() { $db = Typecho_Db::get(); $posts = $db->fetchAll($db->select()->from("table.contents")->where("type = ?", "post")->order("created", Typecho_Db::SORT_DESC)); $widget = Typecho_Widget::widget("Widget_Abstract_Contents"); $result = array(); foreach ($posts as $post) { $post = $widget->filter($post); //match Thumb $post["thumb"] = false; if (self::$_options->thumbMethod == "match") { if ($post["isMarkdown"]) { $post["text"] = MarkdownExtraExtended::defaultTransform($post["text"]); } preg_match_all("/\\<img.*?src\\=(\"|\")(.*?)(\"|\")[^>]*>/i", $post["text"], $matches); if (count($matches[0]) >= 1) { $post["thumb"] = $matches[2][0]; } } $result[$post["cid"]] = array($post["title"], $post["permalink"], $post["thumb"], $post["commentsNum"]); } if (self::$_options->thumbMethod == "attach") { //match Attachments $images = array("jpg", "jpeg", "png", "gif"); $posts = $db->fetchAll($db->select("parent,text")->from("table.contents")->where("type = ?", "attachment")->order("created", Typecho_Db::SORT_ASC)); foreach ($posts as $attach) { $cid = $attach["parent"]; if (isset($result[$cid])) { $attach = @unserialize($attach["text"]); if (@in_array($attach["type"], $images)) { if (!$result[$cid][2] || isset($attach["description"]) && $attach["description"] == "thumb") { $result[$cid][2] = Typecho_Common::url($attach["path"], self::$_options->siteUrl); } } } } } self::setcache("post", $result); }