Changeset 32418 in spip-zone
- Timestamp:
- Oct 28, 2009, 2:41:21 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
_plugins_/yaml/spyc/spyc.php
r31640 r32418 2 2 /** 3 3 * Spyc -- A Simple PHP YAML Class 4 * @version 0.4.1 4 * @version 0.4.5 5 * @author Vlad Andersen <vlad.andersen@gmail.com> 5 6 * @author Chris Wanstrath <chris@ozmm.org> 6 * @author Vlad Andersen <vlad@oneiros.ru> 7 * @link http://spyc.sourceforge.net/ 7 * @link http://code.google.com/p/spyc/ 8 8 * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen 9 9 * @license http://www.opensource.org/licenses/mit-license.php MIT License … … 230 230 231 231 // Start at the base of the array and move through it. 232 $previous_key = -1; 233 foreach ($array as $key => $value) { 234 $string .= $this->_yamlize($key,$value,0,$previous_key); 235 $previous_key = $key; 236 } 237 232 if ($array) { 233 $array = (array)$array; 234 $first_key = key($array); 235 236 $previous_key = -1; 237 foreach ($array as $key => $value) { 238 $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key); 239 $previous_key = $key; 240 } 241 } 238 242 return $string; 239 243 } … … 247 251 * @param $indent The indent of the current node 248 252 */ 249 private function _yamlize($key,$value,$indent, $previous_key = -1 ) {253 private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0) { 250 254 if (is_array($value)) { 251 255 if (empty ($value)) 252 return $this->_dumpNode($key, array(), $indent, $previous_key );256 return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key); 253 257 // It has children. What to do? 254 258 // Make it the right kind of item 255 $string = $this->_dumpNode($key, NULL, $indent, $previous_key );259 $string = $this->_dumpNode($key, NULL, $indent, $previous_key, $first_key); 256 260 // Add the indent 257 261 $indent += $this->_dumpIndent; … … 260 264 } elseif (!is_array($value)) { 261 265 // It doesn't have children. Yip. 262 $string = $this->_dumpNode($key, $value, $indent, $previous_key );266 $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key); 263 267 } 264 268 return $string; … … 276 280 $string = ''; 277 281 $previous_key = -1; 282 $first_key = key($array); 278 283 foreach ($array as $key => $value) { 279 $string .= $this->_yamlize($key, $value, $indent, $previous_key );284 $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key); 280 285 $previous_key = $key; 281 286 } … … 294 299 * @param $indent The indent of the current node 295 300 */ 296 private function _dumpNode($key, $value, $indent, $previous_key = -1 ) {301 private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) { 297 302 // do some folding here, for blocks 298 if (strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || 299 strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || 300 strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) { 303 if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || 304 strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, ' ') !== false || 305 strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || substr ($value, -1, 1) == ':') 306 ) { 301 307 $value = $this->_doLiteralBlock($value,$indent); 302 308 } else { 303 309 $value = $this->_doFolding($value,$indent); 304 if (is_bool($value)) {305 $value = ($value) ? "true" : "false";306 }307 310 } 308 311 309 312 if ($value === array()) $value = '[ ]'; 313 if (in_array ($value, array ('true', 'TRUE', 'false', 'FALSE', 'y', 'Y', 'n', 'N', 'null', 'NULL'), true)) { 314 $value = $this->_doLiteralBlock($value,$indent); 315 } 316 if (trim ($value) != $value) 317 $value = $this->_doLiteralBlock($value,$indent); 318 319 if (is_bool($value)) { 320 $value = ($value) ? "true" : "false"; 321 } 310 322 311 323 $spaces = str_repeat(' ',$indent); 312 324 313 if (is_int($key) && $key - 1 == $previous_key ) {325 if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { 314 326 // It's a sequence 315 327 $string = $spaces.'- '.$value."\n"; 316 328 } else { 329 if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); 317 330 // It's mapped 318 331 if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; } … … 330 343 */ 331 344 private function _doLiteralBlock($value,$indent) { 345 if ($value === "\n") return '\n'; 346 if (strpos($value, "\n") === false && strpos($value, "'") === false) { 347 return sprintf ("'%s'", $value); 348 } 349 if (strpos($value, "\n") === false && strpos($value, '"') === false) { 350 return sprintf ('"%s"', $value); 351 } 332 352 $exploded = explode("\n",$value); 333 353 $newValue = '|'; … … 335 355 $spaces = str_repeat(' ',$indent); 336 356 foreach ($exploded as $line) { 337 $newValue .= "\n" . $spaces . trim($line);357 $newValue .= "\n" . $spaces . ($line); 338 358 } 339 359 return $newValue; … … 349 369 // Don't do anything if wordwrap is set to 0 350 370 351 if ($this->_dumpWordWrap !== 0 && strlen($value) > $this->_dumpWordWrap) {371 if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) { 352 372 $indent += $this->_dumpIndent; 353 373 $indent = str_repeat(' ',$indent); … … 378 398 if (empty ($Source)) return array(); 379 399 if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { 380 $array = syck_load ( $Source);400 $array = syck_load (implode ('', $Source)); 381 401 return is_array($array) ? $array : array(); 382 402 } … … 415 435 416 436 437 if (strpos ($line, '#')) { 438 if (strpos ($line, '"') === false && strpos ($line, "'") === false) 439 $line = preg_replace('/\s+#(.+)$/','',$line); 440 } 441 417 442 $lineArray = $this->_parseLine($line); 418 443 … … 456 481 $line = trim($line); 457 482 if (!$line) return array(); 483 458 484 $array = array(); 459 485 … … 503 529 return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\'')); 504 530 505 if (strpos($value, ' #') !== false )531 if (strpos($value, ' #') !== false && !$is_quoted) 506 532 $value = preg_replace('/\s+#(.+)$/','',$value); 533 534 if (!$is_quoted) $value = str_replace('\n', "\n", $value); 507 535 508 536 if ($first_character == '[' && $last_character == ']') { … … 690 718 } 691 719 720 private function referenceContentsByAlias ($alias) { 721 do { 722 if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } 723 $groupPath = $this->SavedGroups[$alias]; 724 $value = $this->result; 725 foreach ($groupPath as $k) { 726 $value = $value[$k]; 727 } 728 } while (false); 729 return $value; 730 } 731 692 732 private function addArrayInline ($array, $indent) { 693 733 $CommonGroupPath = $this->path; … … 700 740 return true; 701 741 } 702 742 703 743 private function addArray ($incoming_data, $incoming_indent) { 744 745 // print_r ($incoming_data); 704 746 705 747 if (count ($incoming_data) > 1) … … 707 749 708 750 $key = key ($incoming_data); 709 $value = $incoming_data[$key]; 751 $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; 752 if ($key === '__!YAMLZero') $key = '0'; 710 753 711 754 if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. 712 if ($key ) {755 if ($key || $key === '' || $key === '0') { 713 756 $this->result[$key] = $value; 714 } 757 } else { 715 758 $this->result[] = $value; end ($this->result); $key = key ($this->result); 716 759 } … … 729 772 730 773 if ($this->_containsGroupAlias) { 731 do { 732 if (!isset($this->SavedGroups[$this->_containsGroupAlias])) { echo "Bad group name: $this->_containsGroupAlias."; break; } 733 $groupPath = $this->SavedGroups[$this->_containsGroupAlias]; 734 $value = $this->result; 735 foreach ($groupPath as $k) { 736 $value = $value[$k]; 737 } 738 } while (false); 774 $value = $this->referenceContentsByAlias($this->_containsGroupAlias); 739 775 $this->_containsGroupAlias = false; 740 776 } … … 742 778 743 779 // Adding string or numeric key to the innermost level or $this->arr. 744 if ($key) 780 if (is_string($key) && $key == '<<') { 781 if (!is_array ($_arr)) { $_arr = array (); } 782 783 $_arr = array_merge ($_arr, $value); 784 } else if ($key || $key === '' || $key === '0') { 745 785 $_arr[$key] = $value; 746 else {786 } else { 747 787 if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } 748 788 else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } … … 762 802 if ($this->_containsGroupAnchor) { 763 803 $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; 804 if (is_array ($value)) { 805 $k = key ($value); 806 if (!is_int ($k)) { 807 $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k; 808 } 809 } 764 810 $this->_containsGroupAnchor = false; 765 811 } 766 767 812 768 813 } … … 780 825 $line = trim ($line); 781 826 if (!strlen($line)) return false; 782 if ($line[0] == '[' && substr ($line, -1, 1) != ']') return true; 827 if (substr ($line, -1, 1) == ']') return false; 828 if ($line[0] == '[') return true; 829 if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; 783 830 return false; 784 831 } … … 787 834 $line = self::stripIndent($line); 788 835 $line = rtrim ($line, "\r\n\t ") . "\n"; 789 if ($line == "\n") $line = '';790 791 836 if ($literalBlockStyle == '|') { 792 837 return $literalBlock . $line; … … 794 839 if (strlen($line) == 0) 795 840 return rtrim($literalBlock, ' ') . "\n"; 796 841 if ($line == "\n" && $literalBlockStyle == '>') { 842 return rtrim ($literalBlock, " \t") . "\n"; 843 } 797 844 if ($line != "\n") 798 845 $line = trim ($line, "\r\n ") . " "; 799 800 846 return $literalBlock . $line; 801 847 } … … 873 919 874 920 921 private static function unquote ($value) { 922 if (!$value) return $value; 923 if (!is_string($value)) return $value; 924 if ($value[0] == '\'') return trim ($value, '\''); 925 if ($value[0] == '"') return trim ($value, '"'); 926 return $value; 927 } 928 875 929 private function startsMappedSequence ($line) { 876 930 return ($line[0] == '-' && substr ($line, -1, 1) == ':'); … … 879 933 private function returnMappedSequence ($line) { 880 934 $array = array(); 881 $key = trim(substr($line,1,-1));935 $key = self::unquote(trim(substr($line,1,-1))); 882 936 $array[$key] = array(); 883 937 $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key); … … 887 941 private function returnMappedValue ($line) { 888 942 $array = array(); 889 $key = trim(substr($line,0,-1));943 $key = self::unquote (trim(substr($line,0,-1))); 890 944 $array[$key] = ''; 891 945 return $array; … … 906 960 private function returnKeyValuePair ($line) { 907 961 $array = array(); 962 $key = ''; 908 963 if (strpos ($line, ':')) { 909 964 // It's a key/value pair most likely … … 921 976 // Set the type of the value. Int, string, etc 922 977 $value = $this->_toType($value); 923 if (empty($key)) { 924 $array[] = $value; 925 } else { 926 $array[$key] = $value; 927 } 928 } 929 978 if ($key === '0') $key = '__!YAMLZero'; 979 $array[$key] = $value; 980 } else { 981 $array = array ($line); 982 } 930 983 return $array; 931 984 … … 948 1001 if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; 949 1002 if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; 950 if (preg_match('/(&['.$symbolsForReference.']+ $)/', $line, $matches)) return $matches[1];1003 if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; 951 1004 if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; 1005 if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; 952 1006 return false; 953 1007 … … 964 1018 return $line; 965 1019 } 966 967 968 1020 } 969 ?> 1021 1022 // Enable use of Spyc from command line 1023 // The syntax is the following: php spyc.php spyc.yaml 1024 1025 define ('SPYC_FROM_COMMAND_LINE', false); 1026 1027 do { 1028 if (!SPYC_FROM_COMMAND_LINE) break; 1029 if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; 1030 if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'spyc.php') break; 1031 $file = $argv[1]; 1032 printf ("Spyc loading file: %s\n", $file); 1033 print_r (spyc_load_file ($file)); 1034 } while (0);
Note: See TracChangeset
for help on using the changeset viewer.