1<?php 2/** 3 * Wrapper class for whitespace related stuff 4 * 5 * PHP versions 5 6 * 7 * LICENSE: This source file is subject to version 3.0 of the PHP license 8 * that is available through the world-wide-web at the following URI: 9 * http://www.php.net/license/3_0.txt. If you did not receive a copy of 10 * the PHP License and are unable to obtain it through the web, please 11 * send a note to license@php.net so we can mail you a copy immediately. 12 * 13 * @category Tools and Utilities 14 * @package CodeGen 15 * @author Hartmut Holzgraefe <hartmut@php.net> 16 * @copyright 2005-2008 Hartmut Holzgraefe 17 * @license http://www.php.net/license/3_0.txt PHP License 3.0 18 * @version CVS: $Id: Indent.php,v 1.5 2006/10/11 10:07:41 hholzgra Exp $ 19 * @link http://pear.php.net/package/CodeGen 20 */ 21 22/** 23 * Wrapper class for whitespace related stuff 24 * 25 * all methods are actually static, the class is just needed for 26 * namespace emulation to conform with PEAR naming conventions 27 * 28 * @category Tools and Utilities 29 * @package CodeGen 30 * @author Hartmut Holzgraefe <hartmut@php.net> 31 * @copyright 2005-2008 Hartmut Holzgraefe 32 * @license http://www.php.net/license/3_0.txt PHP License 3.0 33 * @version Release: @package_version@ 34 * @link http://pear.php.net/package/CodeGen 35 */ 36class CodeGen_Tools_Indent { 37 /** 38 * Replace leading blanks with tabs 39 * 40 * PHP C coding conventions require tab indention 41 * with a tabsize of 4 42 * 43 * @access public 44 * @param string text to tabify 45 * @param int tab indention level (default: 4 spaces) 46 * @return string tabified text 47 */ 48 function tabify($text, $tabsize = 4) 49 { 50 return preg_replace_callback("/^(".str_repeat(" ", $tabsize).")+/m", 51 create_function('$matches', 'return str_repeat("\t", strlen($matches[0])/'.$tabsize.');'), 52 $text); 53 } 54 55 56 /** 57 * Replace leading tabs with blanks 58 * 59 * PEAR PHP coding conventions require blank indention 60 * with a tabsize of 4 61 * 62 * @access public 63 * @param string text to untabify 64 * @param int tab indention level (default: 4 spaces) 65 * @return string untabified text 66 */ 67 function untabify($text, $tabsize = 4) 68 { 69 return preg_replace_callback("/^(\t)*/m", 70 create_function('$matches', 'return str_repeat(" ", strlen($matches[0])*'.$tabsize.');'), 71 $text); 72 } 73 74 /** 75 * re-indent a block of text 76 * 77 * @access public 78 * @param int number of leading indent spaces 79 * @param string text to reindent 80 * @return string indented text 81 */ 82 function indent($level, $text) 83 { 84 $text = self::untabify($text); 85 86 $lines = explode("\n", $text); 87 88 // remove trailing blank lines 89 while (count($lines) && trim(end($lines)) == "") { 90 array_pop($lines); 91 } 92 93 // how far is this block intented right now? 94 $minIndent = 999; 95 foreach ($lines as $line) { 96 if (trim($line)=="") continue; // ignore blank lines 97 if ($line{0} == '#') continue; // ignore preprocessor instructions 98 preg_match("|^ *|", $line, $matches); // detect leading blanks 99 $minIndent = min($minIndent, strlen($matches[0])); 100 } 101 102 $result = ""; 103 $find = str_repeat(" ", $minIndent); 104 $replace = str_repeat(" ", $level); 105 foreach ($lines as $line) { 106 $result.= self::tabify(preg_replace("|^$find|", $replace, $line)."\n", 4); 107 } 108 109 return self::linetrim($result); 110 } 111 112 113 /** 114 * Trim of leading and trailing whitespace-only lines 115 * 116 * @access public 117 * @param string text 118 * @return string trimmed text 119 */ 120 function linetrim($text) 121 { 122 $text = preg_replace('|^\s*\n|', '', $text); 123 $text = preg_replace('|\n\s*$|', "\n", $text); 124 return $text; 125 } 126 127 /** 128 * Change to DOS/Windows line terminators 129 * 130 * @access public 131 * @param string unix text 132 * @param string dos/windows text 133 */ 134 function dosify($text) 135 { 136 return str_replace("\n", "\r\n", $text); 137 } 138} 139 140/* 141 * Local variables: 142 * tab-width: 4 143 * c-basic-offset: 4 144 * indent-tabs-mode:nil 145 * End: 146 */ 147 148