<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO


/*
    adi_wrap - Automatic wrapper

    Written by Adi Gilbert

    Released under the GNU General Public License

    Version history:
    0.2        - general purpose attributes, e.g. role="", data_thingy="" (hypens not allowed in attribute names, auto converted)
            - 'prefix' & 'suffix' attributes
            - 'trim' attribute (default off for wraptag, on for prefix/suffix)
            - attributes: 'label', 'labeltag', 'label_class', 'label_id'
            - TXP 4.6 tag registration
    0.1        - initial release

*/

// TXP 4.6 tag registration
if (class_exists('\Textpattern\Tag\Registry')) {
    
Txp::get('\Textpattern\Tag\Registry')
        ->
register('adi_wrap')
    ;
}

function 
adi_wrap($atts,$thing=NULL) {

    
$pairs = array(
        
'wraptag'        => '',        // the wraptag
        
'class'            => '',        // class for wraptag
        
'html_id'        => '',        // HTML id for wraptag
        
'label'            => '',        // label string to precede wrap
        
'labeltag'        => '',        // tag to wrap around label
        
'label_class'    => '',        // CSS class for label's tag
        
'label_id'        => '',        // HTML ID for label's tag
        
'prefix'        => '',        // in the begining
        
'suffix'        => '',        // at the end of the day
        
'trim'            => 0,        // trim
    
);

    
extract(lAtts($pairs$atts,FALSE));

    
$other_atts array_diff_key($atts,$pairs); // carve off non-standard atts

    
$output parse($thing);

    if (
trim($output)) { // there's something to output

        
if ($trim)
            
$output trim($output);

        if (!(
$wraptag || $prefix || $suffix)) // default wrap tag
            
$wraptag 'div';

        if (
$label)
            if (empty(
$labeltag))
                
$label_output $label;
            else
                
$label_output doTag($label,$labeltag,$label_class,'',$label_id);
        else
            
$label_output '';

        if (
$wraptag) {
            
$attributes '';
            foreach (
$other_atts as $other_att => $other_value// bundle up "free_form" attributes, convert underscore to hyphen: "free-form"
                
$attributes .= ' '.str_replace('_','-',$other_att).'="'.trim($other_value).'"';
            return 
$label_output.doTag($prefix.$output.$suffix,$wraptag,$class,$attributes,$html_id); // it's a wrap
        
}
        else if (!((
$prefix == '') && ($suffix == ''))) {
            if (
array_key_exists('trim',$atts) === FALSE// trim not specified, so default on
                
$output trim($output);

            return 
$label_output.$prefix.$output.$suffix// low-calorie wrap
        
}
        else
            return 
$label_output.$output;

    }

}