Changeset 32418 in spip-zone


Ignore:
Timestamp:
Oct 28, 2009, 2:41:21 PM (11 years ago)
Author:
fil@…
Message:

spyc en version 0.4.5 (pas bcp mieux qu'avant)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • _plugins_/yaml/spyc/spyc.php

    r31640 r32418  
    22/**
    33   * Spyc -- A Simple PHP YAML Class
    4    * @version 0.4.1
     4   * @version 0.4.5
     5   * @author Vlad Andersen <vlad.andersen@gmail.com>
    56   * @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/
    88   * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen
    99   * @license http://www.opensource.org/licenses/mit-license.php MIT License
     
    230230
    231231    // 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    }
    238242    return $string;
    239243  }
     
    247251     * @param $indent The indent of the current node
    248252     */
    249   private function _yamlize($key,$value,$indent, $previous_key = -1) {
     253  private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0) {
    250254    if (is_array($value)) {
    251255      if (empty ($value))
    252         return $this->_dumpNode($key, array(), $indent, $previous_key);
     256        return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key);
    253257      // It has children.  What to do?
    254258      // 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);
    256260      // Add the indent
    257261      $indent += $this->_dumpIndent;
     
    260264    } elseif (!is_array($value)) {
    261265      // 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);
    263267    }
    264268    return $string;
     
    276280      $string = '';
    277281      $previous_key = -1;
     282      $first_key = key($array);
    278283      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);
    280285        $previous_key = $key;
    281286      }
     
    294299     * @param $indent The indent of the current node
    295300     */
    296   private function _dumpNode($key, $value, $indent, $previous_key = -1) {
     301  private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) {
    297302    // 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    ) {
    301307      $value = $this->_doLiteralBlock($value,$indent);
    302308    } else {
    303309      $value  = $this->_doFolding($value,$indent);
    304       if (is_bool($value)) {
    305         $value = ($value) ? "true" : "false";
    306       }
    307310    }
    308311
    309312    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    }
    310322
    311323    $spaces = str_repeat(' ',$indent);
    312324
    313     if (is_int($key) && $key - 1 == $previous_key) {
     325    if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
    314326      // It's a sequence
    315327      $string = $spaces.'- '.$value."\n";
    316328    } else {
     329      if ($first_key===0)  throw new Exception('Keys are all screwy.  The first one was zero, now it\'s "'. $key .'"');
    317330      // It's mapped
    318331      if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; }
     
    330343     */
    331344  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    }
    332352    $exploded = explode("\n",$value);
    333353    $newValue = '|';
     
    335355    $spaces   = str_repeat(' ',$indent);
    336356    foreach ($exploded as $line) {
    337       $newValue .= "\n" . $spaces . trim($line);
     357      $newValue .= "\n" . $spaces . ($line);
    338358    }
    339359    return $newValue;
     
    349369    // Don't do anything if wordwrap is set to 0
    350370
    351     if ($this->_dumpWordWrap !== 0 && strlen($value) > $this->_dumpWordWrap) {
     371    if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) {
    352372      $indent += $this->_dumpIndent;
    353373      $indent = str_repeat(' ',$indent);
     
    378398    if (empty ($Source)) return array();
    379399    if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) {
    380       $array = syck_load ($Source);
     400      $array = syck_load (implode ('', $Source));
    381401      return is_array($array) ? $array : array();
    382402    }
     
    415435
    416436
     437      if (strpos ($line, '#')) {
     438        if (strpos ($line, '"') === false && strpos ($line, "'") === false)
     439          $line = preg_replace('/\s+#(.+)$/','',$line);
     440      }
     441
    417442      $lineArray = $this->_parseLine($line);
    418443
     
    456481    $line = trim($line);
    457482    if (!$line) return array();
     483
    458484    $array = array();
    459485
     
    503529      return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\''));
    504530   
    505     if (strpos($value, ' #') !== false)
     531    if (strpos($value, ' #') !== false && !$is_quoted)
    506532      $value = preg_replace('/\s+#(.+)$/','',$value);
     533
     534    if (!$is_quoted) $value = str_replace('\n', "\n", $value);
    507535
    508536    if ($first_character == '[' && $last_character == ']') {
     
    690718  }
    691719
     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
    692732  private function addArrayInline ($array, $indent) {
    693733      $CommonGroupPath = $this->path;
     
    700740      return true;
    701741  }
    702  
     742
    703743  private function addArray ($incoming_data, $incoming_indent) {
     744
     745   // print_r ($incoming_data);
    704746
    705747    if (count ($incoming_data) > 1)
     
    707749   
    708750    $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';
    710753
    711754    if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values.
    712       if ($key) {
     755      if ($key || $key === '' || $key === '0') {
    713756        $this->result[$key] = $value;
    714       }  else {
     757      } else {
    715758        $this->result[] = $value; end ($this->result); $key = key ($this->result);
    716759      }
     
    729772
    730773    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);
    739775      $this->_containsGroupAlias = false;
    740776    }
     
    742778
    743779    // 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') {
    745785      $_arr[$key] = $value;
    746     else {
     786    } else {
    747787      if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
    748788      else { $_arr[] = $value; end ($_arr); $key = key ($_arr); }
     
    762802    if ($this->_containsGroupAnchor) {
    763803      $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      }
    764810      $this->_containsGroupAnchor = false;
    765811    }
    766 
    767812
    768813  }
     
    780825    $line = trim ($line);
    781826    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;
    783830    return false;
    784831  }
     
    787834    $line = self::stripIndent($line);
    788835    $line = rtrim ($line, "\r\n\t ") . "\n";
    789     if ($line == "\n") $line = '';
    790 
    791836    if ($literalBlockStyle == '|') {
    792837      return $literalBlock . $line;
     
    794839    if (strlen($line) == 0)
    795840      return rtrim($literalBlock, ' ') . "\n";
    796 
     841    if ($line == "\n" && $literalBlockStyle == '>') {
     842      return rtrim ($literalBlock, " \t") . "\n";
     843    }
    797844    if ($line != "\n")
    798845      $line = trim ($line, "\r\n ") . " ";
    799 
    800846    return $literalBlock . $line;
    801847  }
     
    873919
    874920
     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
    875929  private function startsMappedSequence ($line) {
    876930    return ($line[0] == '-' && substr ($line, -1, 1) == ':');
     
    879933  private function returnMappedSequence ($line) {
    880934    $array = array();
    881     $key         = trim(substr($line,1,-1));
     935    $key         = self::unquote(trim(substr($line,1,-1)));
    882936    $array[$key] = array();
    883937    $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key);
     
    887941  private function returnMappedValue ($line) {
    888942    $array = array();
    889     $key         = trim(substr($line,0,-1));
     943    $key         = self::unquote (trim(substr($line,0,-1)));
    890944    $array[$key] = '';
    891945    return $array;
     
    906960  private function returnKeyValuePair ($line) {
    907961    $array = array();
     962    $key = '';
    908963    if (strpos ($line, ':')) {
    909964      // It's a key/value pair most likely
     
    921976      // Set the type of the value.  Int, string, etc
    922977      $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    }
    930983    return $array;
    931984
     
    9481001    if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
    9491002    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];
    9511004    if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1];
     1005    if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1];
    9521006    return false;
    9531007
     
    9641018    return $line;
    9651019  }
    966 
    967 
    9681020}
    969 ?>
     1021
     1022// Enable use of Spyc from command line
     1023// The syntax is the following: php spyc.php spyc.yaml
     1024
     1025define ('SPYC_FROM_COMMAND_LINE', false);
     1026
     1027do {
     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.