Automatically exported from code.google.com/p/planningalerts
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.
 
 
 
 
 
 

104 rivejä
2.8 KiB

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