├── LICENSE ├── csv-to-table.php ├── package.json └── readme.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /csv-to-table.php: -------------------------------------------------------------------------------- 1 | set('tag', 'table', array( 3 | 'attr' => array( 4 | 'class', 5 | 'delimiter', 6 | 'thead', 7 | 'length' 8 | ), 9 | 'html' => function($tag) { 10 | $file = $tag->file($tag->attr('table')); 11 | 12 | // add class if it's defined in markdown or config file 13 | $class = (null !== $tag->attr('class') ? $tag->attr('class') : (null !== c::get('csvtotable.default.class') ? c::get('csvtotable.default.class') : '')); 14 | $class = (null !== $class ? $class = ' class="'.$class.'"' : ''); 15 | 16 | $thead = (null !== $tag->attr('thead') && $tag->attr('thead') == 'true' ? true : (null !== c::get('csvtotable.default.thead') ? c::get('csvtotable.default.thead') : false)); 17 | 18 | // default length is '0' unless set by user or config 19 | $length = (null !== $tag->attr('length') ? $tag->attr('length') : (null !== c::get('csvtotable.default.length') ? c::get('csvtotable.default.length') : 0)); 20 | 21 | // default to semi-colon as delimiter if it's not set by the user in markdown or config file 22 | $delimiter = (null !== $tag->attr('delimiter') ? $tag->attr('delimiter') : (null !== c::get('csvtotable.default.delimiter') ? c::get('csvtotable.default.delimiter') : ';')); 23 | 24 | $html = ''; 25 | $row = 0; 26 | 27 | if (($handle = fopen($file->url(), "r")) !== false) { 28 | $html .= ''; 29 | if($thead == null || $thead == false) { 30 | $html .= ''; 31 | } 32 | while (($data = fgetcsv($handle, $length, $delimiter)) !== false) { 33 | $num = count($data); 34 | $row++; 35 | 36 | // wrap first row with thead if it's set to true 37 | if($thead == true && $row == 1) { 38 | $html .= ''; 39 | for ($c=0; $c < $num; $c++) { 40 | $html .= '' . $data[$c] . ''; 41 | } 42 | $html .= ''; 43 | } else { 44 | $html .= ''; 45 | for ($c=0; $c < $num; $c++) { 46 | $html .= '' . $data[$c] . ''; 47 | } 48 | $html .= ''; 49 | } 50 | } 51 | fclose($handle); 52 | 53 | $html .= ''; 54 | } 55 | 56 | return $html; 57 | } 58 | )); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "csv-to-table", 3 | "description": "Converts CSV files into HTML tables", 4 | "author": "Lauri Liimatta ", 5 | "version": "1.0.0", 6 | "type": "kirby-plugin", 7 | "license": "MIT" 8 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Kirby CSV to HTML Table converter 2 | 3 | Version 1.0 4 | 5 | ## Installation 6 | 7 | ### 1. Kirby CLI 8 | 9 | If you are using the Kirby CLI, you can install this plugin by running the following commands in your shell: 10 | 11 | ```text 12 | $ cd path/to/kirby 13 | $ kirby plugin:install lauriiii/csv-to-table 14 | ``` 15 | 16 | ### 2. Clone or download 17 | 18 | - Clone or download this repository. 19 | - Unzip the archive if needed and rename the folder to csv-to-table. 20 | 21 | Make sure that the plugin folder structure looks like this: 22 | 23 | ```text 24 | site/plugins/csv-to-table/ 25 | ``` 26 | 27 | ## Usage 28 | 29 | ```text 30 | (table: filename.csv) 31 | ``` 32 | 33 | ### Options 34 | 35 | #### 1. Delimiter 36 | 37 | The separator used in your CSV files. 38 | 39 | Default: ; 40 | 41 | ```text 42 | (table: filename.csv delimiter: ,) 43 | ``` 44 | 45 | #### 2. Length 46 | 47 | Default: 0 48 | 49 | ```text 50 | (table: filename.csv length: 9999) 51 | ``` 52 | 53 | #### 3. Class 54 | 55 | ```text 56 | (table: filename.csv class: test) 57 | ``` 58 | 59 | #### 4. Thead 60 | 61 | Wraps the first row of the table with thead. 62 | 63 | Default: false 64 | 65 | ```text 66 | (table: filename.csv thead: true) 67 | ``` 68 | 69 | ### Config options 70 | 71 | You can use the following settings on your config file to change the defaults. 72 | 73 | ```php 74 | c::set('csvtotable.default.class', ''); 75 | c::set('csvtotable.default.delimiter', ';'); 76 | c::set('csvtotable.default.length', '0'); 77 | c::set('csvtotable.default.thead', false); 78 | ``` 79 | 80 | ## License 81 | 82 | http://www.opensource.org/licenses/mit-license.php 83 | 84 | ## Author 85 | 86 | Lauri Liimatta 87 | http://lauriliimatta.com --------------------------------------------------------------------------------