Plugin Directory

Changeset 564020


Ignore:
Timestamp:
06/26/2012 01:07:32 PM (14 years ago)
Author:
spectraweb
Message:
 
Location:
wp-auto-columns/trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • wp-auto-columns/trunk/include/HTMLSplitter.php

    r561103 r564020  
    99{
    1010
    11     protected $skip;
    12     protected $pass;
    13     protected $modifiers;
    14     protected $headers;
    15     protected $splittable;
    16     protected $break;
    17 
    18     /**
    19      *
    20      */
    21     public function __construct()
    22     {
    23         // skip tags
    24         $this->skip = array('head');
    25         $this->pass = array('#document', 'html', 'body');
    26         // size modifiers
    27         $this->modifiers = array(
    28             '#default' => array('line-count' => 60, 'line-height' => 1, 'margin-bottom' => 0),
    29             'h1' => array('line-count' => 20, 'line-height' => 2.5, 'margin-bottom' => 1),
    30             'h2' => array('line-count' => 25, 'line-height' => 2, 'margin-bottom' => 1),
    31             'h3' => array('line-count' => 40, 'line-height' => 1.5, 'margin-bottom' => 1),
    32             'br' => array('margin-bottom' => 1),
    33             'hr' => array('margin-bottom' => 1),
    34             'ul' => array('margin-bottom' => 1),
    35             'ol' => array('margin-bottom' => 1),
    36             'p' => array('margin-bottom' => 1),
     11    // split configuration
     12    protected $config;
     13
     14    /**
     15     *
     16     */
     17    public function __construct($config = null)
     18    {
     19        // default configuration values
     20        $this->config = array(
     21            // line height in pixels
     22            'line_height' => 14,
     23            // skip tags
     24            'skip' => array('head'),
     25            // pass tags as is
     26            'pass' => array('#document', 'html', 'body'),
     27            // header tags
     28            'headers' => array(),
     29            // splittable tags
     30            'splittable' => array(),
     31            // inline tags
     32            'inline' => array('span', 'a', 'b', 'strong', 'i', 'em'),
     33            // line break tags
     34            'break' => array('br'),
     35            // size modifiers
     36            'modifiers' => array(
     37                'default' => array('line-chars' => 50, 'line-height' => 1, 'margin-bottom' => 0),
     38                'h1' => array('line-chars' => 20, 'line-height' => 2.5, 'margin-bottom' => 1),
     39                'h2' => array('line-chars' => 25, 'line-height' => 2, 'margin-bottom' => 1),
     40                'h3' => array('line-chars' => 40, 'line-height' => 1.5, 'margin-bottom' => 1),
     41                'br' => array('line-height' => 0.5, 'margin-bottom' => 1),
     42                'hr' => array('margin-bottom' => 1),
     43                'ul' => array('margin-bottom' => 1),
     44                'ol' => array('margin-bottom' => 1),
     45                'p' => array('margin-bottom' => 1),
     46            )
    3747        );
    38         // header tags
    39         $this->headers = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
    40         // splittable tags
    41         $this->splittable = array('div', 'p', 'ul', 'ol');
    42         // line break tags
    43         $this->break = array('br');
    44     }
    45 
    46     /**
    47      *
    48      */
    49     public function setModifiers($modifiers)
    50     {
    51         $this->modifiers = array_merge($this->modifiers, $modifiers);
     48
     49        if ($config != null)
     50        {
     51            $this->setConfig($config);
     52        }
     53    }
     54
     55    /**
     56     * Set external modifiers
     57     */
     58    public function setConfig($config)
     59    {
     60        $this->config = $this->array_merge($this->config, $config);
     61    }
     62
     63    /**
     64     *
     65     * @param type $array1
     66     * @param type $array2
     67     * @return
     68     */
     69    private function array_merge()
     70    {
     71        $arrays = func_get_args();
     72        $merged = array();
     73        while ($arrays)
     74        {
     75            $array = array_shift($arrays);
     76            if (!$array)
     77                continue;
     78
     79            foreach ($array as $key => $value)
     80            {
     81                if (is_string($key))
     82                    if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
     83                        $merged[$key] = call_user_func(__FUNCTION__, $merged[$key], $value);
     84                    else
     85                        $merged[$key] = $value;
     86                else
     87                    $merged[] = $value;
     88            }
     89        }
     90        return $merged;
    5291    }
    5392
     
    61100    public function split($html, $columns = 2)
    62101    {
    63         // clean up the html
    64         $html = strip_tags($html, '<p><a><b><i><u><strong><em><br><h1><h2><h3><h4><h5><h6><ul><ol><li><dt><dd><dl><hr><span>');
    65 
    66102        // repair html and fix tags
    67103        $tidy = new tidy();
    68104
    69         //
     105        // tidy config
    70106        $config = array(
    71107            'indent' => false,
     
    74110        $tidy->parseString($html, $config, 'UTF8');
    75111
    76         //
     112        // clean and repair source html
    77113        $tidy->cleanRepair();
    78114
    79         // parse
     115        // parse html
    80116        $doc = new DOMDocument();
    81117        $doc->loadHTML('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head>' . $tidy->body() . '</html>');
    82118
    83         // process
     119        // process html
    84120        $nodeArray = $this->processNode($doc);
    85121
    86         // calculate
    87         $totalHeight = $this->calculateHeight($nodeArray, $this->modifiers['#default']);
    88 
     122        // calculate estimated height of whole html
     123        $totalHeight = $this->calculateHeight($nodeArray, $this->config['modifiers']['default']);
     124
     125        // calculate column height
    89126        $colHeight = floor($totalHeight / $columns);
    90127
    91         //
     128        // array of columns
    92129        $cols = array();
    93130
     
    98135            $node = array_shift($nodeArray);
    99136
    100             if (in_array($node->name, $this->splittable))
     137            if (in_array($node->name, $this->config['splittable']))
    101138            {
    102139                $current_tag = $node->name;
     
    108145                {
    109146                    // skip break tags in the beginning of the column
    110                     if (in_array($childNode->name, $this->break) && count($children) == 0)
     147                    if (in_array($childNode->name, $this->config['break']) && count($children) == 0)
    111148                        continue;
    112149
     
    158195            {
    159196                $cols[$column][] = $node->node->C14N(false);
     197                //$cols[$column][] = '<p><strong>' . $node->height . '</strong></p>';
    160198                $height += $node->height;
    161199
    162                 $header = in_array($node->name, $this->headers);
     200                $header = in_array($node->name, $this->config['headers']);
    163201                if (!$header && $height >= $colHeight)
    164202                {
     
    197235            if ($node->name == 'ol')
    198236            {
     237                // respect OL numbers
    199238                $attributes[] = 'start="' . $start . '"';
    200239            }
     
    219258
    220259    /**
    221      *
    222      * @param type $node
     260     * Preprocess one node
     261     *
     262     * @param DOMNode $node
    223263     * @return null|\stdClass
    224264     */
     
    227267        $name = $node->nodeName;
    228268
    229         if (in_array($name, $this->skip))
     269        if (in_array($name, $this->config['skip']))
    230270        {
    231271            return null;
    232272        }
    233         else if (in_array($name, $this->pass))
     273        else if (in_array($name, $this->config['pass']))
    234274        {
    235275            return $this->getChildArray($node);
     
    263303
    264304    /**
    265      *
     305     * Get array of child nodes
     306     *
     307     * @param DOMNode $node
     308     * @return array
    266309     */
    267310    protected function getChildArray($node)
     
    291334
    292335    /**
    293      *
    294      * @param type $nodeArray
    295      * @param type $params
    296      * @return type
     336     * Calculate estimated height of node with subnodes
     337     *
     338     * @param array $nodeArray
     339     * @param array $params
     340     * @return integer height value
    297341     */
    298342    protected function calculateHeight($nodeArray, $params)
     
    314358            }
    315359
    316             //
     360            // calculate node height
    317361            if (count($node->children) > 0)
    318362            {
    319                 $node->height += $this->calculateHeight($node->children, $nodeParams);
     363                // node has children
     364                $children_height = $this->calculateHeight($node->children, $nodeParams);
     365
     366                $node->height += $children_height + ($nodeParams['margin-bottom'] * $this->config['line_height'] * $nodeParams['line-height']);
     367            }
     368            else if ($node->name == 'img')
     369            {
     370                // image tag, try to calculate height based on attribute
     371                $img_height = intval($node->node->getAttribute('height'));
     372                if ($img_height != 0)
     373                {
     374                    $node->height += $img_height;
     375                }
     376                else
     377                {
     378                    // no height attribute, try to process the image
     379                    $img_src = $node->node->getAttribute('src');
     380                }
    320381            }
    321382            else if ($node->name == '#text')
    322383            {
    323                 $lines = ceil(strlen($node->text) / $params['line-count']);
    324 
    325                 $node->height += ($lines * $params['line-height']) + $params['margin-bottom'];
    326             }
    327 
     384                // text node, height depends on number of chars
     385                $lines = ceil(strlen($node->text) / $nodeParams['line-chars']);
     386
     387                // estimate text height
     388                $node->height += ($lines * $this->config['line_height'] * $nodeParams['line-height']);
     389            }
     390            else
     391            {
     392                // other nodes, height is calculated by estimated measurements
     393                $node->height += (1 * $this->config['line_height'] * $nodeParams['line-height'])
     394                    + ($nodeParams['margin-bottom'] * $this->config['line_height'] * $nodeParams['line-height']);
     395            }
     396
     397            // update node height
    328398            $height += $node->height;
    329399        }
  • wp-auto-columns/trunk/readme.txt

    r561157 r564020  
    55Requires at least: 3.1.0
    66Tested up to: 3.3.1
    7 Stable tag: 1.0.4
     7Stable tag: 1.0.5
    88
    99Wrap block of text with shortcode. It will be split into columns. Automagically.
     
    5858== Changelog ==
    5959
     60= 1.0.5 =
     61* Added processing of "height" attribute for IMG tags
     62* Fixed behaviour of <br/> tags
     63* Settings page for height measurement fine tuning
     64
    6065= 1.0.4 =
    6166* Added editor toolbar button
  • wp-auto-columns/trunk/wp-auto-columns.php

    r561157 r564020  
    77  Author: Spectraweb s.r.o.
    88  Author URI: http://www.spectraweb.cz
    9   Version: 1.0.4
     9  Version: 1.0.5
    1010 */
    1111
     
    8080    public static function on_admin_init()
    8181    {
    82         //register_setting('wp_fetch_page_settings', 'wp_fetch_tm_currency');
     82        register_setting('wp_auto_columns', 'wp_auto_columns_line_height');
     83
     84        register_setting('wp_auto_columns', 'wp_auto_columns_tags_headers');
     85        register_setting('wp_auto_columns', 'wp_auto_columns_tags_splittable');
     86
     87        register_setting('wp_auto_columns', 'wp_auto_columns_height_modifiers');
    8388    }
    8489
     
    8893    public static function on_admin_menu()
    8994    {
    90         //add_options_page(__('Auto Columns Options', 'theme'), __('Auto Columns', 'theme'), 'manage_options', basename(__FILE__), array('WPAutoColumns', 'on_settings'));
     95        add_options_page(__('Auto Columns Options', 'theme'), __('Auto Columns', 'theme'), 'manage_options', basename(__FILE__), array('WPAutoColumns', 'on_settings'));
    9196    }
    9297
     
    100105        extract(shortcode_atts(array('columns' => 2), $atts));
    101106
    102         $splitter = new HTMLSplitter();
    103 
     107        $config = array(
     108            'line_height' => WPAutoColumns::get_option('wp_auto_columns_line_height', 14),
     109            'headers' => WPAutoColumns::get_option_string_array('wp_auto_columns_tags_headers'),
     110            'splittable' => WPAutoColumns::get_option_string_array('wp_auto_columns_tags_splittable'),
     111            'modifiers' => get_option('wp_auto_columns_height_modifiers'),
     112        );
     113
     114        $config = WPAutoColumns::filter_empty($config);
     115
     116        $splitter = new HTMLSplitter($config);
     117
     118        // try to split content
    104119        $col_array = $splitter->split($content, $columns);
    105 
    106120        if (!is_array($col_array))
    107121        {
     
    143157    /**
    144158     *
     159     * @param type $option
     160     * @param type $default
     161     * @return type
     162     */
     163    public static function get_option($option, $default = false)
     164    {
     165        $value = get_option($option, $default);
     166        return $value;
     167    }
     168
     169    /**
     170     *
     171     * @param type $option
     172     * @param type $default
     173     * @return type
     174     */
     175    public static function get_option_string_array($option, $default = false)
     176    {
     177        $ret = array();
     178
     179        $values = explode(',', get_option($option, $default));
     180
     181        foreach ($values as $v)
     182        {
     183            $v = trim($v);
     184            if ($v != '')
     185                $ret[] = $v;
     186        }
     187
     188        return $ret;
     189    }
     190
     191    /**
     192     *
     193     */
     194    private static function filter_empty($array)
     195    {
     196        $ret = array();
     197        foreach ($array as $key => $value)
     198        {
     199            if (is_array($value))
     200            {
     201                $filtered = WPAutoColumns::filter_empty($value);
     202                if (!empty($filtered))
     203                {
     204                    $ret[$key] = $filtered;
     205                }
     206            }
     207            else if ($value != '')
     208            {
     209                $ret[$key] = $value;
     210            }
     211        }
     212        return $ret;
     213    }
     214
     215    /**
     216     *
    145217     */
    146218    public static function on_settings()
    147219    {
    148         echo 'settings';
     220        $options = array();
     221
     222        echo WPAutoColumns::renderTemplate('templates/settings.phtml', $options);
    149223    }
    150224
     
    197271        readfile(plugin_dir_path(__FILE__) . 'js/footer_admin.js');
    198272        echo '</script>' . "\n";
     273    }
     274
     275    /**
     276     *
     277     * @param type $template_name
     278     * @param type $options
     279     * @return string
     280     */
     281    static function renderTemplate($template_name, $options = array())
     282    {
     283        $template_file = plugin_dir_path(__FILE__) . $template_name;
     284
     285        if (is_file($template_file))
     286        {
     287            extract($options);
     288
     289            ob_start();
     290
     291            include $template_file;
     292
     293            $ret = ob_get_contents();
     294
     295            ob_end_clean();
     296        }
     297        else
     298        {
     299            // @todo process error
     300            $ret = '<strong>Template ' . $template_file . ' not found!</strong>';
     301        }
     302
     303        return $ret;
    199304    }
    200305
Note: See TracChangeset for help on using the changeset viewer.