PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/parser/parser.php

https://github.com/ellingen/tumblr-themes
PHP | 686 lines | 621 code | 58 blank | 7 comment | 82 complexity | 652d2b1456225543f65bcc38a9cb6a30 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. require_once 'spyc.php';
  3. class ThimbleParser {
  4. protected $variables = '/{([A-Za-z][A-Za-z0-9\-]*)}/i';
  5. protected $blocks = '/{block:([A-Za-z][A-Za-z0-9]*)}(.*?){\/block:\\1}/is';
  6. public $type = '';
  7. public $defaults = array(
  8. 'RSS' => '/rss',
  9. 'Favicon' => 'http://assets.tumblr.com/images/default_avatar_16.gif',
  10. 'PortraitURL-16' => "http://assets.tumblr.com/images/default_avatar_16.gif",
  11. 'PortraitURL-24' => "http://assets.tumblr.com/images/default_avatar_24.gif",
  12. 'PortraitURL-30' => "http://assets.tumblr.com/images/default_avatar_30.gif",
  13. 'PortraitURL-40' => "http://assets.tumblr.com/images/default_avatar_40.gif",
  14. 'PortraitURL-48' => "http://assets.tumblr.com/images/default_avatar_48.gif",
  15. 'PortraitURL-64' => "http://assets.tumblr.com/images/default_avatar_64.gif",
  16. 'PortraitURL-96' => "http://assets.tumblr.com/images/default_avatar_96.gif",
  17. 'PortraitURL-128' => "http://assets.tumblr.com/images/default_avatar_128.gif",
  18. 'CopyrightYears' => '2007-2010',
  19. );
  20. public $template = array();
  21. public function __construct($data = array(), $type = 'index') {
  22. $this->type = $type;
  23. $this->template = array_merge($this->defaults, Spyc::YAMLLoad($data));
  24. }
  25. public function block_pattern($block_name) {
  26. return '/{block:('.$block_name.')}(.*?){\/block:\\1}/is';
  27. }
  28. public function render_block($name, $html) {
  29. return preg_replace_callback(
  30. $this->block_pattern($name),
  31. create_function(
  32. '$matches',
  33. 'return $matches[2];'
  34. ),
  35. $html
  36. );
  37. }
  38. public function render_variable($name, $replacement, $block) {
  39. $block = preg_replace('/{'.$name.'}/i', $replacement, $block);
  40. $block = preg_replace('/{Plaintext'.$name.'}/i', htmlentities($replacement), $block);
  41. $block = preg_replace('/{JS'.$name.'}/i', json_encode($replacement), $block);
  42. $block = preg_replace('/{JSPlaintext'.$name.'}/i', json_encode(htmlentities($replacement)), $block);
  43. $block = preg_replace('/{URLEncoded'.$name.'}/i', urlencode($replacement), $block);
  44. return $block;
  45. }
  46. public function render_post_variable($name, $post, $block) {
  47. return $this->render_variable($name, $post[$name], $block);
  48. }
  49. public function parse($document) {
  50. $doc = $document;
  51. // Generate Options from Meta tags
  52. $doc = $this->generate_meta($doc);
  53. // Generate based on page type
  54. if ($this->type == 'index') {
  55. $doc = $this->build_index($doc);
  56. }
  57. // render Global Blocks
  58. if ($this->template['Description']) {
  59. $doc = $this->render_block('Description', $doc);
  60. }
  61. if ($this->template['Following']) {
  62. $doc = $this->render_following($this->template['Following'], $doc);
  63. } else {
  64. $doc = $this->strip_block('Following',$doc);
  65. }
  66. if ($this->template['AskLabel']) {
  67. $doc = $this->render_variable('AskLabel', $this->template['AskLabel'], $doc);
  68. $doc = $this->render_block('AskEnabled',$doc);
  69. } else {
  70. $doc = $this->strip_block('AskEnabled',$doc);
  71. }
  72. if ($this->template['SubmissionsEnabled']) {
  73. $doc = $this->render_block('SubmissionsEnabled',$doc);
  74. } else {
  75. $doc = $this->strip_block('SubmissionsEnabled',$doc);
  76. }
  77. if ($this->template['Pages']) {
  78. $doc = $this->get_pages($this->template['Pages'],$doc);
  79. } else {
  80. $doc = $this->strip_block('HasPages',$doc);
  81. }
  82. if ($this->template['TwitterUsername']) {
  83. $doc = $this->render_block('Twitter',$doc);
  84. $doc = $this->render_variable('TwitterUsername', $this->template['TwitterUsername'], $doc);
  85. } else {
  86. $doc = $this->strip_block('Twitter',$doc);
  87. }
  88. // Render remaining global variables;
  89. $doc = $this->seek($doc);
  90. // Cleanup additional blocks
  91. return $this->cleanup($doc);
  92. }
  93. public function generate_meta($document) {
  94. $dom = new DomDocument();
  95. @$dom->loadHTML($document);
  96. $meta = $this->build_options($dom->getElementsByTagName("meta"));
  97. $document = $this->parse_options($meta, $document);
  98. return $document;
  99. }
  100. public function build_options($meta_elements) {
  101. $meta = array(
  102. 'Color' => array(),
  103. 'Font' => array(),
  104. 'Boolean' => array(),
  105. 'Text' => array(),
  106. 'Image' => array()
  107. );
  108. foreach ($meta_elements as $element) {
  109. if ($element->hasAttribute('name') && $element->hasAttribute('content')) {
  110. $name = $element->getAttribute('name');
  111. $content = $element->getAttribute('content');
  112. $option = explode(':',$name);
  113. switch($option[0]) {
  114. case 'color':
  115. $meta['Color'][$option[1]] = $content;
  116. break;
  117. case 'font':
  118. $meta['Font'][$option[1]] = $content;
  119. break;
  120. case 'if':
  121. $meta['Boolean'][$option[1]] = $content;
  122. break;
  123. case 'text':
  124. $meta['Text'][$option[1]] = $content;
  125. break;
  126. case 'image':
  127. $meta['Image'][$option[1]] = $content;
  128. break;
  129. }
  130. }
  131. }
  132. return $meta;
  133. }
  134. public function parse_options($options, $doc) {
  135. foreach ($options['Color'] as $name => $color) {
  136. $doc = $this->render_variable("color:$name", $color, $doc);
  137. }
  138. foreach ($options['Font'] as $name => $font) {
  139. $doc = $this->render_variable("font:$name", $font, $doc);
  140. }
  141. foreach ($options['Boolean'] as $name => $bool) {
  142. $block_name = implode(preg_split('/\s/',ucwords($name)));
  143. if ($bool) {
  144. $doc = $this->render_block("If$block_name",$doc);
  145. $doc = $this->strip_block("IfNot$block_name",$doc);
  146. } else {
  147. $doc = $this->render_block("IfNot$block_name",$doc);
  148. $doc = $this->strip_block("If$block_name",$doc);
  149. }
  150. }
  151. foreach ($options['Text'] as $name => $text) {
  152. $block_name = implode(preg_split('/\s/',ucwords($name)));
  153. if ($text) {
  154. $doc = $this->render_variable("text:$name", $text, $doc);
  155. $doc = $this->render_block("If$block_name",$doc);
  156. $doc = $this->strip_block("IfNot$block_name",$doc);
  157. } else {
  158. $doc = $this->render_block("IfNot$block_name",$doc);
  159. $doc = $this->strip_block("If$block_name",$doc);
  160. }
  161. }
  162. foreach ($options['Image'] as $name => $img) {
  163. $block_name = implode(preg_split('/\s/',ucwords($name)));
  164. if ($img) {
  165. $doc = $this->render_variable("image:$name", $img, $doc);
  166. $doc = $this->render_block('If'.$block_name.'Image',$doc);
  167. $doc = $this->strip_block('IfNot'.$block_name.'Image',$doc);
  168. } else {
  169. $doc = $this->render_block('IfNot'.$block_name.'Image',$doc);
  170. $doc = $this->strip_block('If'.$block_name.'Image',$doc);
  171. }
  172. }
  173. return $doc;
  174. }
  175. public function build_index($doc) {
  176. // probably should build these dynamically
  177. $pages = array(
  178. 'NextPage' => '/page/2',
  179. 'CurrentPage' => '1',
  180. 'TotalPages' => '100'
  181. );
  182. $doc = $this->render_block('IndexPage', $doc);
  183. $doc = $this->render_pagination($pages, $doc);
  184. $doc = $this->get_posts($doc);
  185. return $doc;
  186. }
  187. public function get_pages($pages, $document) {
  188. $html = $document;
  189. $has_page_block = preg_match_all($this->block_pattern('Pages'), $html, $matcher);
  190. $page_group = '';
  191. if ($has_page_block) {
  192. foreach ($pages as $page) {
  193. foreach ($matcher[2] as $page_block) {
  194. $page_block = $this->render_variable('Label', $page['Label'], $page_block);
  195. $page_block = $this->render_variable('URL', $page['URL'], $page_block);
  196. $page_group .= $page_block;
  197. }
  198. }
  199. }
  200. $html = preg_replace($this->block_pattern('Pages'), $page_group, $html);
  201. $html = $this->render_block('HasPages', $html);
  202. return $html;
  203. }
  204. public function render_following($following, $document) {
  205. $html = $document;
  206. $has_following_block = preg_match_all($this->block_pattern('Followed'), $html, $matcher);
  207. $following_group = '';
  208. if ($has_following_block) {
  209. foreach ($following as $user) {
  210. foreach ($matcher[2] as $follows) {
  211. $follows = $this->render_variable('FollowedName', $user['Name'], $follows);
  212. $follows = $this->render_variable('FollowedTitle', $user['Title'], $follows);
  213. $follows = $this->render_variable('FollowedURL', $user['URL'], $follows);
  214. $portraits = array(
  215. 'PortraitURL-16', 'PortraitURL-24', 'PortraitURL-30',
  216. 'PortraitURL-40', 'PortraitURL-48', 'PortraitURL-64',
  217. 'PortraitURL-96', 'PortraitURL-128'
  218. );
  219. foreach ($portraits as $portrait) {
  220. $follows = $this->render_variable('Followed'.$portrait, $user[$portrait], $follows);
  221. }
  222. $following_group .= $follows;
  223. }
  224. }
  225. }
  226. $html = preg_replace($this->block_pattern('Followed'), $following_group, $html);
  227. $html = $this->render_block('Following', $html);
  228. return $html;
  229. }
  230. public function render_pagination($pages, $document) {
  231. $html = $document;
  232. if ($pages['NextPage'] || $pages['PreviousPage']) {
  233. if ($pages['NextPage']) {
  234. $html = $this->render_variable('NextPage', $pages['NextPage'], $html);
  235. $html = $this->render_block('NextPage', $html);
  236. } else {
  237. $html = $this->strip_block('NextPage', $html);
  238. }
  239. if ($pages['PreviousPage']) {
  240. $html = $this->render_variable('PreviousPage', $pages['PreviousPage'], $html);
  241. $html = $this->render_block('PreviousPage', $html);
  242. } else {
  243. $html = $this->strip_block('PreviousPage', $html);
  244. }
  245. $html = $this->render_block('Pagination', $html);
  246. } else {
  247. $html = $this->strip_block('Pagination', $html);
  248. }
  249. return $html;
  250. }
  251. public function get_posts($document) {
  252. $html = preg_replace_callback(
  253. $this->block_pattern('Posts'),
  254. array($this, 'render_posts'),
  255. $document
  256. );
  257. return $html;
  258. }
  259. public function render_posts($matches) {
  260. $block = $matches[2];
  261. $html = '';
  262. $posts = $this->template['Posts'];
  263. foreach ($posts as $index => $post) {
  264. if (($index+1) % 2) {
  265. $block = $this->render_block('Odd', $block);
  266. } else {
  267. $block = $this->render_block('Even', $block);
  268. }
  269. $block = $this->render_block('Post'.($index + 1),$block);
  270. $markup = $this->prepare_post($post, $block);
  271. //render post blocks non-specific to type: permalink, etc.
  272. $html .= $this->render_post($post, $this->select_by_type($post, $markup));
  273. }
  274. return $html;
  275. }
  276. public function select_by_type($post, $block) {
  277. $post_type = $this->block_pattern($post['Type']);
  278. $found = preg_match_all($post_type, $block, $posts);
  279. if ($found) {
  280. $split = preg_split($post_type, $block);
  281. $stripped = array();
  282. foreach ($split as $component) {
  283. $stripped[] = preg_replace($this->blocks, '', $component);
  284. }
  285. $html = implode(implode($posts[0]),$stripped);
  286. return $html;
  287. }
  288. }
  289. public function render_post($post, $block) {
  290. $post_type = $post['Type'];
  291. $pattern = $this->block_pattern($post_type);
  292. $does_match = preg_match_all($pattern, $block, $posts);
  293. $html = '';
  294. if ($does_match) {
  295. foreach($posts[2] as $index => $markup) {
  296. switch($post_type) {
  297. case 'Text':
  298. $html = $this->render_text_post($post, $markup);
  299. break;
  300. case 'Photo':
  301. $html = $this->render_photo_post($post, $markup);
  302. break;
  303. case 'Quote':
  304. $html = $this->render_quote_post($post, $markup);
  305. break;
  306. case 'Link':
  307. $html = $this->render_link_post($post, $markup);
  308. break;
  309. case 'Chat':
  310. $html = $this->render_chat_post($post, $markup);
  311. break;
  312. case 'Audio':
  313. $html = $this->render_audio_post($post, $markup);
  314. break;
  315. case 'Video':
  316. $html = $this->render_video_post($post, $markup);
  317. break;
  318. }
  319. $html = preg_replace($pattern, $html, $block);
  320. }
  321. return $html;
  322. }
  323. }
  324. public function prepare_post($post, $markup) {
  325. $block = $markup;
  326. $block = $this->render_post_variable('Permalink', $post, $block);
  327. $block = $this->render_post_variable('PostId', $post, $block);
  328. $block = $this->render_post_date($post, $block);
  329. if ($post['Tags']) {
  330. $block = $this->render_tags_for_post($post, $block);
  331. }
  332. if ($post['NoteCount']) {
  333. $block = $this->render_post_variable('NoteCount', $post, $block);
  334. $block = $this->render_block('NoteCount', $block);
  335. $block = $this->render_variable('NoteCountWithLabel', $post['NoteCount']." notes", $block);
  336. } else {
  337. $block = $this->strip_block('NoteCount',$block);
  338. }
  339. if ($post['Reblog']) {
  340. $block = $this->render_reblog_info($post, $block);
  341. }
  342. $block = $this->render_block('More', $block);
  343. return $block;
  344. }
  345. public function strip_block($name, $html) {
  346. return preg_replace($this->block_pattern($name), '', $html);
  347. }
  348. public function seek($context) {
  349. preg_match_all($this->variables, $context, $match);
  350. foreach ($match[1] as $variable) {
  351. if (array_key_exists($variable, $this->template)) {
  352. $context = $this->render_variable($variable, $this->template[$variable], $context);
  353. } else {
  354. $context = $this->render_variable($variable, '', $context);
  355. }
  356. }
  357. return $context;
  358. }
  359. public function cleanup($document) {
  360. return preg_replace($this->blocks, '', $document);
  361. }
  362. protected function render_post_date($post, $block) {
  363. $html = $block;
  364. $time = $post['Timestamp'];
  365. $right_now = time();
  366. $day_difference = 1;
  367. while (strtotime('-'.$day_difference.' day', $right_now) >= $time)
  368. {
  369. $day_difference++;
  370. }
  371. $html = $this->render_post_variable('Timestamp', $post, $html);
  372. $html = $this->render_variable('TimeAgo', $day_difference." days ago", $html);
  373. $html = $this->render_variable('DayOfMonth', strftime('%e',$time), $html);
  374. $html = $this->render_variable('DayOfMonthWithZero', strftime('%d',$time), $html);
  375. $html = $this->render_variable('DayOfWeek', strftime('%A',$time), $html);
  376. $html = $this->render_variable('ShortDayOfWeek', strftime('%a',$time), $html);
  377. $html = $this->render_variable('DayOfWeekNumber', strftime('%u',$time), $html);
  378. $html = $this->render_variable('DayOfYear', strftime('%j',$time), $html);
  379. $html = $this->render_variable('WeekOfYear', strftime('%V',$time), $html);
  380. $html = $this->render_variable('Month', strftime('%B',$time), $html);
  381. $html = $this->render_variable('ShortMonth', strftime('%b',$time), $html);
  382. $html = preg_replace('/{MonthNumber}|{MonthNumberWithZero}/i', strftime('%m',$time), $html);
  383. $html = $this->render_variable('Year', strftime('%Y',$time), $html);
  384. $html = $this->render_variable('ShortYear', strftime('%y',$time), $html);
  385. $html = $this->render_variable('AmPm', strftime('%P',$time), $html);
  386. $html = $this->render_variable('CapitalAmPm', strftime('%p',$time), $html);
  387. $html = $this->render_variable('12Hour', strftime('%l',$time), $html);
  388. $html = $this->render_variable('12HourWithZero', strftime('%I',$time), $html);
  389. $html = preg_replace('/{24Hour}|{24HourWithZero}/i', strftime('%H',$time), $html);
  390. $html = $this->render_variable('Minutes', strftime('%M',$time), $html);
  391. $html = $this->render_variable('Seconds', strftime('%S',$time), $html);
  392. $html = $this->render_block('Date', $html);
  393. return $html;
  394. }
  395. protected function render_tags_for_post($post, $block) {
  396. $html = $block;
  397. $tags = $post['Tags'];
  398. $has_tag_block = preg_match_all($this->block_pattern('Tags'), $html, $matcher);
  399. $tag_group = '';
  400. if ($has_tag_block) {
  401. foreach ($tags as $tag) {
  402. $safe_tag = preg_replace('/\s/','_',strtolower($tag));
  403. foreach ($matcher[2] as $tag_block) {
  404. $tag_block = $this->render_variable('Tag', $tag, $tag_block);
  405. $tag_block = $this->render_variable('URLSafeTag', $safe_tag, $tag_block);
  406. $tag_block = preg_replace('/{TagURL}|{TagURLChrono}/i', "/tagged/".$safe_tag, $tag_block);
  407. $tag_group .= $tag_block;
  408. }
  409. }
  410. }
  411. $html = $this->render_block('HasTags', $html);
  412. $html = preg_replace($this->block_pattern('Tags'), $tag_group, $html);
  413. return $html;
  414. }
  415. protected function render_reblog_info($post, $block) {
  416. $html = $block;
  417. $reblog = $post['Reblog'];
  418. $root = $reblog['Root'];
  419. $html = $this->render_post_variable('ReblogParentName', $reblog, $html);
  420. $html = $this->render_post_variable('ReblogParentTitle', $reblog, $html);
  421. $html = $this->render_post_variable('ReblogParentURL', $reblog, $html);
  422. $portraits = array(
  423. 'ReblogParentPortraitURL-16', 'ReblogParentPortraitURL-24', 'ReblogParentPortraitURL-30',
  424. 'ReblogParentPortraitURL-40', 'ReblogParentPortraitURL-48', 'ReblogParentPortraitURL-64',
  425. 'ReblogParentPortraitURL-96', 'ReblogParentPortraitURL-128'
  426. );
  427. foreach($portraits as $portrait) {
  428. $html = $this->render_post_variable($portrait, $reblog, $html);
  429. }
  430. $html = $this->render_block('Reblog', $html);
  431. $html = $this->render_block('RebloggedFrom', $html);
  432. if ($root) {
  433. $html = $this->render_post_variable('ReblogRootName', $root, $html);
  434. $html = $this->render_post_variable('ReblogRootTitle', $root, $html);
  435. $html = $this->render_post_variable('ReblogRootURL', $root, $html);
  436. $root_portraits = array(
  437. 'ReblogRootPortraitURL-16', 'ReblogRootPortraitURL-24', 'ReblogRootPortraitURL-30',
  438. 'ReblogRootPortraitURL-40', 'ReblogRootPortraitURL-48', 'ReblogRootPortraitURL-64',
  439. 'ReblogRootPortraitURL-96', 'ReblogRootPortraitURL-128'
  440. );
  441. foreach($root_portraits as $portrait) {
  442. $html = $this->render_post_variable($portrait, $root, $html);
  443. }
  444. $html = $this->render_block('RebloggedFromReblog', $html);
  445. }
  446. return $html;
  447. }
  448. protected function render_text_post($post, $block) {
  449. $html = '';
  450. $html = $this->render_post_variable('Body', $post, $block);
  451. if ($post['Title']) {
  452. $html = $this->render_post_variable('Title', $post, $html);
  453. $html = $this->render_block('Title', $html);
  454. } else {
  455. $html = $this->strip_block('Title',$html);
  456. }
  457. return $html;
  458. }
  459. protected function render_quote_post($post, $block) {
  460. $html = '';
  461. $html = $this->render_post_variable('Quote', $post, $block);
  462. $html = $this->render_post_variable('Length', $post, $html);
  463. if ($post['Source']) {
  464. $html = $this->render_post_variable('Source', $post, $html);
  465. $html = $this->render_block('Source', $html);
  466. } else {
  467. $html = $this->strip_block('Source',$html);
  468. }
  469. return $html;
  470. }
  471. protected function render_photo_post($post, $block) {
  472. $html = $block;
  473. $photo_sizes = array(
  474. 'PhotoURL-500', 'PhotoURL-400', 'PhotoURL-250', 'PhotoURL-100', 'PhotoURL-75sq'
  475. );
  476. foreach($photo_sizes as $size) {
  477. $html = $this->render_post_variable($size, $post, $html);
  478. }
  479. if ($post['Caption']) {
  480. $html = $this->render_post_variable('Caption', $post, $html);
  481. $html = $this->render_variable('PhotoAlt', strip_tags($post['Caption']), $html);
  482. $html = $this->render_block('Caption', $html);
  483. } else {
  484. $html = $this->strip_block('Caption',$html);
  485. }
  486. if ($post['PhotoURL-HighRes']) {
  487. $html = $this->render_post_variable('PhotoURL-HighRes', $post, $html);
  488. $html = $this->render_block('HighRes', $html);
  489. } else {
  490. $html = $this->strip_block('HighRes',$html);
  491. }
  492. if ($post['LinkURL']) {
  493. $html = $this->render_post_variable('LinkURL', $post, $html);
  494. $html = $this->render_variable(
  495. 'LinkOpenTag',
  496. '<a href="'.$post['LinkURL'].'">',
  497. $html
  498. );
  499. $html = $this->render_variable('LinkCloseTag', '</a>', $html);
  500. }
  501. return $html;
  502. }
  503. protected function render_link_post($post, $block) {
  504. $html = '';
  505. $html = $this->render_post_variable('URL', $post, $block);
  506. if ($post['Name']) {
  507. $html = $this->render_post_variable('Name', $post, $block);
  508. } else {
  509. $html = $this->render_variable('Name', $post['URL'], $html);
  510. }
  511. if ($post['Description']) {
  512. $html = $this->render_post_variable('Description', $post, $html);
  513. $html = $this->render_block('Description', $html);
  514. } else {
  515. $html = $this->strip_block('Description',$html);
  516. }
  517. return $html;
  518. }
  519. protected function render_chat_post($post, $block) {
  520. $html = '';
  521. $has_lines = preg_match_all($this->block_pattern('Lines'), $block, $matcher);
  522. $line_markup = '';
  523. if ($has_lines) {
  524. foreach ($matcher[2] as $each_line) {
  525. foreach ($post['Lines'] as $index => $lines) {
  526. foreach ($lines as $label => $line) {
  527. if (($index+1) % 2) {
  528. $alt = 'odd';
  529. } else {
  530. $alt = 'even';
  531. }
  532. $line_markup .= $this->render_variable('Line', $line, $each_line);
  533. $line_markup = $this->render_variable('Alt', $alt, $line_markup);
  534. $line_markup = $this->render_variable('Label', $label, $line_markup);
  535. $line_markup = $this->render_block('Label', $line_markup);
  536. }
  537. }
  538. }
  539. }
  540. $html = preg_replace($this->block_pattern('Lines'), $line_markup, $block);
  541. if ($post['Title']) {
  542. $html = $this->render_post_variable('Title', $post, $html);
  543. $html = $this->render_block('Title', $html);
  544. } else {
  545. $html = $this->strip_block('Title',$html);
  546. }
  547. return $html;
  548. }
  549. protected function render_audio_post($post, $block) {
  550. $html = $block;
  551. $audio_file = $post['AudioFile'];
  552. if ($post['ExternalAudioURL']) {
  553. $audio_file = $post['ExternalAudioURL'];
  554. $html = $this->render_post_variable('ExternalAudioURL', $post, $html);
  555. $html = $this->render_block('ExternalAudio', $html);
  556. } else {
  557. $html = $this->strip_block('ExternalAudio', $html);
  558. }
  559. $html = $this->render_variable('AudioPlayer', $this->create_audio_player($audio_file,'black'), $html);
  560. $html = $this->render_variable('AudioPlayerBlack', $this->create_audio_player($audio_file,'black'), $html);
  561. $html = $this->render_variable('AudioPlayerWhite', $this->create_audio_player($audio_file), $html);
  562. $html = $this->render_variable('AudioPlayerGrey', $this->create_audio_player($audio_file, 'grey'), $html);
  563. $html = $this->render_post_variable('PlayCount', $post, $html);
  564. $html = $this->render_variable('FormatPlayCount', number_format($post['PlayCount']), $html);
  565. $html = $this->render_variable('PlayCountWithLabel', number_format($post['PlayCount'])." plays", $html);
  566. if ($post['Caption']) {
  567. $html = $this->render_post_variable('Caption', $post, $html);
  568. $html = $this->render_block('Caption', $html);
  569. } else {
  570. $html = $this->strip_block('Caption', $html);
  571. }
  572. if ($post['AlbumArtURL']) {
  573. $html = $this->render_post_variable('AlbumArtURL', $post, $html);
  574. $html = $this->render_block('AlbumArt', $html);
  575. } else {
  576. $html = $this->strip_block('AlbumArt', $html);
  577. }
  578. if ($post['Artist']) {
  579. $html = $this->render_post_variable('Artist', $post, $html);
  580. $html = $this->render_block('Artist', $html);
  581. } else {
  582. $html = $this->strip_block('Artist', $html);
  583. }
  584. if ($post['Album']) {
  585. $html = $this->render_post_variable('Album', $post, $html);
  586. $html = $this->render_block('Album', $html);
  587. } else {
  588. $html = $this->strip_block('Album', $html);
  589. }
  590. if ($post['TrackName']) {
  591. $html = $this->render_post_variable('TrackName', $post, $html);
  592. $html = $this->render_block('TrackName', $html);
  593. } else {
  594. $html = $this->strip_block('TrackName', $html);
  595. }
  596. return $html;
  597. }
  598. protected function render_video_post($post, $block) {
  599. $html = $block;
  600. $html = $this->render_post_variable('Video-500', $post, $html);
  601. $html = $this->render_post_variable('Video-400', $post, $html);
  602. $html = $this->render_post_variable('Video-200', $post, $html);
  603. if ($post['Caption']) {
  604. $html = $this->render_post_variable('Caption', $post, $html);
  605. $html = $this->render_block('Caption', $html);
  606. } else {
  607. $html = $this->strip_block('Caption',$html);
  608. }
  609. return $html;
  610. }
  611. protected function create_audio_player($audio_file, $color = '') {
  612. if ($color && ($color != 'white')) {
  613. if ($color == 'grey') {
  614. $color = '';
  615. $audio_file .= "&color=E4E4E4";
  616. } else {
  617. $color = '_'.$color;
  618. }
  619. } else {
  620. $color = '';
  621. $audio_file .= "&color=FFFFFF";
  622. }
  623. return <<<PLAYER
  624. <script type="text/javascript" language="javascript" src="http://assets.tumblr.com/javascript/tumblelog.js?16"></script><span id="audio_player_459260683">[<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">Flash 9</a> is required to listen to audio.]</span><script type="text/javascript">replaceIfFlash(9,"audio_player_459260683",'<div class="audio_player"><embed type="application/x-shockwave-flash" src="http://demo.tumblr.com/swf/audio_player$color.swf?audio_file=$audio_file" height="27" width="207" quality="best"></embed></div>')</script>
  625. PLAYER;
  626. }
  627. }
  628. ?>