source: branches/0.4/web/addons/job_monarch/dwoo/plugins/builtin/blocks/textformat.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 2.9 KB
Line 
1<?php
2
3/**
4 * Formats a string to the given format, you can wrap lines at a certain
5 * length and indent them
6 * <pre>
7 *  * wrap : maximum line length
8 *  * wrap_char : the character(s) to use to break the line
9 *  * wrap_cut : if true, the words that are longer than $wrap are cut instead of overflowing
10 *  * indent : amount of $indent_char to insert before every line
11 *  * indent_char : character(s) to insert before every line
12 *  * indent_first : amount of additional $indent_char to insert before the first line of each paragraphs
13 *  * style : some predefined formatting styles that set up every required variables, can be "email" or "html"
14 *  * assign : if set, the formatted text is assigned to that variable instead of being output
15 * </pre>
16 * This software is provided 'as-is', without any express or implied warranty.
17 * In no event will the authors be held liable for any damages arising from the use of this software.
18 *
19 * @author     Jordi Boggiano <j.boggiano@seld.be>
20 * @copyright  Copyright (c) 2008, Jordi Boggiano
21 * @license    http://dwoo.org/LICENSE   Modified BSD License
22 * @link       http://dwoo.org/
23 * @version    1.0.0
24 * @date       2008-10-23
25 * @package    Dwoo
26 */
27class Dwoo_Plugin_textformat extends Dwoo_Block_Plugin
28{
29        protected $wrap;
30        protected $wrapChar;
31        protected $wrapCut;
32        protected $indent;
33        protected $indChar;
34        protected $indFirst;
35        protected $assign;
36
37        public function init($wrap=80, $wrap_char="\r\n", $wrap_cut=false, $indent=0, $indent_char=" ", $indent_first=0, $style="", $assign="")
38        {
39                if ($indent_char === 'tab') {
40                        $indent_char = "\t";
41                }
42
43                switch($style) {
44
45                case 'email':
46                        $wrap = 72;
47                        $indent_first = 0;
48                        break;
49                case 'html':
50                        $wrap_char = '<br />';
51                        $indent_char = $indent_char == "\t" ? '&nbsp;&nbsp;&nbsp;&nbsp;':'&nbsp;';
52                        break;
53
54                }
55
56                $this->wrap = (int) $wrap;
57                $this->wrapChar = (string) $wrap_char;
58                $this->wrapCut = (bool) $wrap_cut;
59                $this->indent = (int) $indent;
60                $this->indChar = (string) $indent_char;
61                $this->indFirst = (int) $indent_first + $this->indent;
62                $this->assign = (string) $assign;
63        }
64
65        public function process()
66        {
67                // gets paragraphs
68                $pgs = explode("\n", str_replace(array("\r\n", "\r"), "\n", $this->buffer));
69
70                while (list($i,) = each($pgs)) {
71                        if (empty($pgs[$i])) {
72                                continue;
73                        }
74
75                        // removes line breaks and extensive white space
76                        $pgs[$i] = preg_replace(array('#\s+#', '#^\s*(.+?)\s*$#m'), array(' ', '$1'), str_replace("\n", '', $pgs[$i]));
77
78                        // wordwraps + indents lines
79                        $pgs[$i] = str_repeat($this->indChar, $this->indFirst) .
80                                        wordwrap(
81                                                        $pgs[$i],
82                                                        max($this->wrap - $this->indent, 1),
83                                                        $this->wrapChar . str_repeat($this->indChar, $this->indent),
84                                                        $this->wrapCut
85                                        );
86                }
87
88                if ($this->assign !== '') {
89                        $this->dwoo->assignInScope(implode($this->wrapChar . $this->wrapChar, $pgs), $this->assign);
90                } else {
91                        return implode($this->wrapChar . $this->wrapChar, $pgs);
92                }
93        }
94}
Note: See TracBrowser for help on using the repository browser.