You are here

private function Parser::parseBlockScalar in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/yaml/Parser.php \Symfony\Component\Yaml\Parser::parseBlockScalar()

Parses a block scalar.

Parameters

string $style The style indicator that was used to begin this block scalar (| or >):

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

int $indentation The indentation indicator that was used to begin this block scalar:

Return value

string The text value

1 call to Parser::parseBlockScalar()
Parser::parseValue in vendor/symfony/yaml/Parser.php
Parses a YAML value.

File

vendor/symfony/yaml/Parser.php, line 494

Class

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

Namespace

Symfony\Component\Yaml

Code

private function parseBlockScalar($style, $chomping = '', $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();
  }

  // folded style
  if ('>' === $style) {

    // folded lines
    // replace all non-leading/non-trailing single newlines with spaces
    preg_match('/(\\n*)$/', $text, $matches);
    $text = preg_replace('/(?<!\\n|^)\\n(?!\\n)/', ' ', rtrim($text, "\n"));
    $text .= $matches[1];

    // empty separation lines
    // remove one newline from each group of non-leading/non-trailing newlines
    $text = preg_replace('/[^\\n]\\n+\\K\\n(?=[^\\n])/', '', $text);
  }

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