Skip to content

Instantly share code, notes, and snippets.

@Girgias
Last active May 27, 2024 13:32
Show Gist options
  • Save Girgias/8fc473b7fda9007407ca85df32ad1b1d to your computer and use it in GitHub Desktop.
Save Girgias/8fc473b7fda9007407ca85df32ad1b1d to your computer and use it in GitHub Desktop.
Script to convert custom PHPDOC markup to standard DocBook 5.2 markup
<?php
/**
* Copyright 2024 Gina Peter Banyard
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
function processClassDoc($file)
{
$content = file_get_contents($file);
if (!str_contains($content, 'phpdoc:classref') && !str_contains($content, 'phpdoc:exceptionref')) {
// Skip
return;
}
$role = "class";
if (str_contains($content, 'phpdoc:exceptionref')) {
$role = "exception";
}
$content = str_replace('<phpdoc:classref', '<reference', $content);
$content = str_replace('</phpdoc:classref>', '</reference>', $content);
$content = str_replace('<phpdoc:exceptionref', '<reference', $content);
$content = str_replace('</phpdoc:exceptionref>', '</reference>', $content);
$content = str_replace('xmlns:phpdoc="http://php.net/ns/phpdoc"', 'role="'.$role.'"', $content);
file_put_contents($file, $content);
}
/* Path to the root of EN extension reference tree */
function handleClassDir($dir) {
foreach (new DirectoryIterator($dir) as $docs) {
if ($docs->isDot() || !$docs->isDir() || !$docs->isReadable()) {
continue;
}
if (str_ends_with($docs->getPathname(), 'mongodb') | str_ends_with($docs->getPathname(), 'mongodb/driver')) {
handleClassDir($docs->getPathname());
}
foreach (new DirectoryIterator($docs->getPathname()) as $reference_pages) {
if ($reference_pages->isDot() || $reference_pages->isDir() || !$reference_pages->isReadable()) {
continue;
}
processClassDoc($reference_pages->getPathname());
}
}
}
define('DOCROOT_EN', dirname(__DIR__, 1) . '/en/');
$fileCount = 0;
$directoryToSearch = DOCROOT_EN . 'reference';
handleClassDir($directoryToSearch);
$directoryToSearch = DOCROOT_EN . 'language/predefined';
foreach (new DirectoryIterator($directoryToSearch) as $reference_pages) {
if ($reference_pages->isDot() || $reference_pages->isDir() || !$reference_pages->isReadable()) {
continue;
}
processClassDoc($reference_pages->getPathname());
}
$directoryToSearch = DOCROOT_EN . 'language/predefined/attributes';
foreach (new DirectoryIterator($directoryToSearch) as $reference_pages) {
if ($reference_pages->isDot() || $reference_pages->isDir() || !$reference_pages->isReadable()) {
continue;
}
processClassDoc($reference_pages->getPathname());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment