1 |
<?php |
2 |
|
3 |
/* |
4 |
* Smarty plugin |
5 |
* ------------------------------------------------------------- |
6 |
* Type: block function |
7 |
* Name: textformat |
8 |
* Purpose: format text a certain way with preset styles |
9 |
* or custom wrap/indent settings |
10 |
* Params: style: string (email) |
11 |
* indent: integer (0) |
12 |
* wrap: integer (80) |
13 |
* wrap_char string ("\n") |
14 |
* indent_char: string (" ") |
15 |
* wrap_boundary: boolean (true) |
16 |
* ------------------------------------------------------------- |
17 |
*/ |
18 |
function smarty_block_textformat($params, $content, &$this) |
19 |
{ |
20 |
$style = null; |
21 |
$indent = 0; |
22 |
$indent_first = 0; |
23 |
$indent_char = ' '; |
24 |
$wrap = 80; |
25 |
$wrap_char = "\n"; |
26 |
$wrap_cut = false; |
27 |
$assign = null; |
28 |
|
29 |
if($content == null) { |
30 |
return true; |
31 |
} |
32 |
|
33 |
extract($params); |
34 |
|
35 |
if($style == 'email') { |
36 |
$wrap = 72; |
37 |
} |
38 |
|
39 |
// split into paragraphs |
40 |
$paragraphs = preg_split('![\r\n][\r\n]!',$content); |
41 |
|
42 |
foreach($paragraphs as $paragraph) { |
43 |
if($paragraph == '') { |
44 |
continue; |
45 |
} |
46 |
// convert mult. spaces & special chars to single space |
47 |
$paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph); |
48 |
// indent first line |
49 |
if($indent_first > 0) { |
50 |
$paragraph = str_repeat($indent_char,$indent_first) . $paragraph; |
51 |
} |
52 |
// wordwrap sentences |
53 |
$paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut); |
54 |
// indent lines |
55 |
if($indent > 0) { |
56 |
$paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph); |
57 |
} |
58 |
$output .= $paragraph . $wrap_char . $wrap_char; |
59 |
} |
60 |
|
61 |
if($assign != null) { |
62 |
$this->assign($assign,$output); |
63 |
} else { |
64 |
echo $output; |
65 |
} |
66 |
//echo $content; |
67 |
} |
68 |
|
69 |
/* vim: set expandtab: */ |
70 |
|
71 |
?> |