PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/site/kirby/parsers/kirbytext.php

https://github.com/mozilla/web-forward
PHP | 394 lines | 263 code | 109 blank | 22 comment | 50 complexity | a37a55747bbf1e3d90c2bfc921c47266 MD5 | raw file
  1. <?php
  2. // direct access protection
  3. if(!defined('KIRBY')) die('Direct access is not allowed');
  4. function kirbytext($text, $markdown=true) {
  5. return kirbytext::init($text, $markdown);
  6. }
  7. // create an excerpt without html and kirbytext
  8. function excerpt($text, $length=140, $markdown=true) {
  9. return str::excerpt(kirbytext::init($text, $markdown), $length);
  10. }
  11. function youtube($url, $width=false, $height=false, $class=false) {
  12. $name = kirbytext::classname();
  13. $obj = new $name;
  14. return $obj->youtube(array(
  15. 'youtube' => $url,
  16. 'width' => $width,
  17. 'height' => $height,
  18. 'class' => $class
  19. ));
  20. }
  21. function vimeo($url, $width=false, $height=false, $class=false) {
  22. $name = kirbytext::classname();
  23. $obj = new $name;
  24. return $obj->vimeo(array(
  25. 'vimeo' => $url,
  26. 'width' => $width,
  27. 'height' => $height,
  28. 'class' => $class
  29. ));
  30. }
  31. function flash($url, $width=false, $height=false) {
  32. $name = kirbytext::classname();
  33. $obj = new $name;
  34. return $obj->flash($url, $width, $height);
  35. }
  36. function twitter($username, $text=false, $title=false, $class=false) {
  37. $name = kirbytext::classname();
  38. $obj = new $name;
  39. return $obj->twitter(array(
  40. 'twitter' => $username,
  41. 'text' => $text,
  42. 'title' => $title,
  43. 'class' => $class
  44. ));
  45. }
  46. function gist($url, $file=false) {
  47. $name = kirbytext::classname();
  48. $obj = new $name;
  49. return $obj->gist(array(
  50. 'gist' => $url,
  51. 'file' => $file
  52. ));
  53. }
  54. class kirbytext {
  55. var $obj = null;
  56. var $text = null;
  57. var $mdown = true;
  58. var $smartypants = true;
  59. var $tags = array('gist', 'twitter', 'date', 'image', 'file', 'link', 'email', 'youtube', 'vimeo');
  60. var $attr = array('text', 'file', 'width', 'height', 'link', 'popup', 'class', 'title', 'alt', 'rel', 'lang');
  61. static function init($text=false, $mdown=true, $smartypants=true) {
  62. $classname = self::classname();
  63. $kirbytext = new $classname($text, $mdown, $smartypants);
  64. return $kirbytext->get();
  65. }
  66. function __construct($text=false, $mdown=true, $smartypants=true) {
  67. $this->text = $text;
  68. $this->mdown = $mdown;
  69. $this->smartypants = $smartypants;
  70. // pass the parent page if available
  71. if(is_object($this->text)) $this->obj = $this->text->parent;
  72. }
  73. function get() {
  74. $text = preg_replace_callback('!(?=[^\]])\((' . implode('|', $this->tags) . '):(.*?)\)!i', array($this, 'parse'), (string)$this->text);
  75. $text = preg_replace_callback('!```(.*?)```!is', array($this, 'code'), $text);
  76. $text = ($this->mdown) ? markdown($text) : $text;
  77. $text = ($this->smartypants) ? smartypants($text) : $text;
  78. return $text;
  79. }
  80. function code($code) {
  81. $code = @$code[1];
  82. $lines = explode("\n", $code);
  83. $first = trim(array_shift($lines));
  84. $code = implode("\n", $lines);
  85. $code = trim($code);
  86. if(function_exists('highlight')) {
  87. $result = '<pre class="highlight ' . $first . '">';
  88. $result .= '<code>';
  89. $result .= highlight($code, (empty($first)) ? 'php-html' : $first);
  90. $result .= '</code>';
  91. $result .= '</pre>';
  92. } else {
  93. $result = '<pre class="' . $first . '">';
  94. $result .= '<code>';
  95. $result .= htmlspecialchars($code);
  96. $result .= '</code>';
  97. $result .= '</pre>';
  98. }
  99. return $result;
  100. }
  101. function parse($args) {
  102. $method = strtolower(@$args[1]);
  103. $string = @$args[0];
  104. if(empty($string)) return false;
  105. if(!method_exists($this, $method)) return $string;
  106. $replace = array('(', ')');
  107. $string = str_replace($replace, '', $string);
  108. $attr = array_merge($this->tags, $this->attr);
  109. $search = preg_split('!(' . implode('|', $attr) . '):!i', $string, false, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  110. $result = array();
  111. $num = 0;
  112. foreach($search AS $key) {
  113. if(!isset($search[$num+1])) break;
  114. $key = trim($search[$num]);
  115. $value = trim($search[$num+1]);
  116. $result[ $key ] = $value;
  117. $num = $num+2;
  118. }
  119. return $this->$method($result);
  120. }
  121. function url($url, $lang=false) {
  122. if(str::contains($url, 'http://') || str::contains($url, 'https://')) return $url;
  123. if(!$this->obj) {
  124. global $site;
  125. // search for a matching
  126. $files = $site->pages()->active()->files();
  127. } else {
  128. $files = $this->obj->files();
  129. }
  130. if($files) {
  131. $file = $files->find($url);
  132. $url = ($file) ? $file->url() : url($url, $lang);
  133. }
  134. return $url;
  135. }
  136. function link($params) {
  137. $url = @$params['link'];
  138. $class = @$params['class'];
  139. $rel = @$params['rel'];
  140. $title = @$params['title'];
  141. $lang = @$params['lang'];
  142. $target = self::target($params);
  143. // language attribute is only allowed when lang support is activated
  144. if($lang && !c::get('lang.support')) $lang = false;
  145. // add a css class if available
  146. if(!empty($class)) $class = ' class="' . $class . '"';
  147. if(!empty($rel)) $rel = ' rel="' . $rel . '"';
  148. if(!empty($title)) $title = ' title="' . html($title) . '"';
  149. if(empty($url)) $url = '/';
  150. if(empty($params['text'])) return '<a' . $target . $class . $rel . $title . ' href="' . $this->url($url, $lang) . '">' . html($url) . '</a>';
  151. return '<a' . $target . $class . $rel . $title . ' href="' . $this->url($url, $lang) . '">' . html($params['text']) . '</a>';
  152. }
  153. function image($params) {
  154. global $site;
  155. $url = @$params['image'];
  156. $text = @$params['text'];
  157. $class = @$params['class'];
  158. $alt = @$params['alt'];
  159. $title = @$params['title'];
  160. $target = self::target($params);
  161. // alt is just an alternative for text
  162. if(!empty($text)) $alt = $text;
  163. // width/height
  164. $w = a::get($params, 'width');
  165. $h = a::get($params, 'height');
  166. if(!empty($w)) $w = ' width="' . $w . '"';
  167. if(!empty($h)) $h = ' height="' . $h . '"';
  168. // add a css class if available
  169. if(!empty($class)) $class = ' class="' . $class . '"';
  170. if(!empty($title)) $title = ' title="' . html($title) . '"';
  171. if(empty($alt)) $alt = $site->title();
  172. $image = '<img src="' . $this->url($url) . '"' . $w . $h . $class . $title . ' alt="' . html($alt) . '" />';
  173. if(!empty($params['link'])) {
  174. if($params['link'] == 'self') $params['link'] = $url;
  175. return '<a' . $class . $target . $title . ' href="' . $this->url($params['link']) . '">' . $image . '</a>';
  176. }
  177. return $image;
  178. }
  179. function file($params) {
  180. $url = @$params['file'];
  181. $text = @$params['text'];
  182. $class = @$params['class'];
  183. $title = @$params['title'];
  184. $target = self::target($params);
  185. if(empty($text)) $text = $url;
  186. if(!empty($class)) $class = ' class="' . $class . '"';
  187. if(!empty($title)) $title = ' title="' . html($title) . '"';
  188. return '<a' . $target . $title . $class . ' href="' . $this->url($url) . '">' . html($text) . '</a>';
  189. }
  190. static function date($params) {
  191. $format = @$params['date'];
  192. return (str::lower($format) == 'year') ? date('Y') : date($format);
  193. }
  194. static function target($params) {
  195. if(empty($params['popup'])) return false;
  196. return ' target="_blank"';
  197. }
  198. static function email($params) {
  199. $url = @$params['email'];
  200. $class = @$params['class'];
  201. $title = @$params['title'];
  202. if(empty($url)) return false;
  203. return str::email($url, @$params['text'], $title, $class);
  204. }
  205. static function twitter($params) {
  206. $username = @$params['twitter'];
  207. $class = @$params['class'];
  208. $title = @$params['title'];
  209. $target = self::target($params);
  210. if(empty($username)) return false;
  211. $username = str_replace('@', '', $username);
  212. $url = 'http://twitter.com/' . $username;
  213. // add a css class if available
  214. if(!empty($class)) $class = ' class="' . $class . '"';
  215. if(!empty($title)) $title = ' title="' . html($title) . '"';
  216. if(empty($params['text'])) return '<a' . $target . $class . $title . ' href="' . $url . '">@' . html($username) . '</a>';
  217. return '<a' . $target . $class . $title . ' href="' . $url . '">' . html($params['text']) . '</a>';
  218. }
  219. static function youtube($params) {
  220. $url = @$params['youtube'];
  221. $class = @$params['class'];
  222. $id = false;
  223. // http://www.youtube.com/embed/d9NF2edxy-M
  224. if(@preg_match('!youtube.com\/embed\/([a-z0-9_-]+)!i', $url, $array)) {
  225. $id = @$array[1];
  226. // http://www.youtube.com/watch?feature=player_embedded&v=d9NF2edxy-M#!
  227. } elseif(@preg_match('!v=([a-z0-9_-]+)!i', $url, $array)) {
  228. $id = @$array[1];
  229. // http://youtu.be/d9NF2edxy-M
  230. } elseif(@preg_match('!youtu.be\/([a-z0-9_-]+)!i', $url, $array)) {
  231. $id = @$array[1];
  232. }
  233. // no id no result!
  234. if(empty($id)) return false;
  235. // build the embed url for the iframe
  236. $url = 'https://www.youtube.com/embed/' . $id;
  237. // default width and height if no custom values are set
  238. if(empty($params['width'])) $params['width'] = c::get('kirbytext.video.width');
  239. if(empty($params['height'])) $params['height'] = c::get('kirbytext.video.height');
  240. // add a classname to the iframe
  241. if(!empty($class)) $class = ' class="' . $class . '"';
  242. return '<iframe' . $class . ' width="' . $params['width'] . '" height="' . $params['height'] . '" src="' . $url . '" frameborder="0" allowfullscreen></iframe>';
  243. }
  244. static function vimeo($params) {
  245. $url = @$params['vimeo'];
  246. $class = @$params['class'];
  247. // get the uid from the url
  248. @preg_match('!vimeo.com\/([a-z0-9_-]+)!i', $url, $array);
  249. $id = a::get($array, 1);
  250. // no id no result!
  251. if(empty($id)) return false;
  252. // build the embed url for the iframe
  253. $url = 'https://player.vimeo.com/video/' . $id;
  254. // default width and height if no custom values are set
  255. if(empty($params['width'])) $params['width'] = c::get('kirbytext.video.width');
  256. if(empty($params['height'])) $params['height'] = c::get('kirbytext.video.height');
  257. // add a classname to the iframe
  258. if(!empty($class)) $class = ' class="' . $class . '"';
  259. return '<iframe' . $class . ' src="' . $url . '" width="' . $params['width'] . '" height="' . $params['height'] . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
  260. }
  261. static function flash($url, $w, $h) {
  262. if(!$w) $w = c::get('kirbytext.video.width');
  263. if(!$h) $h = c::get('kirbytext.video.height');
  264. return '<div class="video"><object width="' . $w . '" height="' . $h . '"><param name="movie" value="' . $url . '"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="' . $url . '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' . $w . '" height="' . $h . '"></embed></object></div>';
  265. }
  266. static function gist($params) {
  267. $url = @$params['gist'] . '.js';
  268. $file = @$params['file'];
  269. if(!empty($file)) {
  270. $url = $url .= '?file=' . $file;
  271. }
  272. return '<script src="' . $url . '"></script>';
  273. }
  274. static function classname() {
  275. return class_exists('kirbytextExtended') ? 'kirbytextExtended' : 'kirbytext';
  276. }
  277. function addTags() {
  278. $args = func_get_args();
  279. $this->tags = array_merge($this->tags, $args);
  280. }
  281. function addAttributes($attr) {
  282. $args = func_get_args();
  283. $this->attr = array_merge($this->attr, $args);
  284. }
  285. }