block.textformat.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * Params:
  16. * <pre>
  17. * - style - string (email)
  18. * - indent - integer (0)
  19. * - wrap - integer (80)
  20. * - wrap_char - string ("\n")
  21. * - indent_char - string (" ")
  22. * - wrap_boundary - boolean (true)
  23. * </pre>
  24. *
  25. * @link http://www.smarty.net/manual/en/language.function.textformat.php {textformat}
  26. * (Smarty online manual)
  27. * @param array $params parameters
  28. * @param string $content contents of the block
  29. * @param Smarty_Internal_Template $template template object
  30. * @param boolean &$repeat repeat flag
  31. * @return string content re-formatted
  32. * @author Monte Ohrt <monte at ohrt dot com>
  33. */
  34. function smarty_block_textformat($params, $content, $template, &$repeat)
  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. 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]{2}!', $content);
  72. $_output = '';
  73. foreach ($_paragraphs as &$_paragraph) {
  74. if (!$_paragraph) {
  75. continue;
  76. }
  77. // convert mult. spaces & special chars to single space
  78. $_paragraph = preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER), array(' ', ''), $_paragraph);
  79. // indent first line
  80. if ($indent_first > 0) {
  81. $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
  82. }
  83. // wordwrap sentences
  84. if (Smarty::$_MBSTRING) {
  85. require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
  86. $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
  87. } else {
  88. $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
  89. }
  90. // indent lines
  91. if ($indent > 0) {
  92. $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
  93. }
  94. }
  95. $_output = implode($wrap_char . $wrap_char, $_paragraphs);
  96. if ($assign) {
  97. $template->assign($assign, $_output);
  98. } else {
  99. return $_output;
  100. }
  101. }
  102. ?>