Skip to content

Commit 9f11535

Browse files
Proof of concept for generating endpoint docs from API
1 parent ce48ac6 commit 9f11535

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
composer.lock
3+
vendor

Phakefile.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
3+
4+
if ( ! getenv( 'WP_API_DEV_DOMAIN' ) ) {
5+
echo "`WP_API_DEV_DOMAIN` environment variable must be set to build the site.";
6+
exit(1);
7+
}
8+
9+
function render( $path, $binding ) {
10+
$m = new Mustache_Engine;
11+
$template = file_get_contents( __DIR__ . "/templates/$path" );
12+
return $m->render( $template, $binding );
13+
}
14+
15+
desc( 'Build the endpoint documentation.' );
16+
task( 'endpoint-list', function( $app ){
17+
18+
$endpoints = array(
19+
'comments' => array(
20+
'name' => 'Comments',
21+
'schema_path' => '/wp-json/wp/v2/comments/schema',
22+
),
23+
);
24+
25+
$dev_domain = rtrim( getenv( 'WP_API_DEV_DOMAIN' ), '/' );
26+
27+
foreach( $endpoints as $file_name => $attributes ) {
28+
29+
$response = Requests::get( $dev_domain . $attributes['schema_path'] );
30+
if ( 200 !== $response->status_code ) {
31+
echo "Error fetching schema (HTTP code {$response->status_code})";
32+
exit(1);
33+
}
34+
35+
$response_data = json_decode( $response->body );
36+
// Prepare the data because Mustache is opinionated
37+
$properties = array();
38+
if ( ! empty( $response_data->properties ) ) {
39+
foreach( $response_data->properties as $name => $args ) {
40+
$properties[] = array(
41+
'name' => $name,
42+
'description' => ! empty( $args->description ) ? $args->description : '',
43+
'type' => ! empty( $args->type ) ? $args->type : '',
44+
'context' => ! empty( $args->context ) ? implode( $args->context, ', ' ) : '',
45+
);
46+
}
47+
}
48+
49+
$data = array(
50+
'name' => $attributes['name'],
51+
'schema_properties' => $properties,
52+
);
53+
file_put_contents( dirname( __FILE__ ) . '/_includes/routes/' . $file_name . '.md', render( 'endpoint.mustache', $data ) );
54+
}
55+
56+
});
57+
58+
desc( 'Build the site.' );
59+
task( 'default', 'endpoint-list' );

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
These files comprise v2.wp-api.org. The endpoint documentation is programmatically, but manually, generated from the API.
2+
3+
### Setup
4+
5+
1. Install [Composer](http://getcomposer.org/).
6+
7+
2. Install dev dependencies with `composer install --dev`
8+
9+
3. Build the endpoint documentation:
10+
11+
```bash
12+
vendor/bin/phake
13+
```

composer.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"require-dev": {
3+
"jaz303/phake": "0.5.1",
4+
"mustache/mustache": "~2.4",
5+
"rmccue/requests": "dev-master"
6+
}
7+
}

templates/endpoint.mustache

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## {{ name }}
2+
3+
### Schema
4+
5+
| Property | Type | Description | Context |
6+
| :------- | :--- | :---------- | :------ |
7+
{{#schema_properties}}
8+
| `{{ name }}` | {{ type }} | {{ description }} | {{ context }} |
9+
{{/schema_properties}}
10+
11+
### List All {{ name }}
12+
13+
### Create A {{ name }}
14+
15+
### Retrieve A {{ name }}
16+
17+
### Update A {{ name }}
18+
19+
### Delete A {{ name }}

0 commit comments

Comments
 (0)