block.textformat.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Smarty plugin to format text blocks
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsBlock
  7. */
  8. /**
  9. * Smarty {textformat}{/textformat} block plugin
  10. *
  11. * Type: block function<br>
  12. * Name: textformat<br>
  13. * Purpose: format text a certain way with preset styles
  14. * or custom wrap/indent settings<br>
  15. *
  16. * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
  17. (Smarty online manual)
  18. * @param array $params parameters
  19. * <pre>
  20. * Params: style: string (email)
  21. * indent: integer (0)
  22. * wrap: integer (80)
  23. * wrap_char string ("\n")
  24. * indent_char: string (" ")
  25. * wrap_boundary: boolean (true)
  26. * </pre>
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @param string $content contents of the block
  29. * @param object $smarty Smarty object
  30. * @param boolean &$repeat repeat flag
  31. * @param object $template template object
  32. * @return string content re-formatted
  33. */
  34. function smarty_block_textformat($params, $content, $smarty, &$repeat, $template)
  35. {
  36. if (is_null($content)) {
  37. return;
  38. }
  39. $style = null;
  40. $indent = 0;
  41. $indent_first = 0;
  42. $indent_char = ' ';
  43. $wrap = 80;
  44. $wrap_char = "\n";
  45. $wrap_cut = false;
  46. $assign = null;
  47. foreach ($params as $_key => $_val) {
  48. switch ($_key) {
  49. case 'style':
  50. case 'indent_char':
  51. case 'wrap_char':
  52. case 'assign':
  53. $$_key = (string)$_val;
  54. break;
  55. case 'indent':
  56. case 'indent_first':
  57. case 'wrap':
  58. $$_key = (int)$_val;
  59. break;
  60. case 'wrap_cut':
  61. $$_key = (bool)$_val;
  62. break;
  63. default:
  64. $smarty->trigger_error("textformat: unknown attribute '$_key'");
  65. }
  66. }
  67. if ($style == 'email') {
  68. $wrap = 72;
  69. }
  70. // split into paragraphs
  71. $_paragraphs = preg_split('![\r\n][\r\n]!', $content);
  72. $_output = '';
  73. for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
  74. if ($_paragraphs[$_x] == '') {
  75. continue;
  76. }
  77. // convert mult. spaces & special chars to single space
  78. $_paragraphs[$_x] = preg_replace(array('!\s+!', '!(^\s+)|(\s+$)!'), array(' ', ''), $_paragraphs[$_x]);
  79. // indent first line
  80. if ($indent_first > 0) {
  81. $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
  82. }
  83. // wordwrap sentences
  84. $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
  85. // indent lines
  86. if ($indent > 0) {
  87. $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
  88. }
  89. }
  90. $_output = implode($wrap_char . $wrap_char, $_paragraphs);
  91. return $assign ? $template->assign($assign, $_output) : $_output;
  92. }
  93. ?>