From 407ce2ece0331a3fb748984d4b533de13c11a836 Mon Sep 17 00:00:00 2001 From: Michiel Vancoillie Date: Wed, 7 Aug 2013 17:12:14 +0200 Subject: [PATCH] Update HTML formatter --- src/tdt/formatters/strategies/HTML.php | 98 ++++++++++++++++---------- 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/src/tdt/formatters/strategies/HTML.php b/src/tdt/formatters/strategies/HTML.php index 4442cf1..6ea1e62 100644 --- a/src/tdt/formatters/strategies/HTML.php +++ b/src/tdt/formatters/strategies/HTML.php @@ -7,7 +7,7 @@ * @license AGPLv3 * @author Jan Vansteenlandt * @author Pieter Colpaert - * @author Miel Vander Sande + * @author Miel Vander Sande */ namespace tdt\formatters\strategies; @@ -32,7 +32,9 @@ public function printHeader() { public function printBody() { $generator = new Generator(); + $output = $this->displayTree($this->objectToPrint); + $h = headers_list(); $i = 0; $matches = array(); @@ -40,58 +42,80 @@ public function printBody() { $i++; } if($i < sizeof($h)){ - $output .= "

Next page

"; + //$output .= "

Next page

"; } - $generator->generate($output); + $generator->generate($output, 'resource'); } - + public static function getDocumentation(){ - return "The Html formatter is a formatter which prints nice output for users. It prints everything in the internal object and extra links towards meta-data and documentation."; + return "The HTML formatter is a formatter which prints nice output for users. It prints everything in the internal object and extra links towards meta-data and documentation."; } private function getUrl($type) { $ext = explode(".", $_SERVER['REQUEST_URI']); - return "http://" . $_SERVER['HTTP_HOST'] . str_replace('.' . $ext[1],'.' . $type,$_SERVER['REQUEST_URI']); + return "http://" . $_SERVER['HTTP_HOST'] . str_replace('.' . $ext[1],'.' . $type,$_SERVER['REQUEST_URI']); } private function displayTree($var) { - $newline = "\n"; - $output =""; - foreach($var as $key => $value) { - if (is_array($value) || is_object($value)) { - $value = $newline . "
    " . $this->displayTree($value) . "

"; + if (is_object($this->objectToPrint)) { + $hash = get_object_vars($this->objectToPrint); + } + + $formattedJSON = $this->prettyPrint(json_encode($hash)); + + return str_replace("\/","/", $formattedJSON); + } + + private function prettyPrint($json){ + $result = ''; + $level = 0; + $prev_char = ''; + $in_quotes = false; + $ends_line_level = NULL; + $json_length = strlen( $json ); + + for( $i = 0; $i < $json_length; $i++ ) { + $char = $json[$i]; + $new_line_level = NULL; + $post = ""; + if( $ends_line_level !== NULL ) { + $new_line_level = $ends_line_level; + $ends_line_level = NULL; } + if( $char === '"' && $prev_char != '\\' ) { + $in_quotes = !$in_quotes; + } else if( ! $in_quotes ) { + switch( $char ) { + case '}': case ']': + $level--; + $ends_line_level = NULL; + $new_line_level = $level; + break; - if (is_array($var)) { - if (!stripos($value, "formatValue($value) . "" . $newline; - }else{ - $output .= "
  • " . $key. " : " . $this->formatValue($value) . "
  • " . $newline; - } - - } - else { - $output .= $key. $this->formatValue($value) . $newline; + case '{': case '[': + $level++; + case ',': + $ends_line_level = $level; + break; + + case ':': + $post = " "; + break; + + case " ": case "\t": case "\n": case "\r": + $char = ""; + $ends_line_level = $new_line_level; + $new_line_level = NULL; + break; } - } - else { // is_object - if (!stripos($value, "formatValue($value) . "" . $newline; - } - - $output .= "
  • " . $key . $this->formatValue($value) . "
  • " . $newline; + if( $new_line_level !== NULL ) { + $result .= "\n".str_repeat( " ", $new_line_level ); } + $result .= $char.$post; + $prev_char = $char; } - return $output; - } - // formats url-values to a href= - private function formatValue($value){ - if(substr($value,0,4) == "http" || substr($value,0,5) == "https"){ - $value = '' . $value .""; - } - return $value; + return $result; } } \ No newline at end of file