ApiOutput.php

<?php

namespace Tlf\Scrawl;

class BashApiLexer extends \Tlf\Scrawl\ExtensionType\DefaultExt {

    // public function onBuildStart(){
        // $this->scrawl->addExtension('mdverb', [$this,'handleMdVerb']);
    // }
    //

    protected $paths = [];

    public function onSourceFileFound($file){
        if ($file->ext!='bash')return;

        // $this->paths[$file->path] = $file->relPath;
        
        $lexer = new \Tlf\Lexer();
        $lexer->addGrammar(new \Tlf\Lexer\BashGrammar());
        

        static $i=0;

        echo "Start $i ".$file->relPath;

        // if ($i > 5)return;
        $ast = $lexer->lex($file->path);
        $ast->set('relPath', $file->relPath);
        $this->scrawl->addOutput('bashast', $file->path, $ast->getTree());

        echo "  end\n";

        $i++;
    }

    public function onSourceFilesDone(){
        $files = $this->scrawl->getOutputs('bashast');
        if (!is_array($files))return;

        foreach ($files as $file=>$ast){
            $funcOutput = '';
            $functions = [];
            if (isset($ast['function']))$functions = $ast['function'];
            else echo "\n\nNo functions found in '$file'\n\n";
            ob_start();
            $File = (object)$ast;
            require(__DIR__.'/template/functionList.php');
            // foreach ($functions as $index=>$function){
                // $funcOutput .= print_r($function,true)."\n";
                // $Function = (object)$function;
                // require(dirname(__DIR__,2).'/Template/functionList.php');
            // }
            $funcOutput = ob_get_clean();
            $destFile = 'api/'.$File->relPath.'.md';
            $this->scrawl->addOutput('file', $destFile, $funcOutput);
        }
    }
}