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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  11. * Name: textformat
  12. * Purpose: format text a certain way with preset styles
  13. * or custom wrap/indent settings
  14. * Params:
  15. *
  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. *
  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. * @throws \SmartyException
  35. */
  36. function smarty_block_textformat($params, $content, Smarty_Internal_Template $template, &$repeat)
  37. {
  38. if (is_null($content)) {
  39. return;
  40. }
  41. if (Smarty::$_MBSTRING) {
  42. $template->_checkPlugins(array(array('function' => 'smarty_modifier_mb_wordwrap',
  43. 'file' => SMARTY_PLUGINS_DIR . 'modifier.mb_wordwrap.php')));
  44. }
  45. $style = null;
  46. $indent = 0;
  47. $indent_first = 0;
  48. $indent_char = ' ';
  49. $wrap = 80;
  50. $wrap_char = "\n";
  51. $wrap_cut = false;
  52. $assign = null;
  53. foreach ($params as $_key => $_val) {
  54. switch ($_key) {
  55. case 'style':
  56. case 'indent_char':
  57. case 'wrap_char':
  58. case 'assign':
  59. $$_key = (string) $_val;
  60. break;
  61. case 'indent':
  62. case 'indent_first':
  63. case 'wrap':
  64. $$_key = (int) $_val;
  65. break;
  66. case 'wrap_cut':
  67. $$_key = (bool) $_val;
  68. break;
  69. default:
  70. trigger_error("textformat: unknown attribute '{$_key}'");
  71. }
  72. }
  73. if ($style === 'email') {
  74. $wrap = 72;
  75. }
  76. // split into paragraphs
  77. $_paragraphs = preg_split('![\r\n]{2}!', $content);
  78. foreach ($_paragraphs as &$_paragraph) {
  79. if (!$_paragraph) {
  80. continue;
  81. }
  82. // convert mult. spaces & special chars to single space
  83. $_paragraph =
  84. preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER,
  85. '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER),
  86. array(' ',
  87. ''), $_paragraph);
  88. // indent first line
  89. if ($indent_first > 0) {
  90. $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
  91. }
  92. // wordwrap sentences
  93. if (Smarty::$_MBSTRING) {
  94. $_paragraph = smarty_modifier_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
  95. } else {
  96. $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
  97. }
  98. // indent lines
  99. if ($indent > 0) {
  100. $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
  101. }
  102. }
  103. $_output = implode($wrap_char . $wrap_char, $_paragraphs);
  104. if ($assign) {
  105. $template->assign($assign, $_output);
  106. } else {
  107. return $_output;
  108. }
  109. }