Coder Social home page Coder Social logo

perlkonig / grav-plugin-table-importer Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 6.0 967 KB

Imports tables in YAML, JSON, and CSV formats into pages

Home Page: https://www.perlkonig.com/demos/table-importer

License: MIT License

PHP 100.00%
grav shortcode csv yaml grav-cms json gpm plugin table table-generator

grav-plugin-table-importer's Introduction

Table Importer Plugin

Abandonment Notice: I'm afraid I simply don't have the time to maintain my Grav themes and plugins. Those interested in taking over should refer to the "Abandoned Resource Protocol". Feel free to fork and replace. So long, and thanks for all the fish.

The Table Importer Plugin is for Grav CMS. It imports tables from JSON, YAML, and CSV formats into a page.

For a demo, visit my blog.

Installation

Installing the Table Importer plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.

GPM Installation (Preferred)

The simplest way to install this plugin is via the Grav Package Manager (GPM) through your system's terminal (also called the command line). From the root of your Grav install type:

bin/gpm install table-importer

This will install the Table Importer plugin into your /user/plugins directory within Grav. Its files can be found under /your/site/grav/user/plugins/table-importer.

Manual Installation

To install this plugin, just download the zip version of this repository and unzip it under /your/site/grav/user/plugins. Then, rename the folder to table-importer. You can find these files on GitHub or via GetGrav.org.

You should now have all the plugin files under

/your/site/grav/user/plugins/table-importer

NOTE: This plugin is a modular component for Grav which requires Grav and the Error, Problems and Shortcode Core plugins to operate.

Configuration

To edit the configuratino, first copy table-importer.yaml from the user/plugins/table-importer folder to your user/config/plugins folder and only edit that copy.

The only configuration setting is enabled, which turns the plugin off and on.

This plugin extends the Shortcode Core infrastructure. See that documentation to learn how to disable/enable shortcode processing on a page-by-page basis.

Usage

This plugin converts JSON, YAML, and CSV files into HTML code and can be used in conjunction with other table plugins like Tablesorter. It only works with simple, even data (see the next section for details). If you wish to accomplish something more complex, then consider combining the Import plugin with some custom twig code.

Formatting Your Data

The plugin is naive and assumes that your data is well formed and that your tables have even row lengths (the table is rectangular). If the plugin can't find the data file or can't understand its format, then the shortcode will be replaced by an error message. Otherwise, the only evidence something is wrong will be the shortcode still showing or weird table rendering.

The only requirement is that your data file parse to a two-dimensional array: an array of rows from top to bottom, each row containing cells from left to right. Each cell must be a value PHP can natively render as a string.

Some samples—json, then yaml, then CSV:

[
  ["Col1", "Col2", "Col3"],
  ["Val1", "Val2", "Val3"],
  ["Val4", "Val5", "Val6"],
  ["Val7", "Val8", "Val9"]
]
-
  - Col1
  - Col2
  - Col3
-
  - Val1
  - Val2
  - Val3
-
  - Val4
  - Val5
  - Val6
-
  - Val7
  - Val8
  - Val9
Col1,Col2,Col3
Val1,Val2,Val3
Val4,Val5,Val6
Val7,Val8,Val9

Inserting a Table

This plugin uses the Shortcode Core infrastructure. Read those docs for the nitty gritty of how shortcodes work.

The Table Importer shortcode is a self-closing [ti option1="value1" option2="value2" ... /], and it accepts the following options:

  • file is the only required parameter. It points to the datafile you wish to load. By default, the plugin looks in the same folder as the page file. This is adequate for most usage. You can also load files from the user/data folder by prefixing your file name with data: (e.g., file=data:tables/mytable.yaml).

    If all you're passing is the file name, then you can shorten the code to the form [ti=mytable.yaml/].

  • type is usually unnecessary. It tells the plugin what format the data file is in. The only acceptable values are yaml, json, and csv. However, the plugin looks at the file name extension first. If it's yaml, yml, json, or csv, then there's no need to use the type option.

  • caption will insert a <caption> tag containing the value of this option after being run through PHP's htmlspecialchars.

  • header tells the plugin whether you want a header row or not. By default, the first row is rendered as a header. Passing any value to header will disable the header row.

  • class lets you assign class definitions to the table itself. Whatever you put here will be escaped (via PHP's htmlspecialchars) and placed into the opening <table> tag.

  • id lets you specify the table tag's id attribute (e.g. [ti file="mytable.yaml" id="my-custom-table-id"] yields <table id="my-custom-table-id">...</table>).

  • By default, the content of each cell is escaped using PHP's htmlspecialchars function. If the raw option is set to anything at all, the escaping will be disabled. Only do this if you trust the incoming data!

  • Finally, for CSV files only, you can customize how it will be parsed using any of the following three options:

    • delimiter defines how columns are separated. By default, the value is a comma (,).

    • enclosure defines how cells with special characters are contained. By default, the value is a double quotation mark (").

    • escape defines how special characters can be escaped. By default, the value is a backslash (\).

Example Codes

  • [ti=test.json] (basic import of json table in the same folder as the page itself)

  • [ti=data:test.yaml] (basic import of yaml table in the user/data folder)

  • [ti file=json-as-yaml.json type=yaml] (parse a file as yaml regardless of extension)

  • [ti file=file.csv enclosure='] (parse a CSV file that uses a single quote to enclose items)

  • [ti file=file.yaml header="false" class="imported"] (basic yaml table with no header and a class of imported)

Credits

Because PHP's builtin CSV support is...let's just say inelegant, this plugin incorporates the most excellent PHPLeague CSV library.

grav-plugin-table-importer's People

Contributors

duncan3dc avatar gamahachaa avatar grahamcampbell avatar h4cc avatar hannesvdvreken avatar heart1010 avatar heyratfans avatar jblotus avatar kyleblanker avatar mfrost503 avatar mwender avatar nyamsprod avatar pborreli avatar perlkonig avatar rhukster avatar sagikazarmark avatar vlakarados avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

grav-plugin-table-importer's Issues

Incompatible with shortcode plugin?

This plugin seems to be incompatible with the Shortcode plugin. With Shortcode plugin enabled, the TableImporter shortcode is displayed as raw text [TableImporter>data.csv] instead of being converted to a table.

Is there any workaround for this?

Excel file support

Hi there, while I don't have immediate application for it, this plugin seems like it could fit an upcoming need. Something to look at perhaps would be support for Excel files. The following library does that and also seems to support CSV

https://github.com/PHPOffice/PHPExcel

Something to consider.

Dean

Table does not import in blog archive output

My csv files import successfully while pointing directly on the page, but when I am viewing multiple pages via a blog view, I get the following error:

Table Importer: Could not resolve file name 'filename.csv'.

I suspect that this is caused by the table importer using a relative URL to load the .csv file.

Link inside of the table

Hi,

I am trying to put a link to another web page inside of the table created with Table Importer. I have tried different things, but nothing works.

Any advice?

Loading CSV via HTTP

I tried to import a table via HTTP, but got the following error:

Table Importer: Could not resolve file name 'http://repos.local/content/pages/appendix/file.csv'.

Is this functionality supported?

Table Importer: Could not resolve file name 'table.csv'.

Hi,
Useful concept for one of my clients as they use a lot of tables in their Grav build, but coming up with the error:

Table Importer: Could not resolve file name 'table.csv'.

I have the CSV file in the same directory from where I'm calling it from and my code is just the basic:

[ti file=table.csv enclosure=']

Any ideas? Thanks

array_merge(): Argument #2 is not an array

Error Received:
array_merge(): Argument #2 is not an array

Line Specified:
if (isset($page->header()->{'table-importer'})) {
$this->config->set('plugins.table-importer', array_merge($defaults, $page->header()->{'table-importer'}));
}

CSV sample data used:
Name,Status,Notes
John,Active,Twice per week
Susan,Inactive,Pending surgery
Harry,Active,Traveling to New York

shortcode into cell

Hello,
thank you in advance for your works,
I need to render shortcodes into cells (i.e links)
Building standard table using markdown works as expected:

Name Surname Picture
Luther Blissett Luther Icon

produce a table with the Picture column rendering a link relative to a .png into a local grav folder where default.md page was called

now if I put into the csv the shortcode it's not parsed:
test:
Name,Surname,Picture
Luther,Blissett,[u]https://upload.wikimedia.org/wikipedia/commons/0/02/Luther_Blissett.jpg[/u]

at the end the shortcode was not parsed

thank you for your collaboration
kindly regards

Call to a member function path() on null errors on Grav 1.7

The table-importer plugin v2.2.1 works fine on Grav 1.6.16 but after upgrading to 1.7.x, it throws a Call to a member function path() on null error at user/plugins/table-importer/TableImporterShortcode.php:166.

The error does not occur if the individual page cache is disabled ( cache_enable: false ). The relevant page config is as follows:

title: 'My Tableimporter Page'
tablesorter:
    active: true
    raw: true
    tablenums: '1,2'
    include_widgets: true
    include_metadata: true
    themes: bootstrap
    args:
        1:
            theme: bootstrap
            widgets:
                - filter
                - zebra
        2:
            theme: bootstrap
            widgets:
                - filter
                - zebra
---

Screen Shot 2022-01-25 at 9 38 42 AM

Error loading pages after latest update (linked to caching)

After updating to the latest version, the pages where I'm using this plugin started throwing the error Call to a member function path() on null. It points to line 166 of the file TableImporterShortcode.php:

$path = $this->grav['shortcode']->getPage()->path();

Clearing the cache temporarily fixes the problem and lets the page load once, but then errors on reload. Setting cache_enable: false on the page prevents the error but I'd prefer to let the pages be cached if the issue can be addressed another way.

v2.1.2 do not work.

I followed the installation, table importer and short are active.
But the content [ti file=import.csv/] is not parsed.

E_NOTICE when not using options

Description

When we use a basic import (i.e. [TableImporter>test.json]), we get E_NOTICE errors of undefined index and undefined constant options.

Suggestion

Change grav-plugin-table-importer/table-importer.php:85 to $options = isset($match[3]) ? $match[3] : []; and grav-plugin-table-importer/table-importer.php:86 to if (!empty($options)) {.

Versions

$ php --version
PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.8-0ubuntu0.16.04.2, Copyright (c) 1999-2016, by Zend Technologies

$ bin/gpm --version
Grav Package Manager version 1.1.5

$bin/gpm info table-importer

GPM Releases Configuration: Stable

Found package 'table-importer' under the 'Plugins' section

Table Importer [table-importer]
-------------------------------
Imports tables from JSON, YAML, and CSV formats into Markdown tables within a page

Author      : Aaron Dalton <[email protected]> 
Version     : 1.0.1
Keywords    : grav, plugin, tables, import, json, yaml, csv
Last Update : Tue, 16 Sep 2014, 00:07:16, +00:00 
Homepage    : https://github.com/Perlkonig/grav-plugin-table-importer
Demo        : https://perlkonig.com/demos/table-importer
Docs        : https://github.com/Perlkonig/grav-plugin-table-importer/blob/master/README.md
Repository  : https://github.com/Perlkonig/grav-plugin-table-importer
Bugs        : https://github.com/Perlkonig/grav-plugin-table-importer/issues
Download    : https://github.com/Perlkonig/grav-plugin-table-importer/zipball/v1.0.1
License     : MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.