Changeset 58784 in spip-zone


Ignore:
Timestamp:
Feb 23, 2012, 8:09:03 AM (8 years ago)
Author:
fil@…
Message:

mise a jour de spyc en v 0.5

File:
1 edited

Legend:

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

    r32418 r58784  
    22/**
    33   * Spyc -- A Simple PHP YAML Class
    4    * @version 0.4.5
     4   * @version 0.5
    55   * @author Vlad Andersen <vlad.andersen@gmail.com>
    66   * @author Chris Wanstrath <chris@ozmm.org>
    77   * @link http://code.google.com/p/spyc/
    8    * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2009 Vlad Andersen
     8   * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen
    99   * @license http://www.opensource.org/licenses/mit-license.php MIT License
    1010   * @package Spyc
     
    5959  // SETTINGS
    6060
     61  const REMPTY = "\0\0\0\0\0";
     62
    6163  /**
    6264   * Setting this to true will force YAMLDump to enclose any string value in
     
    231233    // Start at the base of the array and move through it.
    232234    if ($array) {
    233       $array = (array)$array;
    234       $first_key = key($array);
    235      
     235      $array = (array)$array;
    236236      $previous_key = -1;
    237237      foreach ($array as $key => $value) {
    238         $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key);
     238        if (!isset($first_key)) $first_key = $key;
     239        $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array);
    239240        $previous_key = $key;
    240241      }
     
    251252     * @param $indent The indent of the current node
    252253     */
    253   private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0) {
     254  private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
    254255    if (is_array($value)) {
    255256      if (empty ($value))
    256         return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key);
     257        return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
    257258      // It has children.  What to do?
    258259      // Make it the right kind of item
    259       $string = $this->_dumpNode($key, NULL, $indent, $previous_key, $first_key);
     260      $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
    260261      // Add the indent
    261262      $indent += $this->_dumpIndent;
     
    264265    } elseif (!is_array($value)) {
    265266      // It doesn't have children.  Yip.
    266       $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key);
     267      $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
    267268    }
    268269    return $string;
     
    280281      $string = '';
    281282      $previous_key = -1;
    282       $first_key = key($array);
    283283      foreach ($array as $key => $value) {
    284         $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key);
     284        if (!isset($first_key)) $first_key = $key;
     285        $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array);
    285286        $previous_key = $key;
    286287      }
     
    299300     * @param $indent The indent of the current node
    300301     */
    301   private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0) {
     302  private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
    302303    // do some folding here, for blocks
    303304    if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false ||
    304305      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      strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 ||
     307      substr ($value, -1, 1) == ':')
    306308    ) {
    307309      $value = $this->_doLiteralBlock($value,$indent);
     
    320322       $value = ($value) ? "true" : "false";
    321323    }
     324   
     325    if ($value === null) $value = 'null';
     326    if ($value === "'" . self::REMPTY . "'") $value = null;
    322327
    323328    $spaces = str_repeat(' ',$indent);
    324329
    325     if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
     330    //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
     331    if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) {
    326332      // It's a sequence
    327333      $string = $spaces.'- '.$value."\n";
    328334    } else {
    329       if ($first_key===0)  throw new Exception('Keys are all screwy.  The first one was zero, now it\'s "'. $key .'"');
     335      // if ($first_key===0)  throw new Exception('Keys are all screwy.  The first one was zero, now it\'s "'. $key .'"');
    330336      // It's mapped
    331       if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; }
    332       $string = $spaces.$key.': '.$value."\n";
     337      if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; }
     338      $string = rtrim ($spaces.$key.': '.$value)."\n";
    333339    }
    334340    return $string;
     
    375381      $value   = ">\n".$indent.$wrapped;
    376382    } else {
    377       if ($this->setting_dump_force_quotes && is_string ($value))
     383      if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY)
    378384        $value = '"' . $value . '"';
    379385    }
     
    421427        $literalBlock = '';
    422428        $line .= $this->LiteralPlaceHolder;
    423 
     429        $literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1]));
    424430        while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) {
    425           $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle);
     431          $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent);
    426432        }
    427433        $i--;
     
    579585    }
    580586
    581     if (intval($first_character) > 0 && preg_match ('/^[1-9]+[0-9]*$/', $value)) {
     587    if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){
    582588      $intvalue = (int)$value;
    583589      if ($intvalue != PHP_INT_MAX)
     
    598604    if (is_numeric($value)) {
    599605      if ($value === '0') return 0;
    600       if (trim ($value, 0) === $value)
     606      if (rtrim ($value, 0) === $value)
    601607        $value = (float)$value;
    602608      return $value;
     
    783789      $_arr = array_merge ($_arr, $value);
    784790    } else if ($key || $key === '' || $key === '0') {
    785       $_arr[$key] = $value;
     791      if (!is_array ($_arr))
     792        $_arr = array ($key=>$value);
     793      else
     794        $_arr[$key] = $value;
    786795    } else {
    787796      if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
     
    831840  }
    832841
    833   private function addLiteralLine ($literalBlock, $line, $literalBlockStyle) {
    834     $line = self::stripIndent($line);
     842  private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) {
     843    $line = self::stripIndent($line, $indent);
     844    if ($literalBlockStyle !== '|') {
     845        $line = self::stripIndent($line);
     846    }
    835847    $line = rtrim ($line, "\r\n\t ") . "\n";
    836848    if ($literalBlockStyle == '|') {
Note: See TracChangeset for help on using the changeset viewer.