<?php if (!defined('PmWiki')) exit(); /* createColumns.php, a recipe for PmWiki by Adam Overton, December 2010 more info: http://www.pmwiki.org/wiki/Cookbook/CreateColumns . . . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. http://www.gnu.org/copyleft/gpl.html This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. . . . createColumns.php quickly divides up a list of items into separate columns within a table, outputting the appropriate PmWiki (:table:) markup. A user can a) easily distribute a list of items over a specific number of columns, or b) specify the number of items you'd like to list per column so that new columns are automatically added when additions to the list are made. Using createColumns.php: # Creates a table with 3 items per column, in this case resulting in 2 columns... (:createColumns perColumn=3 tableOptions="width=90% align=center" cellOptions="align=center":) item 1 item 2 item 3 item 4 item 5 (:createColumnsend:) # Creates a table that divides up the list of items into 3 columns, in this case resulting in about 2 items per column... (:createColumns nColumns=3 tableOptions="width=90% align=center" cellOptions="align=center":) Arguments: * perColumn - if used (instead of nColumns), this will determine the max number of items to place in each column; additional columns will be created until all listed items have been placed * nColumns - if used (instead of perColumn), this will try to evenly distribute items between a specified number of columns -- using nColumns will override perColumn * tableOptions="..." - inserts options into (:table ...:) * cellOptions="..." - inserts options into (:cell ...:) and (:cellnr ...:) * tableMarkup - setting tableMarkup=0 will supress the output of (:table ...:) and (:tableend:), which is useful if you want to create a list of columns within a pre-existing table Versions: * 2019-11-09 - fix "preg_replace(): The /e ..." and other errors ~Wlkl * 2010-12-16 - initial release */ $RecipeInfo['createColumns']['Version'] = '2019-11-09'; SDVA($CreateColumnsDefaults, array( 'tableMarkup' => 1 ,'perColumn' => 10 )); Markup('createColumns', '<split', '/\\(:createColumns\\s+(.*?)\\s*:\\)(.*?)\\(:createColumnsend:\\)/is', "CreateColumns_func" ); # note: need to use /s modifier - If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. function CreateColumns_func($mat) { # the matches from regular expression: 1=>arguments for table, 2=>text (all items) global $CreateColumnsDefaults; # echo " *** text = " . $mat[2] . " *** match = " . $mat[1] . " *** "; # gather up args $args = ParseArgs($mat[1]); # add default parameters before parsing arguments $args = array_merge($CreateColumnsDefaults, $args); # uses CreateColumnsDefaults, unless supplied by user # tablestart and -end markup can be suppressed by setting tableMarkup=0 $tableMarkup_enabled = $args['tableMarkup']; if ($tableMarkup_enabled) { # table setup $tableOptions = $args['tableOptions']; if($tableOptions) $tableStart = "(:table $tableOptions:)"; else $tableStart = "(:table:)"; $tableEnd = "(:tableend:)"; } # new column markup should execute no matter what $cellOptions = $args['cellOptions']; if($cellOptions) { $newColumn = "(:cell $cellOptions:)"; $newColumnNewRow = "(:cellnr $cellOptions:)"; } else { $newColumn = "(:cell:)"; $newColumnNewRow = "(:cellnr:)"; } # number of columns -- if this is present, then it will override any perColumn argument $numCol = $args['nColumns']; # number of columns # break up text, line-by-line $textArray = explode("\n", $mat[2]); # remove empty slots $textArrayClean = []; foreach($textArray as $item) { if ($item) $textArrayClean[] = $item; } $count = count($textArrayClean); #echo " *** numCol=$numCol cleanCount = $count *** "; # if $numCol is present, determine $perCol if ($numCol) { $perCol = ceil($count / $numCol); # number of items per column } else { # if $numCol not present, then use $perCol to determine number of Columns $perCol = $args['perColumn']; # number of items per column } # insert new column/cell markup $i = 0; $textWithColumns = ''; foreach($textArrayClean as $txt) { # insert new column markup every Nth item if( ($i % $perCol)===0 ) { # 1st one should be (:cellnr:) (in case there is any preceding table markup) if ($i===0) { $textWithColumns .= "$newColumnNewRow\n"; } else { # all remaining columns should be (:cell:) $textWithColumns .= "$newColumn\n"; } } # add text entries line per line $textWithColumns .= $txt."\n"; $i++; } # don't add tablestart and tableend markup if tableMarkup=0 if($tableMarkup_enabled) $textWithColumns = "$tableStart\n$textWithColumns\n$tableEnd"; # echo " *** textWithColumns = $textWithColumns ***"; return $textWithColumns; }