Changeset 564020
- Timestamp:
- 06/26/2012 01:07:32 PM (14 years ago)
- Location:
- wp-auto-columns/trunk
- Files:
-
- 2 added
- 3 edited
-
include/HTMLSplitter.php (modified) (12 diffs)
-
readme.txt (modified) (2 diffs)
-
templates (added)
-
templates/settings.phtml (added)
-
wp-auto-columns.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-auto-columns/trunk/include/HTMLSplitter.php
r561103 r564020 9 9 { 10 10 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 ) 37 47 ); 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; 52 91 } 53 92 … … 61 100 public function split($html, $columns = 2) 62 101 { 63 // clean up the html64 $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 66 102 // repair html and fix tags 67 103 $tidy = new tidy(); 68 104 69 // 105 // tidy config 70 106 $config = array( 71 107 'indent' => false, … … 74 110 $tidy->parseString($html, $config, 'UTF8'); 75 111 76 // 112 // clean and repair source html 77 113 $tidy->cleanRepair(); 78 114 79 // parse 115 // parse html 80 116 $doc = new DOMDocument(); 81 117 $doc->loadHTML('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head>' . $tidy->body() . '</html>'); 82 118 83 // process 119 // process html 84 120 $nodeArray = $this->processNode($doc); 85 121 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 89 126 $colHeight = floor($totalHeight / $columns); 90 127 91 // 128 // array of columns 92 129 $cols = array(); 93 130 … … 98 135 $node = array_shift($nodeArray); 99 136 100 if (in_array($node->name, $this-> splittable))137 if (in_array($node->name, $this->config['splittable'])) 101 138 { 102 139 $current_tag = $node->name; … … 108 145 { 109 146 // 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) 111 148 continue; 112 149 … … 158 195 { 159 196 $cols[$column][] = $node->node->C14N(false); 197 //$cols[$column][] = '<p><strong>' . $node->height . '</strong></p>'; 160 198 $height += $node->height; 161 199 162 $header = in_array($node->name, $this-> headers);200 $header = in_array($node->name, $this->config['headers']); 163 201 if (!$header && $height >= $colHeight) 164 202 { … … 197 235 if ($node->name == 'ol') 198 236 { 237 // respect OL numbers 199 238 $attributes[] = 'start="' . $start . '"'; 200 239 } … … 219 258 220 259 /** 221 * 222 * @param type $node 260 * Preprocess one node 261 * 262 * @param DOMNode $node 223 263 * @return null|\stdClass 224 264 */ … … 227 267 $name = $node->nodeName; 228 268 229 if (in_array($name, $this-> skip))269 if (in_array($name, $this->config['skip'])) 230 270 { 231 271 return null; 232 272 } 233 else if (in_array($name, $this-> pass))273 else if (in_array($name, $this->config['pass'])) 234 274 { 235 275 return $this->getChildArray($node); … … 263 303 264 304 /** 265 * 305 * Get array of child nodes 306 * 307 * @param DOMNode $node 308 * @return array 266 309 */ 267 310 protected function getChildArray($node) … … 291 334 292 335 /** 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 297 341 */ 298 342 protected function calculateHeight($nodeArray, $params) … … 314 358 } 315 359 316 // 360 // calculate node height 317 361 if (count($node->children) > 0) 318 362 { 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 } 320 381 } 321 382 else if ($node->name == '#text') 322 383 { 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 328 398 $height += $node->height; 329 399 } -
wp-auto-columns/trunk/readme.txt
r561157 r564020 5 5 Requires at least: 3.1.0 6 6 Tested up to: 3.3.1 7 Stable tag: 1.0. 47 Stable tag: 1.0.5 8 8 9 9 Wrap block of text with shortcode. It will be split into columns. Automagically. … … 58 58 == Changelog == 59 59 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 60 65 = 1.0.4 = 61 66 * Added editor toolbar button -
wp-auto-columns/trunk/wp-auto-columns.php
r561157 r564020 7 7 Author: Spectraweb s.r.o. 8 8 Author URI: http://www.spectraweb.cz 9 Version: 1.0. 49 Version: 1.0.5 10 10 */ 11 11 … … 80 80 public static function on_admin_init() 81 81 { 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'); 83 88 } 84 89 … … 88 93 public static function on_admin_menu() 89 94 { 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')); 91 96 } 92 97 … … 100 105 extract(shortcode_atts(array('columns' => 2), $atts)); 101 106 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 104 119 $col_array = $splitter->split($content, $columns); 105 106 120 if (!is_array($col_array)) 107 121 { … … 143 157 /** 144 158 * 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 * 145 217 */ 146 218 public static function on_settings() 147 219 { 148 echo 'settings'; 220 $options = array(); 221 222 echo WPAutoColumns::renderTemplate('templates/settings.phtml', $options); 149 223 } 150 224 … … 197 271 readfile(plugin_dir_path(__FILE__) . 'js/footer_admin.js'); 198 272 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; 199 304 } 200 305
Note: See TracChangeset
for help on using the changeset viewer.