PostFormatter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. define("TAG_PROCESSOR_PATH","Model/TagProcessors/");
  3. include(TAG_PROCESSOR_PATH."ITagProcessor.php");
  4. class PostFormatter{
  5. public static $TAG_MATCH_REGEX='@\[([a-z]*) ?(.*?)(\](.*?)\[)?/\1?\]@ms';
  6. private static $_content=array();
  7. private static $_randomIndexModifier="";
  8. private static function ParamsToArgs($params){
  9. $args=array();
  10. foreach ($params as $p){
  11. if (strpos($p, "=")===false)
  12. continue;
  13. $parts=explode("=",$p);
  14. $parts[1]=trim($parts[1],'"');
  15. $args[$parts[0]]=$parts[1];
  16. }
  17. return $args;
  18. }
  19. private static function ProcessTag($matches){
  20. $processorName=$matches[1];
  21. if (!file_exists(TAG_PROCESSOR_PATH.$processorName.".php"))
  22. return $matches[0];
  23. include_once(TAG_PROCESSOR_PATH.$processorName.".php");
  24. $class="\\Tags\\".$processorName;
  25. $processor=new $class;
  26. if (!($processor instanceof \Tags\ITagProcessor))
  27. return $matches[0];
  28. $params=array();
  29. if (isset($matches[2]))
  30. $params=explode(" ",trim($matches[2]));
  31. $content=null;
  32. if (isset($matches[4]))
  33. $content=self::GetProcessedString($matches[4]);
  34. $args=self::ParamsToArgs($params);
  35. $result=call_user_func("\\Tags\\".$processorName."::Process",$args,$content);
  36. if ($result!==FALSE){
  37. $index=count(self::$_content);
  38. self::$_content[$index]=$result;
  39. return '{{'.self::$_randomIndexModifier.$index.'}}';
  40. }
  41. return $matches[0];
  42. }
  43. private static function GetProcessedString($string){
  44. return preg_replace_callback(self::$TAG_MATCH_REGEX,"self::ProcessTag", $string);
  45. }
  46. private static function PopulateString($matches){
  47. if (!isset($matches[1]))
  48. return $matches[0];
  49. $index=(int)$matches[1];
  50. if (!isset(self::$_content[$index]))
  51. return $matches[0];
  52. $str=self::ProcessPopulateString(self::$_content[$index]);
  53. return $str;
  54. }
  55. private static function ProcessPopulateString($string){
  56. $str=preg_replace_callback('/\{\{'.self::$_randomIndexModifier.'(\d+)\}\}/', "self::PopulateString", $string);
  57. return $str;
  58. }
  59. public static function FormatToHTML($str){
  60. self::$_content=array();
  61. self::$_randomIndexModifier=Utils::GenerateRandomString();
  62. $str=self::GetProcessedString($str);
  63. $str=str_replace("\r", '', $str);
  64. $strParts=explode("\n\n", $str);
  65. $returnStr="";
  66. foreach ($strParts as $s){
  67. $s=trim($s);
  68. if ($s=="")
  69. continue;
  70. $s=htmlentities($s);
  71. $s=self::GetProcessedString($s);
  72. $s=nl2br($s);
  73. $returnStr.="<p>$s</p>";
  74. }
  75. $returnStr=self::ProcessPopulateString($returnStr);
  76. return $returnStr;
  77. }
  78. public static function CloseOpenTag($str){
  79. $matches=array();
  80. preg_match_all('@.*\[([a-z]*) ?[^/]*?\](.*)$@ms', $str, $matches); //(?![\[/\1\]])
  81. if (count($matches[0])==0 || preg_match_all('@\[/'.$matches[1][0].'\]@', $matches[2][0])>0)
  82. return $str;
  83. $closeTag='[/'.$matches[1][0].']';
  84. return $str.PHP_EOL.$closeTag;
  85. }
  86. }