Index: trunk/extensions/Translate/spyc/spyc.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Spyc -- A Simple PHP YAML Class |
5 | | - * @version 0.4.2 |
| 5 | + * @version 0.4.5 |
6 | 6 | * @author Vlad Andersen <vlad.andersen@gmail.com> |
7 | 7 | * @author Chris Wanstrath <chris@ozmm.org> |
8 | 8 | * @link http://code.google.com/p/spyc/ |
— | — | @@ -300,19 +300,26 @@ |
301 | 301 | */ |
302 | 302 | private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) { |
303 | 303 | // 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 | + ) { |
307 | 308 | $value = $this->_doLiteralBlock($value,$indent); |
308 | 309 | } else { |
309 | 310 | $value = $this->_doFolding($value,$indent); |
310 | | - if (is_bool($value)) { |
311 | | - $value = ($value) ? "true" : "false"; |
312 | | - } |
313 | 311 | } |
314 | 312 | |
315 | 313 | 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); |
316 | 319 | |
| 320 | + if (is_bool($value)) { |
| 321 | + $value = ($value) ? "true" : "false"; |
| 322 | + } |
| 323 | + |
317 | 324 | $spaces = str_repeat(' ',$indent); |
318 | 325 | |
319 | 326 | if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { |
— | — | @@ -335,6 +342,7 @@ |
336 | 343 | * @param $indent int The value of the indent |
337 | 344 | */ |
338 | 345 | private function _doLiteralBlock($value,$indent) { |
| 346 | + if ($value === "\n") return '\n'; |
339 | 347 | if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
340 | 348 | return sprintf ("'%s'", $value); |
341 | 349 | } |
— | — | @@ -346,7 +354,7 @@ |
347 | 355 | $indent += $this->_dumpIndent; |
348 | 356 | $spaces = str_repeat(' ',$indent); |
349 | 357 | foreach ($exploded as $line) { |
350 | | - $newValue .= "\n" . $spaces . trim($line); |
| 358 | + $newValue .= "\n" . $spaces . ($line); |
351 | 359 | } |
352 | 360 | return $newValue; |
353 | 361 | } |
— | — | @@ -471,8 +479,8 @@ |
472 | 480 | private function _parseLine($line) { |
473 | 481 | if (!$line) return array(); |
474 | 482 | $line = trim($line); |
| 483 | + if (!$line) return array(); |
475 | 484 | |
476 | | - if (!$line) return array(); |
477 | 485 | $array = array(); |
478 | 486 | |
479 | 487 | $group = $this->nodeContainsGroup($line); |
— | — | @@ -520,9 +528,11 @@ |
521 | 529 | if ($is_quoted) |
522 | 530 | return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); |
523 | 531 | |
524 | | - if (strpos($value, ' #') !== false) |
| 532 | + if (strpos($value, ' #') !== false && !$is_quoted) |
525 | 533 | $value = preg_replace('/\s+#(.+)$/','',$value); |
526 | 534 | |
| 535 | + if (!$is_quoted) $value = str_replace('\n', "\n", $value); |
| 536 | + |
527 | 537 | if ($first_character == '[' && $last_character == ']') { |
528 | 538 | // Take out strings sequences and mappings |
529 | 539 | $innerValue = trim(substr ($value, 1, -1)); |
— | — | @@ -732,14 +742,17 @@ |
733 | 743 | |
734 | 744 | private function addArray ($incoming_data, $incoming_indent) { |
735 | 745 | |
| 746 | + // print_r ($incoming_data); |
| 747 | + |
736 | 748 | if (count ($incoming_data) > 1) |
737 | 749 | return $this->addArrayInline ($incoming_data, $incoming_indent); |
738 | 750 | |
739 | 751 | $key = key ($incoming_data); |
740 | 752 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
| 753 | + if ($key === '__!YAMLZero') $key = '0'; |
741 | 754 | |
742 | 755 | if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
743 | | - if ($key || $key === '') { |
| 756 | + if ($key || $key === '' || $key === '0') { |
744 | 757 | $this->result[$key] = $value; |
745 | 758 | } else { |
746 | 759 | $this->result[] = $value; end ($this->result); $key = key ($this->result); |
— | — | @@ -768,7 +781,7 @@ |
769 | 782 | if (!is_array ($_arr)) { $_arr = array (); } |
770 | 783 | |
771 | 784 | $_arr = array_merge ($_arr, $value); |
772 | | - } else if ($key || $key === '') { |
| 785 | + } else if ($key || $key === '' || $key === '0') { |
773 | 786 | $_arr[$key] = $value; |
774 | 787 | } else { |
775 | 788 | if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
— | — | @@ -905,13 +918,21 @@ |
906 | 919 | } |
907 | 920 | |
908 | 921 | |
| 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 | + |
909 | 930 | private function startsMappedSequence ($line) { |
910 | 931 | return ($line[0] == '-' && substr ($line, -1, 1) == ':'); |
911 | 932 | } |
912 | 933 | |
913 | 934 | private function returnMappedSequence ($line) { |
914 | 935 | $array = array(); |
915 | | - $key = trim(substr($line,1,-1)); |
| 936 | + $key = self::unquote(trim(substr($line,1,-1))); |
916 | 937 | $array[$key] = array(); |
917 | 938 | $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key); |
918 | 939 | return array($array); |
— | — | @@ -919,7 +940,7 @@ |
920 | 941 | |
921 | 942 | private function returnMappedValue ($line) { |
922 | 943 | $array = array(); |
923 | | - $key = trim(substr($line,0,-1)); |
| 944 | + $key = self::unquote (trim(substr($line,0,-1))); |
924 | 945 | $array[$key] = ''; |
925 | 946 | return $array; |
926 | 947 | } |
— | — | @@ -938,6 +959,7 @@ |
939 | 960 | |
940 | 961 | private function returnKeyValuePair ($line) { |
941 | 962 | $array = array(); |
| 963 | + $key = ''; |
942 | 964 | if (strpos ($line, ':')) { |
943 | 965 | // It's a key/value pair most likely |
944 | 966 | // If the key is in double quotes pull it out |
— | — | @@ -953,13 +975,11 @@ |
954 | 976 | } |
955 | 977 | // Set the type of the value. Int, string, etc |
956 | 978 | $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); |
962 | 983 | } |
963 | | - |
964 | 984 | return $array; |
965 | 985 | |
966 | 986 | } |