Skip to content

Commit

Permalink
Added description to landing page examples (#1185)
Browse files Browse the repository at this point in the history
* Added description to landing page examples

* Added missing headlines to example page
  • Loading branch information
norberttech authored Aug 11, 2024
1 parent f4c1d9b commit deede5a
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/topics/data_source/array/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Read data directly from a php associative array. Relays on `array_to_rows` DSL function.
22 changes: 22 additions & 0 deletions examples/topics/data_source/csv/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Read data from a csv file.

```php
function from_csv(
string|Path|array $path,
bool $with_header = true,
bool $empty_to_null = true,
?string $delimiter = null,
?string $enclosure = null,
?string $escape = null,
int $characters_read_in_line = 1000,
?Schema $schema = null
):
```

* `with_header` - default true, if false, the first row will be treated as data
* `empty_to_null` - default false, if true, empty string will be treated as null
* `delimiter` - the delimiter of the csv file, when not set, it will be auto detected
* `enclosure` - the enclosure of the csv file, when not set, it will be auto detected
* `escape` - default `\`, the escape character of the csv file
* `characters_in_line` - default `1000`, size of chunk used to read lines from the file
* `schema` - the schema of the csv file, when not set, it will be auto detected
12 changes: 12 additions & 0 deletions examples/topics/data_source/json/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Read data from a json file.

```php
function from_json(
string|Path|array $path,
?string $pointer = null,
?Schema $schema = null,
);
```

* `pointer` - default null, used to iterate only results of a subtree, read more about [pointers](https://github.com/halaxa/json-machine#parsing-a-subtree)
* `schema` - the schema of the csv file, when not set, it will be auto detected
16 changes: 16 additions & 0 deletions examples/topics/data_source/parquet/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Read data from a json file.

```php
function from_parquet(
string|Path|array $uri,
array $columns = [],
Options $options = new Options(),
ByteOrder $byte_order = ByteOrder::LITTLE_ENDIAN,
?int $offset = null,
);
```

* `columns` - default [], list of columns to read, when empty, all columns will be read
* `options` - custom Parquet Reader [Options](https://github.com/flow-php/flow/blob/1.x/src/lib/parquet/src/Flow/Parquet/Options.php)
* `byte_order` - default `ByteOrder::LITTLE_ENDIAN`, the byte order of the parquet file
* `offset` - default null, rows to skip from the beginning of the file
10 changes: 10 additions & 0 deletions examples/topics/data_source/xml/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Read data from a json file.

```php
function from_xml(
string|Path|array $path,
string $xml_node_path = ''
);
```

* `xml_node_path` - default '', the path to the node to read, when empty, the root node will be read. It's not xpath, it is just a sequence of node names separated with slash.
2 changes: 2 additions & 0 deletions examples/topics/schema/inferring/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Schema inferring is a process of generating a schema from a whole or part of the dataset.
Once schema is inferred, it can be saved and used to speed up next dataset processing.
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
use Flow\Parquet\{ByteOrder, Options};

/**
* @param array<Path>|Path|string $uri
* @param array<Path>|Path|string $path
* @param array<string> $columns
*
* @return Extractor
*/
function from_parquet(
string|Path|array $uri,
string|Path|array $path,
array $columns = [],
Options $options = new Options(),
ByteOrder $byte_order = ByteOrder::LITTLE_ENDIAN,
?int $offset = null,
) : Extractor {
if (\is_array($uri)) {
if (\is_array($path)) {
$extractors = [];

if ($offset !== null) {
throw new InvalidArgumentException('Offset can be used only with single file path, not with pattern');
}

foreach ($uri as $filePath) {
foreach ($path as $filePath) {
$extractors[] = new ParquetExtractor(
$filePath,
$options,
Expand All @@ -45,7 +45,7 @@ function from_parquet(
}

return new ParquetExtractor(
\is_string($uri) ? Path::realpath($uri) : $uri,
\is_string($path) ? Path::realpath($path) : $path,
$options,
$byte_order,
$columns,
Expand Down
13 changes: 13 additions & 0 deletions web/landing/assets/controllers/all_links_external_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Controller} from '@hotwired/stimulus';

/* stimulusFetch: 'lazy' */
export default class extends Controller
{
connect()
{
this.element.querySelectorAll('a').forEach(link => {
link.target = '_blank';
link.rel = 'noopener';
});
}
}
24 changes: 24 additions & 0 deletions web/landing/assets/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,28 @@ code {

#blog-post hr {
@apply text-blue-100 my-4 border-t-2 rounded;
}

#example-description h1 {
@apply font-bold text-2xl;
}

#example-description h2 {
@apply font-bold text-xl mt-4;
}

#example-description h1, h2, ul, hr, p {
@apply mb-3;
}

#example-description ul {
@apply list-disc pl-6;
}

#example-description hr {
@apply text-blue-100 my-4 border-t-2 rounded;
}

#example-description code {
@apply text-orange-100;
}
2 changes: 1 addition & 1 deletion web/landing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"monolog/monolog": "^3.5",
"symfony/monolog-bundle": "^3.10",
"coduo/php-humanizer": "^4.0",
"twig/markdown-extra": "^3.8",
"twig/markdown-extra": "^3.11",
"twig/extra-bundle": "^3.8",
"league/commonmark": "^2.4"
},
Expand Down
14 changes: 7 additions & 7 deletions web/landing/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Flow\Website\Controller;

use Flow\Website\Service\{Examples};
use Flow\Website\Service\Examples;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class DefaultController extends AbstractController
final class ExamplesController extends AbstractController
{
public function __construct(
private readonly Examples $examples,
Expand All @@ -30,19 +30,12 @@ public function example(string $topic, string $example) : Response
'examples' => $examples,
'currentTopic' => $topic,
'currentExample' => $example,
'description' => $this->examples->description($currentTopic, $currentExample),
'code' => $this->examples->code($currentTopic, $currentExample),
'output' => $this->examples->output($currentTopic, $currentExample),
]);
}

#[Route('/', name: 'main')]
public function main() : Response
{
return $this->render('main/index.html.twig', [
'topics' => $this->examples->topics(),
]);
}

#[Route('/{topic}/', name: 'topic', priority: 10)]
public function topic(string $topic) : Response
{
Expand All @@ -57,6 +50,7 @@ public function topic(string $topic) : Response
'examples' => $examples,
'currentTopic' => $currentTopic,
'currentExample' => $currentExample,
'description' => $this->examples->description($currentTopic, $currentExample),
'code' => $this->examples->code($currentTopic, $currentExample),
'output' => $this->examples->output($currentTopic, $currentExample),
]);
Expand Down
26 changes: 26 additions & 0 deletions web/landing/src/Flow/Website/Controller/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Flow\Website\Controller;

use Flow\Website\Service\{Examples};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class HomeController extends AbstractController
{
public function __construct(
private readonly Examples $examples,
) {
}

#[Route('/', name: 'home')]
public function home() : Response
{
return $this->render('main/index.html.twig', [
'topics' => $this->examples->topics(),
]);
}
}
11 changes: 11 additions & 0 deletions web/landing/src/Flow/Website/Service/Examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public function code(string $topic, string $example) : string
return \file_get_contents($path);
}

public function description(string $topic, string $example) : ?string
{
$path = \sprintf('%s/topics/%s/%s/description.md', \realpath($this->examplesPath), $topic, $example);

if (false === \file_exists($path)) {
return null;
}

return \file_get_contents($path);
}

/**
* @return string[]
*/
Expand Down
2 changes: 1 addition & 1 deletion web/landing/templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<body class="scroll-smooth bg-black text-white">
<header class="py-2 text-white bg-blue-200">
<div class="mx-auto max-w-screen-xl px-4 flex items-center justify-between">
<a href="{{ path('main') }}">
<a href="{{ path('home') }}">
<img src="{{ asset('images/logo.svg') }}" alt="flow php" width="176" height="48">
</a>

Expand Down
12 changes: 12 additions & 0 deletions web/landing/templates/example/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

{% block main %}
<div class="py-10 px-2 sm:px-4 mx-auto max-w-screen-xl" data-hx-boost="true">
<h2 class="mb-4 text-2xl font-semibold tracking-wide">Examples:</h2>
<nav class="font-medium text-center bg-orange-100 rounded">
<ul class="flex whitespace-nowrap overflow-auto justify-between">
{% for topic in topics %}
Expand Down Expand Up @@ -37,7 +38,18 @@
</nav>

<div id="example" class="-mt-36 pt-36">
{% if description %}
<h2 class="text-xl mt-5 mb-5">Description</h2>
<article id="example-description"
class="rounded px-4 pt-4 overflow-auto shadow-2xl shadow-gray rounded border-gray border-2 relative mb-10"
{{ stimulus_controller('all_links_external') }}
>
{{ description|markdown_to_html }}
</article>
{% endif %}

{% apply spaceless %}
<h2 class="text-xl mt-5 mb-5">Code</h2>
<pre class="rounded p-4 overflow-auto shadow-2xl shadow-gray rounded border-gray border-2 relative">
<button class="absolute top-0 right-0 bg-orange-100 rounded px-4 leading-9 [&.copied]:before:content-['Copied!'] before:absolute before:-translate-x-24" title="copy code" data-clipboard-target="#code" {{ stimulus_controller('clipboard') }}>
<img src="{{ asset('images/icons/copy.svg') }}" alt="copy code" width="20" height="20" class="inline">
Expand Down

0 comments on commit deede5a

Please sign in to comment.