/** * Remove blank lines. * * @param string $source * @param Isolator $isolator * @return string */ protected function normalizeEndings($source, Isolator $isolator) { //Step #1, \n only $source = $isolator->isolatePHP(Strings::normalizeEndings($source)); //Step #2, chunk by lines $sourceLines = explode("\n", $source); //Step #3, no blank lines and html comments (will keep conditional commends) $sourceLines = array_filter($sourceLines, function ($line) { return trim($line); }); $source = $isolator->repairPHP(join("\n", $sourceLines)); $isolator->reset(); return $source; }
/** * Parses tag body for arguments, name, etc. * * @param string $content Tag content to be parsed (from < till >). * @return array */ protected function parseToken($content) { $token = [self::TOKEN_NAME => '', self::TOKEN_TYPE => self::TAG_OPEN, self::TOKEN_CONTENT => '<' . ($content = $this->repairPHP($content)) . '>', self::TOKEN_ATTRIBUTES => []]; //Some parts of text just looks like tags, but their not if (!preg_match('/^\\/?[a-z0-9_:\\/][a-z 0-9\\._\\-:\\/]*/i', $content)) { $token[self::TOKEN_TYPE] = self::PLAIN_TEXT; unset($token[self::TOKEN_NAME], $token[self::TOKEN_NAME]); return $token; } //Local PHP isolation $isolator = new Isolator('-argument-', '-block-', true); //No PHP blocks $content = $isolator->isolatePHP($content); //Parsing arguments, due they already checked for open-close quotas we can use regular expression $attribute = '/(?P<name>[a-z0-9_\\-\\.\\:]+)[ \\n\\t\\r]*(?:(?P<equal>=)[ \\n\\t\\r]*' . '(?P<value>[a-z0-9\\-]+|\'[^\']+\'|\\"[^\\"]+\\"|\\"\\"))?/si'; preg_match_all($attribute, $content, $attributes); foreach ($attributes['value'] as $index => $value) { if ($value && ($value[0] == "'" || $value[0] == '"')) { $value = trim($value, $value[0]); } $name = $this->repairPHP($isolator->repairPHP($attributes['name'][$index])); $token[self::TOKEN_ATTRIBUTES][$name] = $this->repairPHP($isolator->repairPHP($value)); if (empty($attributes['equal'][$index])) { $token[self::TOKEN_ATTRIBUTES][$name] = null; } } //Fetching name $name = $isolator->repairPHP(current(explode(' ', $content))); if ($name[0] == '/') { $token[self::TOKEN_TYPE] = self::TAG_CLOSE; unset($token[self::TOKEN_ATTRIBUTES]); } if ($content[strlen($content) - 1] == '/') { $token[self::TOKEN_TYPE] = self::TAG_SHORT; } $token[self::TOKEN_NAME] = $name = trim($name, '/'); unset($token[self::TOKEN_ATTRIBUTES][$name]); $token[self::TOKEN_NAME] = trim($token[self::TOKEN_NAME]); if ($token[self::TOKEN_TYPE] == self::TAG_OPEN && in_array($token[self::TOKEN_NAME], $this->voidTags)) { $token[self::TOKEN_TYPE] = self::TAG_VOID; } return $token; }