<?php
/**
 * Smarty plugin
 * ----------------------------------------------------------------
 * Type:     modifier
 * Name:     dropcap
 * Purpose:  Wraps string in p tags
 * 			 wraps first letter in span class = $dropclass
 *           wraps remainder in span with no class
 * 
 * 			 If you set class='class' it will add the letter itself
 *           as a class of the inside span.
 * 
 * Author:   Ken Griffith, kengriffith@gmail.com /
 *        http://dev.cmsmadesimple.org/users/kendo451/
 * ----------------------------------------------------------------
 **/
function smarty_cms_modifier_dropcaps($string,$dropclass='firstLetter')
{
  $str_array = str_split( $string ); 
  
  $newstring = '';
  
  $htmlTag = false;
  $firstLetterFound = false;
  $firstParagraphFound = false;
  $thisTag = '';
  $endOfPar = 0;
  
  	foreach ( $str_array as $index => $char)
  
	{
		
// 	    Next five lines used for debugging

//		$htmlTagState = ($htmlTag) ? 'true' : 'false';
//		$firstLetterFoundState = ($firstLetterFound) ? 'true' : 'false';
//		$firstParagraphFoundState = ($firstParagraphFound) ? 'true' : 'false';
		
//		print('<!--' . htmlentities($char) . ', ' . $htmlTagState . ', '. $firstLetterFoundState . ', ' . $firstParagraphFoundState . ' -->');
		
		
		if ( !$firstLetterFound ){
		
			if (!$htmlTag){
				
				if ( $char == '<') {
					$newstring .= $char;
					$htmlTag = true;

					
				}else{
					$firstLetterFound = true;
					$newstring .= '<span class="' . $dropclass . 
						'"><span class="char_' . $char . '">' . $char . 
						'</span></span><span>';		
				} 
				
			}else{
				$newstring .= $char;
				if ( $char == '>') $htmlTag = false;
			}
		
			
		}elseif ( !$firstParagraphFound ) {
			
			if (!$htmlTag){
				
				if ( $char == '<') {
				
					$htmlTag = true;
					
				}
				
				$newstring .= $char;
				
			}else{
				$newstring .= $char;
				
				if ( $char == ' ' || $char == '>'){
				// $thisTag should contain an html tag
				
					if ( $thisTag == '/div' || $thisTag == '/p' || $thisTag == 'br' || $thisTag == 'br/') {
					// we have found end of the first paragraph
					
					$firstParagraphFound = true;
					// 
					$endOfPar = $index + 56 + strlen($dropclass) - 1 - strlen($thisTag);	
					}	
					
				}else{
					$thisTag .= $char;
				}
				
				
				if ( $char == '>') {
				// reached end of tag, so reset thisTag to empty
					$htmlTag = false;
					$thisTag = '';
				}
			}		
			
		}else{
			
			$newstring .= $char;
			
		}
			

	}

	if ( $firstParagraphFound ){
		// close span just before end of 1st paragraph
		$newstring = substr_replace($newstring, '</span>', $endOfPar, 0);
	
	}else{
		// reached end of string without end of block level tag
		// so close the span at the end
		$newstring .= '</span>';
	}
	
  	return $newstring;    
}//end of function
function smarty_cms_help_function_dropcaps() {
  ?>
  <h3>What does this do?</h3>

<p>A SMARTY Modifier that finds the first letter in a string, and wraps in spans with "dropclass". It wraps the remainder of the string in an unclassed SPAN tag.</p>

<h3>How do I use it?</h3>

<p>Use this to modify a string, like so: {$string|dropcaps}</p>
<p>The default class for the dropcaps is "firstLetter". To change the default dropcap class, add the second parameter: {$strip|dropcaps:"anotherClass"} </p>

<h3>What parameters does it take?</h3>

(optional) dropclass='someClass' 
  
<h3>Example Output</h3>

<code>{capture assign='string'}This is the string.{/capture}</code>
<code>{$string|dropcaps}</code>

<p>Above code produces following output:</p>

<code>&lt;span class="firstLetter"&gt;&lt;span class="char_T"&gt;T&lt;/span>&lt;span&gt;his is the string.&lt;/span&gt;</code>  
  
<h3>Example CSS</h3>  
  
<code>
	.firstLetter{<br>
    display:block;<br>
    float:left;<br>
    margin-top :-0.63em; [[* use -0.205em for two lines *]]<br>
    margin-left:-0em;<br>
    margin-right:0.5em;<br>
    height:4.5em;<br>
}<br>
.firstLetter span{<br>
    font-size:5.6em;   [[* use 3.33em for two lines *]]<br>
    line-height:1.0em;<br>
    text-shadow:5px 5px 0px #eee,7px 7px 0px #707070;<br>
}<br>
.firstLetter + span{<br>
    margin-left:-0em;<br>
}<br>
	
</code>

<h3>Limitations</h3>
<p>This tag will recognize html tags and correctly identify the first letter if the string begins with a tag.  However, if the string begins with an HTML entity or an &amp; it will generate invalid html.</p>  
  
  <?php
}

function smarty_cms_about_function_dropcaps() {
	?>
	<p>Author: Ken Griffith&lt;ken@cmsmadesimple.org&gt;</p>
	<p>Version: 0.2</p>
	<p>
	Change History:<br/>
	Modified to make it ignore html tags inside textblog
	</p>
	<?php
}

?>
