Gitdoc

Building pages from a composer.json file

This plugin collects and creates pages that contain content to the official Github documentation README of dependencies in a composer file.

generator.php

This plugin requires that the generator.php has to be run from the PHP command line in a terminal environment.

After modifying the generator.php file, which can be found in folder <GRAV Root folder>/user/plugins/gitdoc, you're good to go.

What has to be modified

define( "PAGE_FOLDER", PAGE_ROOT . "/05.slim/04.composer-package-documentation" ); /* (1) */
define( "COMPOSER_JSON", dirname( __FILE__ ) . "/composer/composer.json" ); /* (2) */
  1. This is the root folder in the /user/pages/ under which the pages will be generated.
  2. The location of the composer.json file. You can copy your composer.json file to be examined and generated to this folder. You can simply create a folder composer under the root of your GRAV folder.

Generating the files

Now you do from the root folder of the plugin in a terminal:

php generator.php

Because of caching in GRAV, it can take a while before the pages show up the first time.

Complete Source

<?php
define( "GRAV_ROOT", dirname( __FILE__, 4 ) );
define( "PAGE_ROOT", GRAV_ROOT . "/user/pages" );

define( "PAGE_FOLDER", PAGE_ROOT . "/05.slim/04.composer-package-documentation" );
define( "COMPOSER_JSON", dirname( __FILE__ ) . "/composer/composer.json" );

$template = "
---
title: '%s'
---

%s(0)
";

echo "\n--- Grav Root ---\n";
var_dump( GRAV_ROOT );
echo "\n--- Page Root ---\n";
var_dump( PAGE_ROOT );
echo "\n--- Page Folder ---\n";
var_dump( PAGE_FOLDER );
echo "\n--- Composer File ---\n";
var_dump( COMPOSER_JSON );

/* Step 1. read the composer.json */

echo "\n--- Read composer.json ---\n";

if ( !file_exists( COMPOSER_JSON ) ) {
    echo "\nComposer file not found\n";
    echo "Exit!\n";
    exit;
}

$composerData = file_get_contents( COMPOSER_JSON );

$composer = json_decode( $composerData );

if ( !isset( $composer->require ) ) {
    echo "No requirements found\n";
    echo "Exit!\n";
    exit;
}

/* Step 2. clear the root folder contents */
echo "\n--- Deleting folder contents ---\n";
delete_files( PAGE_FOLDER );
echo "\n (status) Folder contents deleted\n";

/* Step 3. iterate the composer.json */
echo "\n--- Iterate composer.json ---\n";

$counter = 1;
foreach ( $composer->require as $package => $version ) {
    echo "(status) Processing " . $package . "\n";

    $dirname = PAGE_FOLDER . "/" . str_pad( $counter, 2, "0", STR_PAD_LEFT ) . "." . str_replace( "/", "_", $package );
    $filename = "default.en.md";

    $content = sprintf($template, $package, $package);

    if ( is_dir( $dirname ) === false ) {
        mkdir( $dirname, 777, true );
    }

    $file = fopen( $dirname . "/" . $filename, "w" );
    fwrite( $file, $content );
    fclose( $file );

    $counter ++;

}

/* Step 4. ready */
echo "\n----------------------------------------------------------------------------\n";
echo "(finised) All packages have been processed, upload it to your GRAV CMS pages\n";
echo "----------------------------------------------------------------------------\n";

function delete_files( $target ) {
    if ( is_dir( $target ) ) {
        $files = glob( $target . "*", GLOB_MARK ); //GLOB_MARK adds a slash to directories returned

        foreach ( $files as $file ) {
            delete_files( $file );
        }

        rmdir( $target );
    } elseif ( is_file( $target ) ) {
        unlink( $target );
    }
}
Last update: March 23rd at 10:55pm