| 1 |
<?php |
| 2 |
/* GCompris - generate-doc.php |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2024 Bruno Anselme <[email protected]> |
| 5 |
* |
| 6 |
* Authors: |
| 7 |
* Bruno Anselme <[email protected]> |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
* |
| 11 |
* Usage : |
| 12 |
* $ php generate-doc.php |
| 13 |
* Build GCompris-Teachers.md documentation file |
| 14 |
*/ |
| 15 |
$dirs = Array(".", "components", "dialogs", "panels", "views", "singletons", "activities"); |
| 16 |
$classes = Array(); |
| 17 |
$baseDir = "../"; |
| 18 |
$filePattern = "*"; |
| 19 |
$outputFile = "GCompris-Teachers.md"; |
| 20 |
|
| 21 |
function readAndClean($file) { |
| 22 |
$content = file_get_contents($file); |
| 23 |
$content = preg_replace("/\/\*.*\*\//s","",$content); // Remove result_ /* */ comments |
| 24 |
$content = preg_replace("/\/\/.*$/m","",$content); // Remove // comments |
| 25 |
return $content; |
| 26 |
} |
| 27 |
|
| 28 |
// Find qml objects names inside qml files |
| 29 |
function inspectClass($parentClassName, $parentFileName, $depth) { |
| 30 |
global $classes, $output; |
| 31 |
$content = readAndClean($parentFileName); // Read qml file |
| 32 |
foreach ($classes as $className => $fileName) { // Loop on each class |
| 33 |
$pos = 0; |
| 34 |
$count = 0; |
| 35 |
while (!($pos === false)) { // === is mandatory here. pos is either false, either an integer |
| 36 |
$pos = strpos($content, "$className {", $pos + 1); |
| 37 |
if (!($pos === false)) |
| 38 |
$count++; |
| 39 |
} |
| 40 |
if (($count) && ($parentClassName != $className)) { |
| 41 |
$output .= str_repeat(" ",$depth)." * [$className]($fileName) ($count)\n"; |
| 42 |
inspectClass($className, $fileName, $depth + 1); // Recursive call to inspect this included class |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Read directories tree and delete some chars |
| 48 |
function readTree() { |
| 49 |
global $baseDir; |
| 50 |
$out = shell_exec("tree -d --charset unicode $baseDir"); |
| 51 |
$out = preg_replace("/[\|\-\ `]{4}/"," ", $out); // Remove tree chars |
| 52 |
$out = preg_replace("/ (\w)/"," * $1", $out); // Add * before directory name |
| 53 |
$out = preg_replace("/\d+ \* directories\n/s","", $out); // Remove last line from tree command |
| 54 |
$out = preg_replace("/\.\.\//","**teachers/**", $out); // Bold main directory |
| 55 |
return $out; |
| 56 |
} |
| 57 |
|
| 58 |
// Program starts here |
| 59 |
foreach ($dirs as $dir) { // Build classes list from directories file's names (ending with .qml) |
| 60 |
foreach (glob("$baseDir$dir/$filePattern") as $file) { // For each file in dir |
| 61 |
if ($file == '.' || $file == '..') continue; |
| 62 |
$classes[basename($file, ".qml")] = $file; // Keep class name and file name in $classes array |
| 63 |
} |
| 64 |
} |
| 65 |
foreach ($dirs as $dir) { // Build classes list from directories file's names (ending with .qml) |
| 66 |
foreach (glob("$baseDir$dir/*/$filePattern") as $file) { // For each file in subdir |
| 67 |
if ($file == '.' || $file == '..') continue; |
| 68 |
$classes[basename($file, ".qml")] = $file; // Keep class name and file name in $classes array |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$output = ""; |
| 73 |
$output .= "# GCompris teachers - Generated documentation.\n"; |
| 74 |
$output .= "## Qml object nesting hierarchy.\n"; |
| 75 |
$output .= " * [Main](".$classes["Main"].")\n"; |
| 76 |
$depth = 1; // Depth controls indentation |
| 77 |
inspectClass("Main", $classes["Main"], $depth); // Enter inspectClass from Main object in Main.qml |
| 78 |
|
| 79 |
// $output .= "\n## Teachers directory hierarchy.\n"; |
| 80 |
// $output .= readTree(); |
| 81 |
|
| 82 |
$output .= "\nFile generated on ".date('Y/m/d H:i:s e', time())." with ".basename(__FILE__)."\n"; |
| 83 |
|
| 84 |
file_put_contents($outputFile, $output); |
| 85 |
print("Generate file: $outputFile\n"); |
| 86 |
?> |