mandatory = true; $componentOption->shorthelp = "The name of the component."; $params->registerOption( $componentOption ); $targetOption = new ezcConsoleOption( 't', 'target', ezcConsoleInput::TYPE_STRING ); $targetOption->mandatory = true; $targetOption->shorthelp = "The directory to where the generated documentation should be written."; $params->registerOption( $targetOption ); $versionOption = new ezcConsoleOption( 'v', 'version', ezcConsoleInput::TYPE_STRING ); $versionOption->mandatory = true; $versionOption->shorthelp = "The version of the component that should be read. E.g. trunk, 1.0rc1, etc."; $params->registerOption( $versionOption ); $outputFilenameOption = new ezcConsoleOption( 'o', 'outputfilename', ezcConsoleInput::TYPE_STRING ); $outputFilenameOption->shorthelp = "The name of the file to render to."; $params->registerOption( $outputFilenameOption ); $filenameOption = new ezcConsoleOption( 'f', 'filename', ezcConsoleInput::TYPE_STRING ); $filenameOption->mandatory = true; $filenameOption->shorthelp = "The name of the file to render."; $params->registerOption( $filenameOption ); // Process console parameters try { $params->process(); } catch ( ezcConsoleOptionException $e ) { print( $e->getMessage(). "\n" ); print( "\n" ); echo $params->getSynopsis() . "\n"; foreach ( $params->getOptions() as $option ) { echo "-{$option->short}, --{$option->long}\t {$option->shorthelp}\n"; } echo "\n"; exit(); } $component = $params->getOption( 'component' )->value; $filename = $params->getOption( 'filename' )->value; $version = $params->getOption( 'version' )->value; $outputFilename = $params->getOption( 'outputfilename' )->value; $output = getRstOutput( $filename ); if ( $output === false ) { echo " FAILED\n"; exit( 1 ); } $output = removeHeaderFooter( $output ); $output = addNewHeader( $component, $output, $filename ); $output = addExampleLineNumbers( $output ); $output = addLinks( $component, $output, $version ); $output = addNewFooter( $output ); $targetDir = $params->getOption( 'target' )->value; if ( $outputFilename ) { $filename = $outputFilename; } else { $filenameParts = explode( '/', $filename ); $filename = array_splice( $filenameParts, -1 ); $filename = basename( $filename[0], '.txt' ); $filename = "{$component}_$filename.html"; } //echo "$targetDir/{$component}_$filename.html\n"; file_put_contents( "$targetDir/$filename", $output ); echo " OK\n"; function getRstOutput( $fileName ) { exec( "rst2html $fileName", $output, $returnCode ); return $returnCode == 0 ? join( "\n", $output ) : false; } function removeHeaderFooter( $output ) { $output = substr( $output, strpos( $output, '' ) + 7 ); $output = preg_replace( '@

eZ components - [A-Za-z]+

@', '', $output ); $output = preg_replace( '@<\/body>.*@ms', '', $output ); return $output; } function addNewFooter( $output ) { return $output . "\n". "
Last updated: ". date( 'D, d M Y' ) . "
"; } function addNewHeader( $component, $output, $filename ) { $exploded = explode( '/', $filename ); $filename = array_splice( $exploded, -1 ); $filename = basename( $filename[0], '.txt' ); $title = ucfirst( $filename ); $outputHeader = <<

$component: $title

[ Tutorial ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]
FOO; return $outputHeader . $output; } function addLinks( $component, $output, $version ) { // $base = "http://ez.no/doc/components/view/$version/(file)/$component/"; $base = "$component/"; $output = preg_replace( '@(ezc[A-Z][a-zA-Z0-9]+)::\$([A-Za-z0-9]+)@', "\\0", $output ); $output = preg_replace( "@(ezc[A-Z][a-zA-Z0-9]+)::([A-Za-z0-9_]+)(?=\()@", "\\0", $output ); $output = preg_replace( "@(ezc[A-Z][a-zA-Z0-9]+)-(>|\>)([A-Za-z0-9_]+)(?=\()@", "\\0", $output ); $output = preg_replace( "@(ezc[A-Z][a-zA-Z0-9]+)::([A-Z_]+)\\b@", "\\0", $output ); $output = preg_replace( "@(?])(ezc[A-Z][a-zA-Z0-9]+)@", "\\0", $output ); $output = preg_replace( "@()(ezc[A-Z][a-zA-Z0-9]+)(\()@", "\\1\\2\\3", $output ); $output = preg_replace( "@(ezc[A-Z][a-zA-Z]+)(::)([A-Z_]+)@", "\\1::\\3", $output ); $output = preg_replace( "@()(ezc[A-Z][a-zA-Z0-9]+)()@", "\\1\\2\\3", $output ); $output = preg_replace( "@()(ezc[A-Z][a-zA-Z0-9]+)(::)([A-Za-z]+)()@", "\\1\\2::\\4\\5", $output ); $output = preg_replace( "@()(ezc[A-Z][a-zA-Z0-9]+Exception)(\ \\$)@", "\\1\\2\\3", $output ); return $output; } function addExampleLineNumbers( $output ) { return preg_replace_callback( '@
(.+?)<\/pre>@ms', 'callbackAddLineNumbers', $output );
}

$lineNr = 0;

function callbackAddLineNr( $args )
{
    global $lineNr;

    $nrString = str_replace( ' ', ' ', sprintf( '%3d', $lineNr ) );
    if ( $lineNr == 0 )
    {
        $val = '';
    }
    else
    {
        $val = $nrString . ". {$args[1]}\n";
    }
    $lineNr++;
    return $val;
}

function callbackAddLineNumbers( $args )
{
    global $lineNr;

    $lineNr = 0;
    
    if ( strstr( $args[1], '<?php' ) !== false )
    {
        $listing = '
';
        $highlighted = highlight_string( html_entity_decode( $args[1] ), true );
        $highlighted = preg_replace( '@^.
@ms', '
', $highlighted ); $highlighted = preg_replace( '@()(.*?)((
)+)(.*?)(
)@ms', '\1\2\6\3\1\5\6', $highlighted ); $highlighted = preg_replace( '@()(.+?)(
)(
)@ms', '\1\2\4\3', $highlighted ); $highlighted = preg_replace( '@@', '', $highlighted ); $highlighted = preg_replace( '@

.
$@ms', "
", $highlighted ); $highlighted = preg_replace_callback( '@(.*?)
@', "callbackAddLineNr", $highlighted ); $listing .= $highlighted . '
'; return $listing; } else { return $args[0]; } }