Last active
May 21, 2022 19:45
-
-
Save nyamsprod/4d8c0037cd4f514062ef6549460d1fef to your computer and use it in GitHub Desktop.
Convert a CSV document into a Markdown document containing a table representing the original CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use League\BooBoo; | |
use League\CLImate; | |
use League\Csv; | |
use League\Container; | |
use League\Flysystem; | |
use League\HTMLToMarkdown; | |
use League\Pipeline; | |
require 'vendor/autoload.php'; | |
$booboo = new BooBoo\BooBoo([new BooBoo\Formatter\CommandLineFormatter()]); | |
$booboo->register(); | |
$container = new Container\Container(); | |
$container->add(HTMLToMarkdown\Converter\TableConverter::class); | |
$container->add(HTMLToMarkdown\Converter\ConverterInterface::class, fn () => $container->get(HTMLToMarkdown\Converter\TableConverter::class)); | |
$container->add(HTMLToMarkdown\Environment::class)->addMethodCall('addConverter', [HTMLToMarkdown\Converter\ConverterInterface::class]); | |
$container->add(HTMLToMarkdown\HtmlConverter::class)->addArgument(HTMLToMarkdown\Environment::class); | |
$container->add('root_directory', new Container\Argument\Literal\StringArgument(__DIR__)); | |
$container->add(Flysystem\Local\LocalFilesystemAdapter::class)->addArgument($container->get('root_directory')); | |
$container->add(Flysystem\FilesystemAdapter::class, fn () => $container->get(Flysystem\Local\LocalFilesystemAdapter::class)); | |
$container->add(Flysystem\Filesystem::class)->addArgument(Flysystem\FilesystemAdapter::class); | |
$container->add(CLImate\CLImate::class); | |
/** @var Flysystem\Filesystem $fileServer */ | |
$fileServer = $container->get(Flysystem\Filesystem::class); | |
/** @var CLImate\CLImate $climate $htmlConverter */ | |
$htmlConverter = $container->get(HTMLToMarkdown\HtmlConverter::class); | |
/** @var CLImate\CLImate $climate */ | |
$climate = $container->get(CLImate\CLImate::class); | |
$pipeline = (new Pipeline\Pipeline()) | |
->pipe(fn (string $path) => $fileServer->readStream($path)) | |
->pipe(fn ($stream): Csv\Reader => Csv\Reader::createFromStream($stream)->setHeaderOffset(0)) | |
->pipe(fn (Csv\Reader $csv): string => Csv\HTMLConverter::create()->convert($csv, $csv->getHeader())) | |
->pipe(fn (string $html): string => $htmlConverter->convert($html)) | |
->pipe(fn (string $markdown): mixed => $climate->to(['out', 'buffer'])->out($markdown)) | |
->pipe(fn (mixed $result): mixed => $fileServer->write('result.md', $climate->output->get('buffer')->get())) | |
; | |
$pipeline->process('packages.csv'); | |
// returns and save to result.md file the following Markdown markup | |
// | Last name | First name | Package | | |
// |---|---|---| | |
// | O'Dell | Colin | CommonMark | | |
// | De Jonge | Frank | Flysystem | | |
// | Nyamagana Butera | Ignace | CSV | | |
// | Reinink | Jonathan | Glide | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Last name | First name | Package | |
---|---|---|---|
O'Dell | Colin | CommonMark | |
De Jonge | Frank | Flysystem | |
Nyamagana Butera | Ignace | CSV | |
Reinink | Jonathan | Glide |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is a totally unrealistic take on what some of the league packages can do.
You should not use them for this BUT you should use them like this.
This fictional example is to illustrate how good some of the feature they provide are.
This is an extended version of the original tweet https://twitter.com/nyamsprod/status/1527540324482138117 example.
Special thanks to all the league members to have make this example work on PHP8.1