JSON.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * JSON renderer.
  5. *
  6. * Based on the HTML renderer by Andrey Demenev.
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Text
  15. * @package Text_Highlighter
  16. * @author Stoyan Stefanov <ssttoo@gmail.com>
  17. * @copyright 2006 Stoyan Stefanov
  18. * @license http://www.php.net/license/3_0.txt PHP License
  19. * @version CVS: $Id: JSON.php,v 1.1 2007/06/03 02:37:09 ssttoo Exp $
  20. * @link http://pear.php.net/package/Text_Highlighter
  21. */
  22. /**
  23. * @ignore
  24. */
  25. require_once dirname(__FILE__).'/../Renderer.php';
  26. require_once dirname(__FILE__).'/../Renderer/Array.php';
  27. /**
  28. * JSON renderer, based on Andrey Demenev's HTML renderer.
  29. *
  30. * @author Stoyan Stefanov <ssttoo@gmail.com>
  31. * @category Text
  32. * @package Text_Highlighter
  33. * @copyright 2006 Stoyan Stefanov
  34. * @license http://www.php.net/license/3_0.txt PHP License
  35. * @version Release: 0.5.0
  36. * @link http://pear.php.net/package/Text_Highlighter
  37. */
  38. class Text_Highlighter_Renderer_JSON extends Text_Highlighter_Renderer_Array
  39. {
  40. /**
  41. * Signals that no more tokens are available
  42. *
  43. * @abstract
  44. * @access public
  45. */
  46. function finalize()
  47. {
  48. parent::finalize();
  49. $output = parent::getOutput();
  50. $json_array = array();
  51. foreach ($output AS $token) {
  52. if ($this->_enumerated) {
  53. $json_array[] = '["' . $token[0] . '","' . $token[1] . '"]';
  54. } else {
  55. $key = key($token);
  56. $json_array[] = '{"class": "' . $key . '","content":"' . $token[$key] . '"}';
  57. }
  58. }
  59. $this->_output = '['. implode(',', $json_array) .']';
  60. $this->_output = str_replace("\n", '\n', $this->_output);
  61. }
  62. }
  63. /*
  64. * Local variables:
  65. * tab-width: 4
  66. * c-basic-offset: 4
  67. * c-hanging-comment-ender-p: nil
  68. * End:
  69. */
  70. ?>