Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Parser.php

function 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:

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

File

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

Class

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

Namespace

Symfony\Component\Yaml

Code

private function parseBlockScalar(string $style, string $chomping = '', int $indentation = 0) : string {
    $notEOF = $this->moveToNextLine();
    if (!$notEOF) {
        return '';
    }
    $isCurrentLineBlank = $this->isCurrentLineBlank();
    $blockLines = [];
    // leading blank lines are consumed before determining indentation
    while ($notEOF && $isCurrentLineBlank) {
        // newline only if not EOF
        if ($notEOF = $this->moveToNextLine()) {
            $blockLines[] = '';
            $isCurrentLineBlank = $this->isCurrentLineBlank();
        }
    }
    // determine indentation if not specified
    if (0 === $indentation) {
        $currentLineLength = \strlen($this->currentLine);
        for ($i = 0; $i < $currentLineLength && ' ' === $this->currentLine[$i]; ++$i) {
            ++$indentation;
        }
    }
    if ($indentation > 0) {
        $pattern = \sprintf('/^ {%d}(.*)$/', $indentation);
        while ($notEOF && ($isCurrentLineBlank || self::preg_match($pattern, $this->currentLine, $matches))) {
            if ($isCurrentLineBlank && \strlen($this->currentLine) > $indentation) {
                $blockLines[] = substr($this->currentLine, $indentation);
            }
            elseif ($isCurrentLineBlank) {
                $blockLines[] = '';
            }
            else {
                $blockLines[] = $matches[1];
            }
            // newline only if not EOF
            if ($notEOF = $this->moveToNextLine()) {
                $isCurrentLineBlank = $this->isCurrentLineBlank();
            }
        }
    }
    elseif ($notEOF) {
        $blockLines[] = '';
    }
    if ($notEOF) {
        $blockLines[] = '';
        $this->moveToPreviousLine();
    }
    elseif (!$this->isCurrentLineLastLineInDocument()) {
        $blockLines[] = '';
    }
    // folded style
    if ('>' === $style) {
        $text = '';
        $previousLineIndented = false;
        $previousLineBlank = false;
        for ($i = 0, $blockLinesCount = \count($blockLines); $i < $blockLinesCount; ++$i) {
            if ('' === $blockLines[$i]) {
                $text .= "\n";
                $previousLineIndented = false;
                $previousLineBlank = true;
            }
            elseif (' ' === $blockLines[$i][0]) {
                $text .= "\n" . $blockLines[$i];
                $previousLineIndented = true;
                $previousLineBlank = false;
            }
            elseif ($previousLineIndented) {
                $text .= "\n" . $blockLines[$i];
                $previousLineIndented = false;
                $previousLineBlank = false;
            }
            elseif ($previousLineBlank || 0 === $i) {
                $text .= $blockLines[$i];
                $previousLineIndented = false;
                $previousLineBlank = false;
            }
            else {
                $text .= ' ' . $blockLines[$i];
                $previousLineIndented = false;
                $previousLineBlank = false;
            }
        }
    }
    else {
        $text = implode("\n", $blockLines);
    }
    // deal with trailing newlines
    if ('' === $chomping) {
        $text = preg_replace('/\\n+$/', "\n", $text);
    }
    elseif ('-' === $chomping) {
        $text = preg_replace('/\\n+$/', '', $text);
    }
    return $text;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal