Created
October 27, 2011 20:33
-
-
Save nerdsrescueme/1320783 to your computer and use it in GitHub Desktop.
Tokenizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Tokenizer { | |
protected $tokenFormat = '[! %s !]' | |
protected $rules = array(); | |
protected $raw; | |
public function execute($input) | |
{ | |
$this->raw = $input; | |
foreach($this->rules as $token => $rule) | |
{ | |
if($this->isClosable($token)) | |
{ | |
$search = array($rule['open'], $rule['close']); | |
$replace = array( | |
sprintf($this->tokenFormat, 'OPEN_'.$token), | |
sprintf($this->tokenFormat, 'CLOSE_'.$token), | |
); | |
$input = preg_replace($search, $replace, $input); | |
} | |
} | |
} | |
protected function isClosable($token) | |
{ | |
return isset($this->rules[$token]['close']; | |
} | |
protected function isNestable($key) | |
{ | |
return isset($this->rules[$token]['nested']) and $this->rules[$key]['nested']; | |
} | |
} | |
class Markdown { | |
protected $rules = array( | |
'EM' => array('open' => '\s_\S|\s\*\S'), 'close' => '\S_\s|\S\*\s'), | |
'STRONG' => array('open' => '\s__\S|\s\*\*\S'), 'close' => '\S__\s|\S\*\*\s'), | |
'EM_STRONG' =>array('open' => '\s___\S|\s\*\*\*\S'), 'close' => '\S___\s|\S\*\*\*\s'), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment