You are here

private function Parser::parseFoldedScalar in Translation template extractor 7.2

Same name and namespace in other branches
  1. 6.3 vendor/Symfony/Component/Yaml/Parser.php \Symfony\Component\Yaml\Parser::parseFoldedScalar()
  2. 7.3 vendor/Symfony/Component/Yaml/Parser.php \Symfony\Component\Yaml\Parser::parseFoldedScalar()

Parses a folded scalar.

Parameters

string $separator The separator that was used to begin this folded scalar (| or >):

string $indicator The indicator that was used to begin this folded scalar (+ or -):

integer $indentation The indentation that was used to begin this folded scalar:

Return value

string The text value

1 call to Parser::parseFoldedScalar()
Parser::parseValue in vendor/Symfony/Component/Yaml/Parser.php
Parses a YAML value.

File

vendor/Symfony/Component/Yaml/Parser.php, line 417

Class

Parser
Parser parses YAML strings to convert them to PHP arrays.

Namespace

Symfony\Component\Yaml

Code

private function parseFoldedScalar($separator, $indicator = '', $indentation = 0) {
  $notEOF = $this
    ->moveToNextLine();
  if (!$notEOF) {
    return '';
  }
  $isCurrentLineBlank = $this
    ->isCurrentLineBlank();
  $text = '';

  // leading blank lines are consumed before determining indentation
  while ($notEOF && $isCurrentLineBlank) {

    // newline only if not EOF
    if ($notEOF = $this
      ->moveToNextLine()) {
      $text .= "\n";
      $isCurrentLineBlank = $this
        ->isCurrentLineBlank();
    }
  }

  // determine indentation if not specified
  if (0 === $indentation) {
    if (preg_match('/^ +/', $this->currentLine, $matches)) {
      $indentation = strlen($matches[0]);
    }
  }
  if ($indentation > 0) {
    $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
    while ($notEOF && ($isCurrentLineBlank || preg_match($pattern, $this->currentLine, $matches))) {
      if ($isCurrentLineBlank) {
        $text .= substr($this->currentLine, $indentation);
      }
      else {
        $text .= $matches[1];
      }

      // newline only if not EOF
      if ($notEOF = $this
        ->moveToNextLine()) {
        $text .= "\n";
        $isCurrentLineBlank = $this
          ->isCurrentLineBlank();
      }
    }
  }
  elseif ($notEOF) {
    $text .= "\n";
  }
  if ($notEOF) {
    $this
      ->moveToPreviousLine();
  }

  // replace all non-trailing single newlines with spaces in folded blocks
  if ('>' === $separator) {
    preg_match('/(\\n*)$/', $text, $matches);
    $text = preg_replace('/(?<!\\n)\\n(?!\\n)/', ' ', rtrim($text, "\n"));
    $text .= $matches[1];
  }

  // deal with trailing newlines as indicated
  if ('' === $indicator) {
    $text = preg_replace('/\\n+$/s', "\n", $text);
  }
  elseif ('-' === $indicator) {
    $text = preg_replace('/\\n+$/s', '', $text);
  }
  return $text;
}