r56695 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56694‎ | r56695 | r56696 >
Date:08:10, 21 September 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
New version of spyc (r91)
Modified paths:
  • /trunk/extensions/Translate/spyc/spyc.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/spyc/spyc.php
@@ -1,7 +1,7 @@
22 <?php
33 /**
44 * Spyc -- A Simple PHP YAML Class
5 - * @version 0.4.2
 5+ * @version 0.4.5
66 * @author Vlad Andersen <vlad.andersen@gmail.com>
77 * @author Chris Wanstrath <chris@ozmm.org>
88 * @link http://code.google.com/p/spyc/
@@ -300,19 +300,26 @@
301301 */
302302 private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) {
303303 // do some folding here, for blocks
304 - if (is_string ($value) && (strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false ||
305 - strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false ||
306 - strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || substr ($value, -1, 1) == ':') {
 304+ if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false ||
 305+ strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, ' ') !== false ||
 306+ strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || substr ($value, -1, 1) == ':')
 307+ ) {
307308 $value = $this->_doLiteralBlock($value,$indent);
308309 } else {
309310 $value = $this->_doFolding($value,$indent);
310 - if (is_bool($value)) {
311 - $value = ($value) ? "true" : "false";
312 - }
313311 }
314312
315313 if ($value === array()) $value = '[ ]';
 314+ if (in_array ($value, array ('true', 'TRUE', 'false', 'FALSE', 'y', 'Y', 'n', 'N', 'null', 'NULL'), true)) {
 315+ $value = $this->_doLiteralBlock($value,$indent);
 316+ }
 317+ if (trim ($value) != $value)
 318+ $value = $this->_doLiteralBlock($value,$indent);
316319
 320+ if (is_bool($value)) {
 321+ $value = ($value) ? "true" : "false";
 322+ }
 323+
317324 $spaces = str_repeat(' ',$indent);
318325
319326 if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
@@ -335,6 +342,7 @@
336343 * @param $indent int The value of the indent
337344 */
338345 private function _doLiteralBlock($value,$indent) {
 346+ if ($value === "\n") return '\n';
339347 if (strpos($value, "\n") === false && strpos($value, "'") === false) {
340348 return sprintf ("'%s'", $value);
341349 }
@@ -346,7 +354,7 @@
347355 $indent += $this->_dumpIndent;
348356 $spaces = str_repeat(' ',$indent);
349357 foreach ($exploded as $line) {
350 - $newValue .= "\n" . $spaces . trim($line);
 358+ $newValue .= "\n" . $spaces . ($line);
351359 }
352360 return $newValue;
353361 }
@@ -471,8 +479,8 @@
472480 private function _parseLine($line) {
473481 if (!$line) return array();
474482 $line = trim($line);
 483+ if (!$line) return array();
475484
476 - if (!$line) return array();
477485 $array = array();
478486
479487 $group = $this->nodeContainsGroup($line);
@@ -520,9 +528,11 @@
521529 if ($is_quoted)
522530 return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\''));
523531
524 - if (strpos($value, ' #') !== false)
 532+ if (strpos($value, ' #') !== false && !$is_quoted)
525533 $value = preg_replace('/\s+#(.+)$/','',$value);
526534
 535+ if (!$is_quoted) $value = str_replace('\n', "\n", $value);
 536+
527537 if ($first_character == '[' && $last_character == ']') {
528538 // Take out strings sequences and mappings
529539 $innerValue = trim(substr ($value, 1, -1));
@@ -732,14 +742,17 @@
733743
734744 private function addArray ($incoming_data, $incoming_indent) {
735745
 746+ // print_r ($incoming_data);
 747+
736748 if (count ($incoming_data) > 1)
737749 return $this->addArrayInline ($incoming_data, $incoming_indent);
738750
739751 $key = key ($incoming_data);
740752 $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null;
 753+ if ($key === '__!YAMLZero') $key = '0';
741754
742755 if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values.
743 - if ($key || $key === '') {
 756+ if ($key || $key === '' || $key === '0') {
744757 $this->result[$key] = $value;
745758 } else {
746759 $this->result[] = $value; end ($this->result); $key = key ($this->result);
@@ -768,7 +781,7 @@
769782 if (!is_array ($_arr)) { $_arr = array (); }
770783
771784 $_arr = array_merge ($_arr, $value);
772 - } else if ($key || $key === '') {
 785+ } else if ($key || $key === '' || $key === '0') {
773786 $_arr[$key] = $value;
774787 } else {
775788 if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
@@ -905,13 +918,21 @@
906919 }
907920
908921
 922+ private static function unquote ($value) {
 923+ if (!$value) return $value;
 924+ if (!is_string($value)) return $value;
 925+ if ($value[0] == '\'') return trim ($value, '\'');
 926+ if ($value[0] == '"') return trim ($value, '"');
 927+ return $value;
 928+ }
 929+
909930 private function startsMappedSequence ($line) {
910931 return ($line[0] == '-' && substr ($line, -1, 1) == ':');
911932 }
912933
913934 private function returnMappedSequence ($line) {
914935 $array = array();
915 - $key = trim(substr($line,1,-1));
 936+ $key = self::unquote(trim(substr($line,1,-1)));
916937 $array[$key] = array();
917938 $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key);
918939 return array($array);
@@ -919,7 +940,7 @@
920941
921942 private function returnMappedValue ($line) {
922943 $array = array();
923 - $key = trim(substr($line,0,-1));
 944+ $key = self::unquote (trim(substr($line,0,-1)));
924945 $array[$key] = '';
925946 return $array;
926947 }
@@ -938,6 +959,7 @@
939960
940961 private function returnKeyValuePair ($line) {
941962 $array = array();
 963+ $key = '';
942964 if (strpos ($line, ':')) {
943965 // It's a key/value pair most likely
944966 // If the key is in double quotes pull it out
@@ -953,13 +975,11 @@
954976 }
955977 // Set the type of the value. Int, string, etc
956978 $value = $this->_toType($value);
957 - if (empty($key)) {
958 - $array[] = $value;
959 - } else {
960 - $array[$key] = $value;
961 - }
 979+ if ($key === '0') $key = '__!YAMLZero';
 980+ $array[$key] = $value;
 981+ } else {
 982+ $array = array ($line);
962983 }
963 -
964984 return $array;
965985
966986 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r91Prevent diff pages from being cached; paranoid HTML escaping on hoversvibber03:51, 8 February 2002

Status & tagging log