new export options
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Tables\Column;
|
|||||||
use App\Tables\CStructArrayExporter;
|
use App\Tables\CStructArrayExporter;
|
||||||
use App\Tables\CsvExporter;
|
use App\Tables\CsvExporter;
|
||||||
use App\Tables\JsonExporter;
|
use App\Tables\JsonExporter;
|
||||||
|
use App\Tables\PhpExporter;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use MightyPork\Exceptions\NotApplicableException;
|
use MightyPork\Exceptions\NotApplicableException;
|
||||||
@@ -280,10 +281,22 @@ class TableController extends Controller
|
|||||||
$exporter = new CsvExporter($tableModel);
|
$exporter = new CsvExporter($tableModel);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'csv-tab':
|
||||||
|
$exporter = (new CsvExporter($tableModel))->withDelimiter("\t");
|
||||||
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
$exporter = new CStructArrayExporter($tableModel);
|
$exporter = new CStructArrayExporter($tableModel);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'php':
|
||||||
|
$exporter = new PhpExporter($tableModel);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'js':
|
||||||
|
$exporter = (new JsonExporter($tableModel))->withJsWrapper();
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
abort(400, "Unspecified or unknown format.");
|
abort(400, "Unspecified or unknown format.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,30 @@ abstract class BaseExporter
|
|||||||
$this->columns = Column::columnsFromJson($table->revision->columns);
|
$this->columns = Column::columnsFromJson($table->revision->columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getHeaderComment($prefix = " ")
|
||||||
|
{
|
||||||
|
$table = $this->table;
|
||||||
|
$owner = $this->table->owner;
|
||||||
|
|
||||||
|
$s =
|
||||||
|
$prefix."Table \"$table->title\" by $owner->title\n".
|
||||||
|
$prefix."exported ".date('M n, Y \\a\\t G:i')."\n".
|
||||||
|
$prefix."\n".
|
||||||
|
$prefix."License: ". str_replace("\n", "\n$prefix", $table->license ?: 'CC0') ."\n".
|
||||||
|
$prefix."\n".
|
||||||
|
$prefix."Upstream: https://datatable.directory/$owner->handle/$table->name\n";
|
||||||
|
|
||||||
|
$s .=
|
||||||
|
$prefix."\n".
|
||||||
|
$prefix."Columns:\n";
|
||||||
|
|
||||||
|
foreach ($this->columns as $c) {
|
||||||
|
$s .= $prefix." - $c->name ... $c->title\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $s;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string - mime type for the downloaded file
|
* @return string - mime type for the downloaded file
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ class CStructArrayExporter extends BaseExporter
|
|||||||
*/
|
*/
|
||||||
protected function writeDocument()
|
protected function writeDocument()
|
||||||
{
|
{
|
||||||
|
$table = $this->table;
|
||||||
|
|
||||||
$any_bool = false;
|
$any_bool = false;
|
||||||
$fields = array_map(function (Column $c) use (&$any_bool) {
|
$fields = array_map(function (Column $c) use (&$any_bool) {
|
||||||
$type = $c->type;
|
$type = $c->type;
|
||||||
@@ -50,13 +52,15 @@ class CStructArrayExporter extends BaseExporter
|
|||||||
return (object)['name' => $c->name, 'type' => $c->type, 'ctype' => $type, 'cname' => $name];
|
return (object)['name' => $c->name, 'type' => $c->type, 'ctype' => $type, 'cname' => $name];
|
||||||
}, $this->columns);
|
}, $this->columns);
|
||||||
|
|
||||||
$ctablename = $this->cify($this->table->name);
|
$ctablename = $this->cify($table->name);
|
||||||
|
|
||||||
// preamble
|
// preamble
|
||||||
if ($any_bool) {
|
if ($any_bool) {
|
||||||
echo "#include <stdbool.h>\n\n";
|
echo "#include <stdbool.h>\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
|
||||||
|
|
||||||
echo "struct " . $ctablename . " {\n";
|
echo "struct " . $ctablename . " {\n";
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
echo " " . $field->ctype . " " . $field->cname . ";\n";
|
echo " " . $field->ctype . " " . $field->cname . ";\n";
|
||||||
|
|||||||
@@ -6,6 +6,14 @@ namespace App\Tables;
|
|||||||
|
|
||||||
class CsvExporter extends BaseExporter
|
class CsvExporter extends BaseExporter
|
||||||
{
|
{
|
||||||
|
private $delimiter = ',';
|
||||||
|
|
||||||
|
public function withDelimiter($delimiter)
|
||||||
|
{
|
||||||
|
$this->delimiter = $delimiter;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string - mime type for the downloaded file
|
* @return string - mime type for the downloaded file
|
||||||
*/
|
*/
|
||||||
@@ -33,7 +41,7 @@ class CsvExporter extends BaseExporter
|
|||||||
return $c->title;
|
return $c->title;
|
||||||
}, $this->columns);
|
}, $this->columns);
|
||||||
|
|
||||||
fputcsv($handle, $columnNames);
|
fputcsv($handle, $columnNames, $this->delimiter);
|
||||||
|
|
||||||
foreach ($this->iterateRows() as $row) {
|
foreach ($this->iterateRows() as $row) {
|
||||||
$items = [];
|
$items = [];
|
||||||
@@ -44,7 +52,7 @@ class CsvExporter extends BaseExporter
|
|||||||
$items[] = null;
|
$items[] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fputcsv($handle, $items);
|
fputcsv($handle, $items, $this->delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
|
|||||||
@@ -6,12 +6,20 @@ namespace App\Tables;
|
|||||||
|
|
||||||
class JsonExporter extends BaseExporter
|
class JsonExporter extends BaseExporter
|
||||||
{
|
{
|
||||||
|
private $fullJsDoc = false;
|
||||||
|
|
||||||
|
public function withJsWrapper()
|
||||||
|
{
|
||||||
|
$this->fullJsDoc = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string - mime type for the downloaded file
|
* @return string - mime type for the downloaded file
|
||||||
*/
|
*/
|
||||||
protected function getMimeType()
|
protected function getMimeType()
|
||||||
{
|
{
|
||||||
return 'application/json';
|
return $this->fullJsDoc ? 'application/javascript' : 'application/json';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,6 +35,12 @@ class JsonExporter extends BaseExporter
|
|||||||
*/
|
*/
|
||||||
protected function writeDocument()
|
protected function writeDocument()
|
||||||
{
|
{
|
||||||
|
if ($this->fullJsDoc) {
|
||||||
|
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
|
||||||
|
|
||||||
|
echo 'module.exports = ';
|
||||||
|
}
|
||||||
|
|
||||||
echo "[\n";
|
echo "[\n";
|
||||||
|
|
||||||
$first = true;
|
$first = true;
|
||||||
@@ -36,7 +50,7 @@ class JsonExporter extends BaseExporter
|
|||||||
} else {
|
} else {
|
||||||
echo ",\n";
|
echo ",\n";
|
||||||
}
|
}
|
||||||
echo json_encode($row, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
|
echo ' '.json_encode($row, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "\n]\n";
|
echo "\n]\n";
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Tables;
|
||||||
|
|
||||||
|
|
||||||
|
class PhpExporter extends BaseExporter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return string - mime type for the downloaded file
|
||||||
|
*/
|
||||||
|
protected function getMimeType()
|
||||||
|
{
|
||||||
|
return $this->wantDownload ? 'application/x-php' : 'text/plain';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string - file extension for the downloaded file
|
||||||
|
*/
|
||||||
|
protected function getFileExtension()
|
||||||
|
{
|
||||||
|
return 'php';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write the document to stdout ('php://output')
|
||||||
|
*/
|
||||||
|
protected function writeDocument()
|
||||||
|
{
|
||||||
|
echo "<?php\n\n";
|
||||||
|
|
||||||
|
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
|
||||||
|
|
||||||
|
echo "return [\n";
|
||||||
|
|
||||||
|
$first = true;
|
||||||
|
foreach ($this->iterateRows() as $row) {
|
||||||
|
if ($first) {
|
||||||
|
$first = false;
|
||||||
|
} else {
|
||||||
|
echo ",\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo ' [';
|
||||||
|
|
||||||
|
$firstf = true;
|
||||||
|
foreach ($this->columns as $column) {
|
||||||
|
if ($firstf) {
|
||||||
|
$firstf = false;
|
||||||
|
} else {
|
||||||
|
echo ", ";
|
||||||
|
}
|
||||||
|
|
||||||
|
var_export($column->name);
|
||||||
|
echo ' => ';
|
||||||
|
var_export($row->{$column->name});
|
||||||
|
}
|
||||||
|
|
||||||
|
echo ']';
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "\n];\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,10 @@
|
|||||||
<ul class="mt-2">
|
<ul class="mt-2">
|
||||||
@foreach([
|
@foreach([
|
||||||
'json' => 'JSON array of objects',
|
'json' => 'JSON array of objects',
|
||||||
|
'js' => 'JavaScript module',
|
||||||
|
'php' => 'PHP include',
|
||||||
'csv' => 'CSV table',
|
'csv' => 'CSV table',
|
||||||
|
'csv-tab' => 'CSV table, tabbed',
|
||||||
'c' => 'C structs array',
|
'c' => 'C structs array',
|
||||||
] as $type => $title)
|
] as $type => $title)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user