Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .castor/website.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

use Symfony\Component\Console\Input\InputOption;

// The demo website: a static page, in website/public, that audits a document
// with the validator, run by PHP built as WebAssembly. See website/README.md.
// The demo website: static pages, in website/public, that audit a document
// with the validator, and run the JSON-LD algorithms on one, with PHP built as
// WebAssembly. See website/README.md.

#[AsTask(description: 'Installs the dependencies of the demo website')]
function install(): void
Expand Down Expand Up @@ -75,7 +76,7 @@ function build(
]));

// What is embedded takes some of the memory of the WebAssembly module:
// only what bin/audit.php reads
// only what bin/audit.php and bin/playground.php read
fs()->mirror($website, $app, finder()
->in(["{$website}/assets/images", "{$website}/bin", "{$website}/src", "{$website}/templates", "{$website}/vendor"])
// Composer copies the directories of the repository that Git ignores too
Expand All @@ -94,7 +95,13 @@ function check(): void
io()->title('Checking the website');

check_stack(wasmStack());
test_page('website/tests/page.test.mjs', 'website/tests/format.test.mjs');
test_page(
'website/tests/page.test.mjs',
'website/tests/format.test.mjs',
'website/tests/loadUrl.test.mjs',
'website/tests/permalink.test.mjs',
'website/tests/playground.test.mjs',
);
}

#[AsTask(description: 'Serves the demo website')]
Expand Down Expand Up @@ -131,7 +138,8 @@ function wasmStack(): Stack
->php('8.5.12')
// What the library, Twig and league/commonmark require
->extensions('ctype', 'dom', 'filter', 'libxml', 'mbstring')
// Filled by build(), assets/php.js runs /app/bin/audit.php
// Filled by build(), assets/php.js runs /app/bin/audit.php and
// /app/bin/playground.php
->embed('website/build/app', at: '/app')
->targets(Target::Web)
->outputDir('website/public/build')
Expand Down
7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ The latest figures, and what each of them measures, are in [BENCHMARKS.md](BENCH

### Demo website

These commands build the [demo website](website/README.md), that runs the validator in the browser:
These commands build the [demo website](website/README.md), that runs the validator and the JSON-LD
algorithms in the browser:

Command | Description
---- | -----
`castor website:build` | Builds the demo website: its assets, and PHP as WebAssembly with the validator
`castor website:check` | Checks, in node, that what is built boots, and audits a document
`castor website:build` | Builds the demo website: its assets, and PHP as WebAssembly with the library
`castor website:check` | Checks, in node, that what is built boots, audits a document, and runs the JSON-LD algorithms
`castor website:serve` | Serves the demo website

### Vocabulary generation
Expand Down
3 changes: 3 additions & 0 deletions docs/json-ld.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ The library implements the W3C
- [Deserialization to RDF](https://www.w3.org/TR/json-ld11-api/#deserialize-json-ld-to-rdf-algorithm) (`toRdf`): `JoliCode\StructuredData\JsonLd\Algorithms\Rdf\ToRdfConverter`
- [Serialization from RDF](https://www.w3.org/TR/json-ld11-api/#serialize-rdf-as-json-ld-algorithm) (`fromRdf`): `JoliCode\StructuredData\JsonLd\Algorithms\Rdf\FromRdfConverter`

> [!TIP]
> Try them in your browser, in the [JSON-LD playground](https://structured-data-validator.jolicode.com/playground/).

## Usage

Create an instance of the algorithm and pass it the JSON-LD document you want to convert.
Expand Down
80 changes: 68 additions & 12 deletions website/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
> [!TIP]
> This demo website is deployed under https://structured-data-validator.jolicode.com/

A static page that audits a document with the validator, in the browser: PHP is built as
WebAssembly, with [lyrixx/php-wasm-builder](https://github.com/lyrixx/php-wasm-builder), and the
library of this repository embedded in it. GitHub Pages serves `public/`, deployed from `main` by
Static pages, that run the library in the browser: PHP is built as WebAssembly, with
[lyrixx/php-wasm-builder](https://github.com/lyrixx/php-wasm-builder), and the library of this
repository embedded in it. GitHub Pages serves `public/`, deployed from `main` by
`.github/workflows/website.yml`.

It comes from
- The **homepage**, at `/`, has the form of the validator, and its examples.
- The **validator**, at `/validator/`, audits a document, and shows the result.
- The **JSON-LD playground**, at `/playground/`, runs the JSON-LD algorithms on a document: expand,
compact, flatten, frame, to RDF and from RDF.

The validator comes from
[jolicode/structured-data-validator-demo](https://github.com/jolicode/structured-data-validator-demo),
without its server: the editor and the tree of the properties are its ones, with another look.
Fetching a URL needs a server, and is not here: a page can not read another website. Pretty-printing
Expand All @@ -17,14 +22,63 @@ as its `bin/format-document.js` does in node.

## How it works

- `public/index.html` is the homepage. `assets/app.js` gives the document of its form, or one of
`assets/examples.js`, to PHP, and shows what it answers in place of the form.
- `bin/audit.php` is what PHP runs: it audits the document, and renders `templates/result.html.twig`,
the editor and the result, with Twig.
- `assets/php.js` loads PHP, and runs `bin/audit.php`.
- The footer of the homepage is the JoliCode open-source one,
[jolicode/oss-theme](https://github.com/jolicode/oss-theme): `castor website:build` downloads it
to `public/build/joli-footer.html`, and the homepage loads it.
`assets/php.js` loads PHP, and runs a script of `bin/`: the request is on its standard input, and its
answer on its standard output, as JSON.

### The state of the pages

The validator and the playground share a state: the document, and what the playground adds to it
(its tab, its context, its frame, its options). `assets/pageState.js` writes it:

- in the fragment of the URL of the page, compressed by `assets/permalink.js`: a link to the page
opens it as it is, and its "Copy link" button copies this link;
- in the links of the menu: the validator opens the playground with its document, and the
playground the validator, that validates what was typed there.

A state too long for a link is kept in the session storage of the tab, and the fragment is `#local`:
the pages still open each other, and reload, but "Copy link" is disabled.

### The validator

- `public/index.html` is the homepage. `assets/home.js` opens the page of the validator with the
document of its form, or one of `assets/examples.js`. Nothing is audited there: PHP is only
fetched ahead.
- `public/validator/index.html` is the page of the validator. It holds the frame of the result, the
editor and the toolbar, that shows as the page opens: PHP has to start before it answers, and the
document is there meanwhile. `assets/controllers/result_controller.js` runs the page: it gives
the document the page is opened with to PHP, and shows what it answers in this frame. Without a
document, it goes back to the homepage. The document is audited again as it is typed, once the
visitor pauses, as in the playground: PHP runs in the thread of the page, so a document that
takes long to audit waits for a longer pause.
- `bin/audit.php` is what PHP runs: it audits the document, and renders what is found with Twig,
the counts of the toolbar, `templates/counts.html.twig`, and the entities,
`templates/result.html.twig`. The settings of the audit are the ones of who opens the page: they
are not in the link.

### The playground

- `public/playground/index.html` is the page, and `assets/controllers/playground_controller.js` what
runs it: the algorithm of the selected tab runs again as the visitor types, on the document, with
the context or the frame it needs, and the options it reads.
- `bin/playground.php` is what PHP runs: an algorithm, on what it is given. It answers with the
output, or the error code of the specification.
- `assets/jsonLd.js` loads the remote contexts. PHP has no network in the browser: when a document
needs a context that is not there, `bin/playground.php` answers that it is missing,
`assets/loadUrl.js` fetches it, and PHP is asked again with it. The website of the context has to
allow it, with CORS headers. The schema.org context is the one the library ships, and is never
fetched.
- `assets/playgroundExamples.js` holds the examples.

Canonicalization (RDFC-1.0) is not there: the library does not implement it.

### Every page

- `assets/editor.js` is the CodeMirror editor of the validator and of the playground.
- The header, with its menu, is written in each page: there is no layout to share it. The links are
relative: the website works under any path.
- The footer is the JoliCode open-source one, [jolicode/oss-theme](https://github.com/jolicode/oss-theme):
`castor website:build` downloads it to `public/build/joli-footer.html`, and the homepage and the
playground load it.

## Build it

Expand All @@ -40,3 +94,5 @@ Command | Description
`composer.json` declares the parent directory as a path repository: the website is built with the
code of the working directory, not with a release. Run `castor website:build` again after a change:
only the last step of the build of PHP runs again.

A test file of `tests/` runs once it is listed in the `check()` task.
60 changes: 0 additions & 60 deletions website/assets/app.js

This file was deleted.

28 changes: 28 additions & 0 deletions website/assets/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// The "Copy" buttons of the pages: the button says whether it worked, for a
// moment.

const FEEDBACK_DELAY = 1500;

// The label of each button, and the timeout that brings it back: a second
// click comes while the button still says "Copied"
const labels = new WeakMap();
const timeouts = new WeakMap();

export async function copyWithFeedback(text, button) {
if (!labels.has(button)) {
labels.set(button, button.textContent);
}

try {
await navigator.clipboard.writeText(text);
button.textContent = "Copied";
} catch {
button.textContent = "Not copied";
}

clearTimeout(timeouts.get(button));
timeouts.set(
button,
setTimeout(() => (button.textContent = labels.get(button)), FEEDBACK_DELAY),
);
}
Loading