Skip to content

Instantly share code, notes, and snippets.

@adamkiss
Created December 7, 2012 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkiss/4233629 to your computer and use it in GitHub Desktop.
Save adamkiss/4233629 to your computer and use it in GitHub Desktop.
PW TextFormatter – {{ something }} parsing module
<?php
/**
* Temporary Tags for Text fields
*
* I need to push my site out ASAP, fast. I need this.
*
* @author Adam Kiss
* @version 1.0
* @since 2012-09-20
* @license None. I don't care
*
* @link http://www.adamkiss.com
*/
class TextformatterTempTags extends Textformatter implements Module {
/***************************************************************************************************
MODULE INFO
***************************************************************************************************/
public static function getModuleInfo() {
return array(
// basic information
'title' => 'Temporary adamkiss.com tags',
'version' => 100,
// load information
'singular' => true,
'autoload' => false
);
}
/***************************************************************************************************
VARIABLES
***************************************************************************************************/
/**
* Regexp for single tags
*/
private $_rgx_tag = '/{{\s?([^}]*)\s?([^}]*)?\s?}}/';
/***************************************************************************************************
MODULE STUFF (SUBMODULES, RUNNER)
***************************************************************************************************/
public function formatValue(Page $page, Field $field, &$value) {
$value = $this->applyTags($str);
}
/***************************************************************************************************
DATA MANIPULATION & CACHE
***************************************************************************************************/
private function applyTags( $text ) {
//init
$html = $text;
$found = ''; $tagsFound = array();
preg_match_all($this->_rgx_tag, $html, $found, PREG_SET_ORDER);
foreach ($found as $f){
try {
$original = $f[0];
$parts = explode(' ', $f[1]);
$tag = explode(':', array_shift($parts));
// $tag now should be array(0=>group, 1=>functions)
$parameters = implode('', $parts);
if (!empty($parameters)){
$params = new Selectors($parameters);
}else{
$params = false;
}
//apply tag by group, function and send parameters
$html = str_ireplace(
$original,
$this->{$tag[1]}($parameters),
$html);
} catch (Exception $e) {
//
}
}
return $html;
}
/***************************************************************************************************
TEMP TAGS
***************************************************************************************************/
private function image($params){
// image
}
private function figure($params){
// figure
}
private function gist($params){
// this is simple, right?
if ($params[0]->field === 'url')
return "<script src=\"https://gist.github.com/{$params[0]->value}\"></script>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment