render.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env php
  2. <?php
  3. use splitbrain\phpcli\CLI;
  4. use splitbrain\phpcli\Options;
  5. if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
  6. define('NOSESSION', 1);
  7. require_once(DOKU_INC . 'inc/init.php');
  8. /**
  9. * A simple commandline tool to render some DokuWiki syntax with a given
  10. * renderer.
  11. *
  12. * This may not work for plugins that expect a certain environment to be
  13. * set up before rendering, but should work for most or even all standard
  14. * DokuWiki markup
  15. *
  16. * @license GPL2
  17. * @author Andreas Gohr <andi@splitbrain.org>
  18. */
  19. class RenderCLI extends CLI {
  20. /**
  21. * Register options and arguments on the given $options object
  22. *
  23. * @param Options $options
  24. * @return void
  25. */
  26. protected function setup(Options $options) {
  27. $options->setHelp(
  28. 'A simple commandline tool to render some DokuWiki syntax with a given renderer.' .
  29. "\n\n" .
  30. 'This may not work for plugins that expect a certain environment to be ' .
  31. 'set up before rendering, but should work for most or even all standard ' .
  32. 'DokuWiki markup'
  33. );
  34. $options->registerOption('renderer', 'The renderer mode to use. Defaults to xhtml', 'r', 'mode');
  35. }
  36. /**
  37. * Your main program
  38. *
  39. * Arguments and options have been parsed when this is run
  40. *
  41. * @param Options $options
  42. * @throws DokuCLI_Exception
  43. * @return void
  44. */
  45. protected function main(Options $options) {
  46. $renderer = $options->getOpt('renderer', 'xhtml');
  47. // do the action
  48. $source = stream_get_contents(STDIN);
  49. $info = array();
  50. $result = p_render($renderer, p_get_instructions($source), $info);
  51. if(is_null($result)) throw new DokuCLI_Exception("No such renderer $renderer");
  52. echo $result;
  53. }
  54. }
  55. // Main
  56. $cli = new RenderCLI();
  57. $cli->run();