Files
smarty/libs/plugins/block.textformat.php

112 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/**
2010-08-17 15:39:51 +00:00
* Smarty plugin to format text blocks
*
* @package Smarty
* @subpackage PluginsBlock
*/
/**
2010-08-17 15:39:51 +00:00
* Smarty {textformat}{/textformat} block plugin
2011-09-16 14:19:56 +00:00
*
2010-08-17 15:39:51 +00:00
* Type: block function<br>
* Name: textformat<br>
* Purpose: format text a certain way with preset styles
2011-09-16 14:19:56 +00:00
* or custom wrap/indent settings<br>
* Params:
2010-08-17 15:39:51 +00:00
* <pre>
2011-09-16 14:19:56 +00:00
* - style - string (email)
* - indent - integer (0)
* - wrap - integer (80)
* - wrap_char - string ("\n")
* - indent_char - string (" ")
* - wrap_boundary - boolean (true)
2010-08-17 15:39:51 +00:00
* </pre>
2011-09-16 14:19:56 +00:00
*
* @link http://www.smarty.net/manual/en/language.function.textformat.php {textformat}
* (Smarty online manual)
* @param array $params parameters
* @param string $content contents of the block
* @param Smarty_Internal_Template $template template object
* @param boolean &$repeat repeat flag
2010-08-17 15:39:51 +00:00
* @return string content re-formatted
2011-09-16 14:19:56 +00:00
* @author Monte Ohrt <monte at ohrt dot com>
2010-08-17 15:39:51 +00:00
*/
function smarty_block_textformat($params, $content, $template, &$repeat)
{
if (is_null($content)) {
return;
2011-09-16 14:19:56 +00:00
}
$style = null;
$indent = 0;
$indent_first = 0;
$indent_char = ' ';
$wrap = 80;
$wrap_char = "\n";
$wrap_cut = false;
$assign = null;
foreach ($params as $_key => $_val) {
switch ($_key) {
case 'style':
case 'indent_char':
case 'wrap_char':
case 'assign':
$$_key = (string)$_val;
break;
case 'indent':
case 'indent_first':
case 'wrap':
$$_key = (int)$_val;
break;
case 'wrap_cut':
$$_key = (bool)$_val;
break;
default:
trigger_error("textformat: unknown attribute '$_key'");
2011-09-16 14:19:56 +00:00
}
}
if ($style == 'email') {
$wrap = 72;
2011-09-16 14:19:56 +00:00
}
// split into paragraphs
2011-09-16 14:19:56 +00:00
$_paragraphs = preg_split('![\r\n]{2}!', $content);
$_output = '';
for ($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
if ($_paragraphs[$_x] == '') {
continue;
2011-09-16 14:19:56 +00:00
}
// convert mult. spaces & special chars to single space
2011-09-16 14:19:56 +00:00
$_paragraphs[$_x] = preg_replace(array('!\s+!u', '!(^\s+)|(\s+$)!u'), array(' ', ''), $_paragraphs[$_x]);
// indent first line
if ($indent_first > 0) {
$_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
2011-09-16 14:19:56 +00:00
}
// wordwrap sentences
2011-09-16 14:19:56 +00:00
if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
$_paragraphs[$_x] = smarty_mb_wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
} else {
$_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
}
// indent lines
if ($indent > 0) {
$_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
2011-09-16 14:19:56 +00:00
}
}
$_output = implode($wrap_char . $wrap_char, $_paragraphs);
if ($assign) {
$template->assign($assign, $_output);
} else {
return $_output;
}
2011-09-16 14:19:56 +00:00
}
2010-08-17 15:39:51 +00:00
?>