You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

block.textformat.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Type: block function<br>
  11. * Name: textformat<br>
  12. * Purpose: format text a certain way with preset styles
  13. * or custom wrap/indent settings<br>
  14. * Params:
  15. * <pre>
  16. * - style - string (email)
  17. * - indent - integer (0)
  18. * - wrap - integer (80)
  19. * - wrap_char - string ("\n")
  20. * - indent_char - string (" ")
  21. * - wrap_boundary - boolean (true)
  22. * </pre>
  23. *
  24. * @link http://www.smarty.net/manual/en/language.function.textformat.php {textformat}
  25. * (Smarty online manual)
  26. *
  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. *
  32. * @return string content re-formatted
  33. * @author Monte Ohrt <monte at ohrt dot com>
  34. */
  35. function smarty_block_textformat($params, $content, $template, &$repeat)
  36. {
  37. if (is_null($content)) {
  38. return;
  39. }
  40. $style = null;
  41. $indent = 0;
  42. $indent_first = 0;
  43. $indent_char = ' ';
  44. $wrap = 80;
  45. $wrap_char = "\n";
  46. $wrap_cut = false;
  47. $assign = null;
  48. foreach ($params as $_key => $_val) {
  49. switch ($_key) {
  50. case 'style':
  51. case 'indent_char':
  52. case 'wrap_char':
  53. case 'assign':
  54. $$_key = (string) $_val;
  55. break;
  56. case 'indent':
  57. case 'indent_first':
  58. case 'wrap':
  59. $$_key = (int) $_val;
  60. break;
  61. case 'wrap_cut':
  62. $$_key = (bool) $_val;
  63. break;
  64. default:
  65. trigger_error("textformat: unknown attribute '$_key'");
  66. }
  67. }
  68. if ($style == 'email') {
  69. $wrap = 72;
  70. }
  71. // split into paragraphs
  72. $_paragraphs = preg_split('![\r\n]{2}!', $content);
  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. }