-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEBUG: This is the tool I used to re-generate the root-manifest file,…
… altered slightly from a0a6349
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* Usage: `cd gutenberg/docs && php generate.php` | ||
* PHP 7.1+ recommended | ||
*/ | ||
/** | ||
* Get the markdown title from the first h1 tag, e.g. `# text` | ||
* | ||
* @param string $contents | ||
* | ||
* @return string | ||
*/ | ||
function getTitle(string $contents = '') | ||
{ | ||
// Attempt to extract a title | ||
if (preg_match('/^#\s(.+)/m', $contents, $matches)) { | ||
$title = $matches[1]; | ||
} | ||
return $title ?? null; | ||
} | ||
/** | ||
* Get a slug from the given file path | ||
* | ||
* @param string $filePath | ||
* | ||
* @return string | ||
*/ | ||
function getSlug(string $filePath) | ||
{ | ||
// Get the file path | ||
$path = __DIR__ . '/../' . $filePath; | ||
// Get the markdown file name | ||
$fileName = basename($path, '.md'); | ||
// Get the appropriate file path | ||
$slug = ($fileName === 'README' || $fileName == 'readme' ) ? basename(dirname($path)) : $fileName; | ||
return ( 'docs' == $slug ) ? 'handbook' : $slug; | ||
} | ||
/** | ||
* Get the "parent" setting. The parent a docs-relative path to the | ||
* containing folder (or null, if it is a top-level item) | ||
* | ||
* @param string $filePath | ||
* | ||
* @return string | ||
*/ | ||
function getParent(string $filePath) | ||
{ | ||
// Sanitises the path string | ||
$path = str_replace( | ||
[ | ||
__DIR__ . '/../docs/', | ||
'.md', | ||
'/readme', | ||
'/README', | ||
], | ||
'', | ||
$filePath | ||
); | ||
// If a slash exists in the path... | ||
if (stripos($path, '/')) { | ||
// Explode the path into path segments | ||
$segments = explode('/', $path); | ||
// Pop the last item off the array of segments | ||
array_pop($segments); | ||
// Implode it again and use it as the $parent | ||
$parent = basename( implode('/', $segments) ); | ||
} | ||
return $parent ?? null; | ||
} | ||
/** | ||
* Process a table-of-contents array into a manifest file | ||
* | ||
* @param array $level | ||
* | ||
* @return array | ||
*/ | ||
function process(array $level) | ||
{ | ||
$manifest = []; | ||
foreach ($level as $i => $child) { | ||
foreach ($child as $file => $children) { | ||
// Build an absolute path to the file | ||
$path = __DIR__ . '/../' . $file; | ||
// Append to the manifest | ||
$title = getTitle(file_get_contents($path)); | ||
if ( ! $title ) { | ||
$title = ucwords( basename( $path ) ); | ||
if ( 'Readme.md' == $title ) { | ||
$title = ucwords( basename( dirname( $path ) ) ); | ||
} | ||
} | ||
$manifest[] = [ | ||
'title' => $title, | ||
'slug' => getSlug($file), | ||
'markdown_source' => sprintf('https://raw.githubusercontent.com/WordPress/gutenberg/master/%s', $file), | ||
'parent' => getParent($path), | ||
]; | ||
// Run the process for children items | ||
if (!empty($children) && is_array($children)) { | ||
$additions = process($children); | ||
// Merge the children into the containing manifest | ||
$manifest = array_merge($manifest, $additions); | ||
} | ||
if (is_string($children)) { | ||
// @todo | ||
// The TOC.json format includes tokens that could be replaced | ||
// in the future, for easily embedding auto-generated content | ||
// into the overall manifest. | ||
} | ||
} | ||
} | ||
return $manifest; | ||
} | ||
// Decode the TOC JSON | ||
$contents = json_decode(file_get_contents(__DIR__ . '/toc.json'), true); | ||
// Process the contents | ||
$response = process($contents); | ||
// Push out to root-manifest.json | ||
file_put_contents(__DIR__ . '/root-manifest.json', json_encode($response, JSON_PRETTY_PRINT)); |