Index: trunk/extensions/Translate/spyc/spyc.php |
— | — | @@ -1,11 +1,11 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Spyc -- A Simple PHP YAML Class |
5 | | - * @version 0.4.5-svn |
| 5 | + * @version 0.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/ |
9 | | - * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen |
| 9 | + * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen |
10 | 10 | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
11 | 11 | * @package Spyc |
12 | 12 | */ |
— | — | @@ -58,10 +58,12 @@ |
59 | 59 | |
60 | 60 | // SETTINGS |
61 | 61 | |
| 62 | + const REMPTY = "\0\0\0\0\0"; |
| 63 | + |
62 | 64 | /** |
63 | 65 | * Setting this to true will force YAMLDump to enclose any string value in |
64 | 66 | * quotes. False by default. |
65 | | - * |
| 67 | + * |
66 | 68 | * @var bool |
67 | 69 | */ |
68 | 70 | public $setting_dump_force_quotes = false; |
— | — | @@ -141,7 +143,7 @@ |
142 | 144 | /** |
143 | 145 | * Load a string of YAML into a PHP array statically |
144 | 146 | * |
145 | | - * The load method, when supplied with a YAML string, will do its best |
| 147 | + * The load method, when supplied with a YAML string, will do its best |
146 | 148 | * to convert YAML in a string into a PHP array. Pretty simple. |
147 | 149 | * |
148 | 150 | * Note: use this function if you don't want files from the file system |
— | — | @@ -230,7 +232,7 @@ |
231 | 233 | |
232 | 234 | // Start at the base of the array and move through it. |
233 | 235 | if ($array) { |
234 | | - $array = (array)$array; |
| 236 | + $array = (array)$array; |
235 | 237 | $previous_key = -1; |
236 | 238 | foreach ($array as $key => $value) { |
237 | 239 | if (!isset($first_key)) $first_key = $key; |
— | — | @@ -255,7 +257,7 @@ |
256 | 258 | return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
257 | 259 | // It has children. What to do? |
258 | 260 | // Make it the right kind of item |
259 | | - $string = $this->_dumpNode($key, NULL, $indent, $previous_key, $first_key, $source_array); |
| 261 | + $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array); |
260 | 262 | // Add the indent |
261 | 263 | $indent += $this->_dumpIndent; |
262 | 264 | // Yamlize the array |
— | — | @@ -319,6 +321,9 @@ |
320 | 322 | if (is_bool($value)) { |
321 | 323 | $value = ($value) ? "true" : "false"; |
322 | 324 | } |
| 325 | + |
| 326 | + if ($value === null) $value = 'null'; |
| 327 | + if ($value === "'" . self::REMPTY . "'") $value = null; |
323 | 328 | |
324 | 329 | $spaces = str_repeat(' ',$indent); |
325 | 330 | |
— | — | @@ -375,7 +380,7 @@ |
376 | 381 | $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
377 | 382 | $value = ">\n".$indent.$wrapped; |
378 | 383 | } else { |
379 | | - if ($this->setting_dump_force_quotes && is_string ($value)) |
| 384 | + if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) |
380 | 385 | $value = '"' . $value . '"'; |
381 | 386 | } |
382 | 387 | |
— | — | @@ -408,7 +413,7 @@ |
409 | 414 | $cnt = count($Source); |
410 | 415 | for ($i = 0; $i < $cnt; $i++) { |
411 | 416 | $line = $Source[$i]; |
412 | | - |
| 417 | + |
413 | 418 | $this->indent = strlen($line) - strlen(ltrim($line)); |
414 | 419 | $tempPath = $this->getParentPathByIndent($this->indent); |
415 | 420 | $line = self::stripIndent($line, $this->indent); |
— | — | @@ -421,9 +426,9 @@ |
422 | 427 | $line = rtrim ($line, $literalBlockStyle . " \n"); |
423 | 428 | $literalBlock = ''; |
424 | 429 | $line .= $this->LiteralPlaceHolder; |
425 | | - |
| 430 | + $literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1])); |
426 | 431 | while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { |
427 | | - $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle); |
| 432 | + $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent); |
428 | 433 | } |
429 | 434 | $i--; |
430 | 435 | } |
— | — | @@ -500,9 +505,9 @@ |
501 | 506 | return $this->returnArrayElement($line); |
502 | 507 | |
503 | 508 | if ($this->isPlainArray($line)) |
504 | | - return $this->returnPlainArray($line); |
505 | | - |
506 | | - |
| 509 | + return $this->returnPlainArray($line); |
| 510 | + |
| 511 | + |
507 | 512 | return $this->returnKeyValuePair($line); |
508 | 513 | |
509 | 514 | } |
— | — | @@ -528,7 +533,7 @@ |
529 | 534 | |
530 | 535 | if ($is_quoted) |
531 | 536 | return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); |
532 | | - |
| 537 | + |
533 | 538 | if (strpos($value, ' #') !== false && !$is_quoted) |
534 | 539 | $value = preg_replace('/\s+#(.+)$/','',$value); |
535 | 540 | |
— | — | @@ -555,7 +560,7 @@ |
556 | 561 | $value = $this->_toType($value); |
557 | 562 | return array($key => $value); |
558 | 563 | } |
559 | | - |
| 564 | + |
560 | 565 | if ($first_character == '{' && $last_character == '}') { |
561 | 566 | $innerValue = trim(substr ($value, 1, -1)); |
562 | 567 | if ($innerValue === '') return array(); |
— | — | @@ -602,7 +607,7 @@ |
603 | 608 | $value = (float)$value; |
604 | 609 | return $value; |
605 | 610 | } |
606 | | - |
| 611 | + |
607 | 612 | return $value; |
608 | 613 | } |
609 | 614 | |
— | — | @@ -705,7 +710,7 @@ |
706 | 711 | if ($finished) break; |
707 | 712 | |
708 | 713 | $i++; |
709 | | - if ($i > 10) |
| 714 | + if ($i > 10) |
710 | 715 | break; // Prevent infinite loops. |
711 | 716 | } |
712 | 717 | |
— | — | @@ -733,7 +738,7 @@ |
734 | 739 | private function addArrayInline ($array, $indent) { |
735 | 740 | $CommonGroupPath = $this->path; |
736 | 741 | if (empty ($array)) return false; |
737 | | - |
| 742 | + |
738 | 743 | foreach ($array as $k => $_) { |
739 | 744 | $this->addArray(array($k => $_), $indent); |
740 | 745 | $this->path = $CommonGroupPath; |
— | — | @@ -747,7 +752,7 @@ |
748 | 753 | |
749 | 754 | if (count ($incoming_data) > 1) |
750 | 755 | return $this->addArrayInline ($incoming_data, $incoming_indent); |
751 | | - |
| 756 | + |
752 | 757 | $key = key ($incoming_data); |
753 | 758 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
754 | 759 | if ($key === '__!YAMLZero') $key = '0'; |
— | — | @@ -763,7 +768,7 @@ |
764 | 769 | } |
765 | 770 | |
766 | 771 | |
767 | | - |
| 772 | + |
768 | 773 | $history = array(); |
769 | 774 | // Unfolding inner array tree. |
770 | 775 | $history[] = $_arr = $this->result; |
— | — | @@ -782,7 +787,7 @@ |
783 | 788 | if (!is_array ($_arr)) { $_arr = array (); } |
784 | 789 | |
785 | 790 | $_arr = array_merge ($_arr, $value); |
786 | | - } elseif ($key || $key === '' || $key === '0') { |
| 791 | + } else if ($key || $key === '' || $key === '0') { |
787 | 792 | if (!is_array ($_arr)) |
788 | 793 | $_arr = array ($key=>$value); |
789 | 794 | else |
— | — | @@ -834,8 +839,11 @@ |
835 | 840 | return false; |
836 | 841 | } |
837 | 842 | |
838 | | - private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) { |
839 | | - $line = self::stripIndent($line); |
| 843 | + private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) { |
| 844 | + $line = self::stripIndent($line, $indent); |
| 845 | + if ($literalBlockStyle !== '|') { |
| 846 | + $line = self::stripIndent($line); |
| 847 | + } |
840 | 848 | $line = rtrim ($line, "\r\n\t ") . "\n"; |
841 | 849 | if ($literalBlockStyle == '|') { |
842 | 850 | return $literalBlock . $line; |
— | — | @@ -854,8 +862,8 @@ |
855 | 863 | foreach ($lineArray as $k => $_) { |
856 | 864 | if (is_array($_)) |
857 | 865 | $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
858 | | - elseif (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
859 | | - $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
| 866 | + else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
| 867 | + $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
860 | 868 | } |
861 | 869 | return $lineArray; |
862 | 870 | } |
— | — | @@ -907,7 +915,7 @@ |
908 | 916 | if ($line[0] != '-') return false; |
909 | 917 | if (strlen ($line) > 3) |
910 | 918 | if (substr($line,0,3) == '---') return false; |
911 | | - |
| 919 | + |
912 | 920 | return true; |
913 | 921 | } |
914 | 922 | |
— | — | @@ -952,14 +960,14 @@ |
953 | 961 | private function startsMappedValue ($line) { |
954 | 962 | return (substr ($line, -1, 1) == ':'); |
955 | 963 | } |
956 | | - |
| 964 | + |
957 | 965 | private function isPlainArray ($line) { |
958 | 966 | return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
959 | 967 | } |
960 | | - |
| 968 | + |
961 | 969 | private function returnPlainArray ($line) { |
962 | | - return $this->_toType($line); |
963 | | - } |
| 970 | + return $this->_toType($line); |
| 971 | + } |
964 | 972 | |
965 | 973 | private function returnKeyValuePair ($line) { |
966 | 974 | $array = array(); |
— | — | @@ -999,7 +1007,7 @@ |
1000 | 1008 | } |
1001 | 1009 | |
1002 | 1010 | |
1003 | | - private function nodeContainsGroup ($line) { |
| 1011 | + private function nodeContainsGroup ($line) { |
1004 | 1012 | $symbolsForReference = 'A-z0-9_\-'; |
1005 | 1013 | if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
1006 | 1014 | if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |