restoreState() public method

public restoreState ( CursorState $state )
$state CursorState
 public static function parse(Cursor $cursor)
 {
     if (null === self::$regexp) {
         $regex = RegexHelper::getInstance();
         self::$regexp = sprintf('/^\\s*([.#][_a-z0-9-]+|%s%s)(?<!})\\s*/i', $regex->getPartialRegex(RegexHelper::ATTRIBUTENAME), $regex->getPartialRegex(RegexHelper::ATTRIBUTEVALUESPEC));
     }
     $state = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     if ('{' !== $cursor->getCharacter()) {
         $cursor->restoreState($state);
         return [];
     }
     $cursor->advanceBy(1);
     if (':' === $cursor->getCharacter()) {
         $cursor->advanceBy(1);
     }
     $attributes = [];
     while ($attribute = trim($cursor->match(self::$regexp))) {
         if ('#' === $attribute[0]) {
             $attributes['id'] = substr($attribute, 1);
             continue;
         }
         if ('.' === $attribute[0]) {
             $attributes['class'][] = substr($attribute, 1);
             continue;
         }
         list($name, $value) = explode('=', $attribute, 2);
         $first = $value[0];
         $last = substr($value, -1);
         if (('"' === $first && '"' === $last || "'" === $first && "'" === $last) && strlen($value) > 1) {
             $value = substr($value, 1, -1);
         }
         if ('class' === strtolower(trim($name))) {
             foreach (array_filter(explode(' ', trim($value))) as $class) {
                 $attributes['class'][] = $class;
             }
         } else {
             $attributes[trim($name)] = trim($value);
         }
     }
     if (0 === $cursor->advanceWhileMatches('}')) {
         $cursor->restoreState($state);
         return [];
     }
     if (isset($attributes['class'])) {
         $attributes['class'] = implode(' ', $attributes['class']);
     }
     return $attributes;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     if ($cursor->getFirstNonSpaceCharacter() !== '<') {
         return false;
     }
     $savedState = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     $line = $cursor->getRemainder();
     for ($blockType = 1; $blockType <= 7; $blockType++) {
         $match = RegexHelper::matchAt(RegexHelper::getHtmlBlockOpenRegex($blockType), $line);
         if ($match !== null && ($blockType < 7 || !$context->getContainer() instanceof Paragraph)) {
             $cursor->restoreState($savedState);
             $context->addBlock(new HtmlBlock($blockType));
             $context->setBlocksParsed(true);
             return true;
         }
     }
     $cursor->restoreState($savedState);
     return false;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $previousState = $cursor->saveState();
     $indent = $cursor->advanceToFirstNonSpace();
     $fence = $cursor->match('/^`{3,}(?!.*`)|^~{3,}(?!.*~)/');
     if (!$fence) {
         $cursor->restoreState($previousState);
         return false;
     }
     // fenced code block
     $fenceLength = strlen($fence);
     $context->addBlock(new FencedCode($fenceLength, $fence[0], $indent));
     return true;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     $previousState = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     $fence = $cursor->match('/^\\[TOC\\]/');
     if (is_null($fence)) {
         $cursor->restoreState($previousState);
         return false;
     }
     $context->addBlock(new TableOfContents());
     return true;
 }
Beispiel #5
0
 /**
  * @param Cursor $cursor
  * @param int    $markerLength
  *
  * @return int
  */
 private function calculateListMarkerPadding(Cursor $cursor, $markerLength)
 {
     $start = $cursor->saveState();
     $spacesStartCol = $cursor->getColumn();
     while ($cursor->getColumn() - $spacesStartCol < 5) {
         if (!$cursor->advanceBySpaceOrTab()) {
             break;
         }
     }
     $blankItem = $cursor->peek() === null;
     $spacesAfterMarker = $cursor->getColumn() - $spacesStartCol;
     if ($spacesAfterMarker >= 5 || $spacesAfterMarker < 1 || $blankItem) {
         $cursor->restoreState($start);
         $cursor->advanceBySpaceOrTab();
         return $markerLength + 1;
     }
     return $markerLength + $spacesAfterMarker;
 }
Beispiel #6
0
 /**
  * @return bool
  */
 protected function cancel()
 {
     $this->cursor->restoreState($this->originalState);
     return false;
 }
 /**
  * @param Cursor $cursor
  * @param int    $markerLength
  *
  * @return int
  */
 private function calculateListMarkerPadding(Cursor $cursor, $markerLength)
 {
     $start = $cursor->saveState();
     $spacesStartCol = $cursor->getColumn();
     $spacesStartOffset = $cursor->getPosition();
     do {
         $cursor->advanceBy(1, true);
         $nextChar = $cursor->getCharacter();
     } while ($cursor->getColumn() - $spacesStartCol < 5 && ($nextChar === ' ' || $nextChar === "\t"));
     $blankItem = $cursor->peek() === null;
     $spacesAfterMarker = $cursor->getColumn() - $spacesStartCol;
     if ($spacesAfterMarker >= 5 || $spacesAfterMarker < 1 || $blankItem) {
         $cursor->restoreState($start);
         if ($cursor->peek() === ' ') {
             $cursor->advanceBy(1, true);
         }
         return $markerLength + 1;
     } else {
         return $markerLength + $spacesAfterMarker;
     }
 }