Processes documents

@param string $text
@param mixed $args
@return string
484763
protected function processDocuments($text, $args)
    {
        list($matches, $ids) = $this->getDocumentMatches($text);

        $documents = $this->getDocuments($ids);

        return $this->makeDocuments($matches, $ids, $documents, $text);
    }
681573
public static function plainProcessor($text)
    {
        $text = static::emailToLink($text);
        $text = static::urlToLink($text);
        $text = nl2br($text);

        return $text;
    }
124217
public function render($text)
    {
        $text = trim(str_replace(array("\r\n", "\r"), "\n", $text), "\n");

        foreach ($this->rules as $regex => $replacement) {
            if (is_callable($replacement)) {
                $text = preg_replace_callback($regex, $replacement, $text);
            } else {
                $text = preg_replace($regex, $replacement, $text);
            }
        }

        return trim($text);
    }
192815
private function decode($text)
    {
        try {
            if (!$this->pandoc) {
                $this->pandoc = new Pandoc();
            }

            return $this->pandoc->runWith($text, [
                'from' => 'latex',
                'to' => 'plain',
            ]);
        } catch (PandocException $exception) {
            throw new ProcessorException(
                sprintf(
                    'Error while processing LaTeX to Unicode: %s',
                    $exception->getMessage()
                ),
                0,
                $exception
            );
        }
    }
219983
private function getCmd(array $args)
    {
        // get path to console from kernel..
        $consolePath = $this->kernel->getRootDir() . '/console';

        // this code was copied from Symfony\Component\Process\PhpProcess and deserves a cleanup
        $executableFinder = new PhpExecutableFinder();
        if (false === $php = $executableFinder->find()) {
            $php = null;
        }
        if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) {
            // exec is mandatory to deal with sending a signal to the process
            // see https://github.com/symfony/symfony/issues/5030 about prepending
            // command with exec
            $php = 'exec '.$php;
        }

        $cmd = $php.' '.$consolePath.' -n ';

        foreach ($args as $key => $val) {
            if (strlen($key) > 1) {
                $cmd .= ' ' . $key;
            }
            if (strlen($key) > 1 && !is_null($val)) {
                $cmd .= '=';
            }
            if (strlen($val) > 1) {
                $cmd .= escapeshellarg($val);
            }
        }

        return $cmd;
    }
1095749
public static function t($msg, $args = array())
    {
        assert('is_string($msg)');
        if (extension_loaded('gettext')) {
            bindtextdomain("phptoolkit", dirname(dirname(__DIR__)).'/locale');
            textdomain('phptoolkit');

            $translatedMsg = gettext($msg);
        } else {
            $translatedMsg = $msg;
        }
        if (!empty($args)) {
            $params = array_merge(array($translatedMsg), $args);
            $translatedMsg = call_user_func_array('sprintf', $params);
        }
        return $translatedMsg;
    }
695731
protected final function rules(&$text, $rules = null) {
        if($rules === null) {
            $rules = array('except' => array());
        }
        else if(is_array($rules) && count($rules) > 1) {
            $rules = array('only' => $rules);
        }

        if(isset($rules['only']) && is_array($rules['only'])) {
            $rules = $rules['only'];
            foreach($rules as $r) {
                if(!empty($this->tagrules[$r])) {
                    $this->process_tag_rule($r, $this->tagrules[$r], $text);
                }
            }
        }
        else if(isset($rules['except']) && is_array($rules['except'])) {
            $rules = $rules['except'];
            foreach($this->tagrules as $r => $tr) {
                if(!in_array($r, $rules)) {
                    $this->process_tag_rule($r, $tr, $text);
                }
            }
        }
    }
1030527
public static function parse($text, $context = null)
    {
        if (!static::$_enabled || strpos($text, '{') === false) {
            return $text;
        }

        if ($context === null) {
            $context = static::_getDefaultContext();
        }

        static::cache('context', $context);
        $pattern = static::_regex();

        return preg_replace_callback("/{$pattern}/s", 'static::_doShortcode', $text);
    }
436504
public static function parse($text) {
    static $int_regex = <<<'EOF'
/^[+-]?(?:
  0
  | [1-9][0-9]*
  | 0[xX][0-9a-fA-F]+
  | 0[0-7]+
  | 0b[01]+
)$/x
EOF;
    static $decimal_regex = <<<EOF
/^[+-]?(?:
  [0-9]*\.[0-9]+ (?:[eE][+-]?[0-9]+)?
  | [0-9]+\.[0-9]* (?:[eE][+-]?[0-9]+)?
  | [0-9]+[eE][+-]?[0-9]+
)$/x
EOF;
    static $var_regex = '/^\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
    switch ($text) {
      case 'abstract':
        return static::_abstract();
      case 'array':
        return static::_array();
      case 'as':
        return static::_as();
      case 'break':
        return static::_break();
      case 'callable':
        return static::_callable();
      case 'case':
        return static::_case();
      case 'catch':
        return static::_catch();
      case 'class':
        return static::_class();
      case 'clone':
        return static::_clone();
      case 'const':
        return static::_const();
      case 'continue':
        return static::_continue();
      case 'declare':
        return static::_declare();
      case 'default':
        return static::_default();
      case 'die':
        return static::_die();
      case 'do':
        return static::_do();
      case 'echo':
        return static::_echo();
      case 'else':
        return static::_else();
      case 'elseif':
        return static::_elseIf();
      case 'empty':
        return static::_empty();
      case 'enddeclare':
        return static::_endDeclare();
      case 'endfor':
        return static::_endFor();
      case 'endforeach':
        return static::_endForeach();
      case 'endif':
        return static::_endIf();
      case 'endswitch':
        return static::_endSwitch();
      case 'endwhile':
        return static::_endWhile();
      case 'eval':
        return static::_eval();
      case 'exit':
        return static::_exit();
      case 'extends':
        return static::_extends();
      case 'final':
        return static::_final();
      case 'finally':
        return static::_finally();
      case 'for':
        return static::_for();
      case 'foreach':
        return static::_foreach();
      case 'function':
        return static::_function();
      case 'global':
        return static::_global();
      case 'goto':
        return static::_goto();
      case 'if':
        return static::_if();
      case 'implements':
        return static::_implements();
      case 'include':
        return static::_include();
      case 'include_once':
        return static::_includeOnce();
      case 'instanceof':
        return static::_instanceOf();
      case 'insteadof':
        return static::_insteadOf();
      case 'interface':
        return static::_interface();
      case 'isset':
        return static::_isset();
      case 'list':
        return static::_list();
      case 'namespace':
        return static::_namespace();
      case 'new':
        return static::_new();
      case 'print':
        return static::_print();
      case 'private':
        return static::_private();
      case 'protected':
        return static::_protected();
      case 'public':
        return static::_public();
      case 'require':
        return static::_require();
      case 'require_once':
        return static::_requireOnce();
      case 'return':
        return static::_return();
      case 'static':
        return static::_static();
      case 'switch':
        return static::_switch();
      case 'throw':
        return static::_throw();
      case 'trait':
        return static::_trait();
      case 'try':
        return static::_try();
      case 'unset':
        return static::_unset();
      case 'use':
        return static::_use();
      case 'var':
        return static::_var();
      case 'while':
        return static::_while();
      case 'yield':
        return static::_yield();
      case 'and':
        return static::logicalAnd();
      case 'or':
        return static::logicalOr();
      case 'xor':
        return static::logicalXor();
      case '(array)':
        return static::arrayCast();
      case '(bool)':
      case '(boolean)':
        return static::booleanCast();
      case '(real)':
      case '(double)':
      case '(float)':
        return static::doubleCast();
      case '(int)':
      case '(integer)':
        return static::integerCast();
      case '(object)':
        return static::objectCast();
      case '(string)':
        return static::stringCast();
      case '(unset)':
        return static::unsetCast();
      case '__halt_compiler':
        return static::haltCompiler();
      case '__CLASS__':
        return static::classConstant();
      case '__DIR__':
        return static::dirConstant();
      case '__FILE__':
        return static::fileConstant();
      case '__FUNCTION__':
        return static::functionConstant();
      case '__LINE__':
        return static::lineConstant();
      case '__METHOD__':
        return static::methodConstant();
      case '__NAMESPACE__':
        return static::namespaceConstant();
      case '__TRAIT__':
        return static::traitConstant();
      case '...':
        return static::splat();
      case '=>':
        return static::doubleArrow();
      case '->':
        return static::objectOperator();
      case '::':
        return static::doubleColon();
      case '&=':
        return static::bitwiseAndAssign();
      case '|=':
        return static::bitwiseOrAssign();
      case '^=':
        return static::bitwiseXorAssign();
      case '*=':
        return static::multiplyAssign();
      case '/=':
        return static::divideAssign();
      case '%=':
        return static::modulusAssign();
      case '+=':
        return static::addAssign();
      case '-=':
        return static::subtractAssign();
      case '.=':
        return static::concatAssign();
      case '===':
        return static::isIdentical();
      case '==':
        return static::isEqual();
      case '!==':
        return static::isNotIdentical();
      case '!=':
        return static::isNotEqual();
      case '<=':
        return static::isLessThanOrEqual();
      case '>=':
        return static::isGreaterThanOrEqual();
      case '&&':
        return static::booleanAnd();
      case '||':
        return static::booleanOr();
      case '<<':
        return static::bitwiseShiftLeft();
      case '<<=':
        return static::bitwiseShiftLeftAssign();
      case '>>':
        return static::bitwiseShiftRight();
      case '>>=':
        return static::bitwiseShiftRightAssign();
      case '\\':
        return static::namespaceSeparator();
      case '--':
        return static::decrement();
      case '++':
        return static::increment();
      case '`':
      case '~':
      case '!':
      case '@':
      case '%':
      case '^':
      case '&':
      case '*':
      case '(':
      case ')':
      case '-':
      case '+':
      case '=':
      case '{':
      case '}':
      case '[':
      case ']':
      case '|':
      case ':':
      case ';':
      case "'":
      case '"':
      case '<':
      case ',':
      case '>':
      case '.':
      case '?':
      case '/':
        return new TokenNode($text, $text);
      case ' ':
        return static::space();
      case '?>':
        return static::closeTag();
      default:
        if (rtrim($text) === '<?php') {
          return static::openTag();
        }
        elseif (preg_match($int_regex, $text)) {
          return static::integer($text);
        }
        elseif (preg_match($decimal_regex, $text)) {
          return static::decimalNumber($text);
        }
        elseif (preg_match($var_regex, $text)) {
          return static::variable($text);
        }
        // @todo handle all tokens as per http://php.net/manual/en/tokens.php
        throw new \InvalidArgumentException("Unable to parse '{$text}'");
    }
  }
1122529
protected function processDocBlock($text, array $uses = []): array
    {
        $parser = new DocBlockParser($text);

        $rtn = [
            'params' => [],
            'return' => null,
            'inheritdoc' => false,
        ];

        if (isset($parser->tags['param'])) {
            $rtn = $this->processDocBlockParams($parser->tags['param'], $uses, $rtn);
        }

        if (isset($parser->tags['return'])) {
            $rtn['return'] = $this->getDocBlockReturnType($parser->tags['return'], $uses);
        }

        if (stripos($parser->comment, '@inheritdoc') !== false) {
            $rtn['inheritdoc'] = true;
        }

        return $rtn;
    }
231553
protected function parseArgument($arg)
    {
        //if it's a literal string value
        if (strpos($arg, '"') === 0
            || strpos($arg, "'") === 0
        ) {
            return "'" . str_replace("'", '\\\'', substr($arg, 1, -1)) . "'";
        }
        
        //if it's null
        if (strtolower($arg) === 'null'
            || strtolower($arg) === 'true'
            || strtolower($arg) === 'false'
            || is_numeric($arg)
        ) {
            return $arg;
        }
        
        $arg = str_replace(array('[', ']', '(', ')'), '', $arg);
        $arg = str_replace("'", '\\\'', $arg);
        return sprintf(self::BLOCK_ARGUMENT_VALUE, $arg);
    }
790587
public static function extract_arguments($text, $begin = null, $end = null)
	{
		$argv = [];
		if(!empty($text))
		{
			$args = explode(',', $text);
			foreach ($args as $a) {
				// trim the argument
				$a = trim( $this->trim_string($a) );
				// parse null
				if(strtolower($a) == "null")
					$a = null;
				array_push($argv, $a);
			}
		}
		return $argv;
	}
633707
public function run($args)
  {
    $this->args = $args;
    $command = $this->args['commands'][1];
    $path = $this->root.DIRECTORY_SEPARATOR.'cmd'.DIRECTORY_SEPARATOR.$command;
    if(!is_dir($path)){
      print $command.' doesn\'t appear to be a valid command!';
      return;
    }
    $file = $path.DIRECTORY_SEPARATOR.$this->helpFile;
    if(stream_resolve_include_path($file)){
       print file_get_contents($file);
    }
  }
548625
public function loadGrammar(string $text): Grammar
    {
        $this->symbols = [];
        $this->inline = [];
        $this->regexpMap = [];
        $this->defines = [];
        $this->whitespaces = [];
        $this->modifiers = null;

        try {
            /** @var Rule[] $rules */
            $rules = $this->parser->parse($text, $this->actions)->made();

            /** @var int[] $subject_rules_count */
            $subject_rules_count = [];
            /** @var int[] $inline_ref_count */
            $inline_ref_count = [];
            /** @var Rule[] $inline_ref_rule */
            $inline_ref_rule = [];

            $non_terminal_is_hidden = [];
            $hidden_non_terminals = [];

            foreach ($rules as $rule) {
                $subject = $rule->getSubject();
                $subject_name = $subject->getName();
                $subject_hidden = $subject->isHidden();

                // RegExp tokens cannot be non-terminals in the same time
                // since there is no anonymous RegExp terminals
                if (isset($this->regexpMap[$subject_name])) {
                    throw new GrammarException(
                        "Symbol `$subject_name` defined as non-terminal and as regexp terminal in the same time"
                    );
                }

                if (isset($non_terminal_is_hidden[$subject_name])) {
                    if ($subject_hidden !== $non_terminal_is_hidden[$subject_name]) {
                        throw new GrammarException(
                            "Symbol `$subject_name` defined both as hidden and as visible"
                        );
                    }
                } else {
                    $non_terminal_is_hidden[$subject_name] = $subject_hidden;
                }

                $subject_rules_count[$subject_name] = ($subject_rules_count[$subject_name] ?? 0) + 1;

                if ($subject_hidden) {
                    $hidden_non_terminals[$subject_name] = true;
                }

                foreach ($rule->getDefinition() as $symbol) {
                    $name = $symbol->getName();
                    if (isset($this->inline[$name])) {
                        $inline_ref_count[$name] = ($inline_ref_count[$name] ?? 0) + 1;

                        // it is needed in case of the only reference,
                        // so array of an subject is not needed
                        $inline_ref_rule[$name] = $rule;
                    }
                }
            }

            foreach ($hidden_non_terminals as $name => $_) {
                if (isset($this->symbols[$name][false])) {
                    $this->symbols[$name][false]->setIsHidden(true);
                }
            }

            // Convert non-terminals defined only once with inline tokens
            // `Subject: "inline"`
            // into fixed terminals
            $fixed = [];
            foreach ($inline_ref_count as $inline_token => $ref_count) {
                if (1 !== $ref_count) {
                    continue;
                }

                $rule = $inline_ref_rule[$inline_token];
                if (null === $rule->getTag() && 1 === count($rule->getDefinition())) {
                    $subject = $rule->getSubject();
                    $name = $subject->getName();

                    if (1 !== $subject_rules_count[$name]) {
                        continue;
                    }

                    // remove rule
                    $key = array_search($rule, $rules, true);
                    if (false !== $key) {
                        unset($rules[$key]);
                    }

                    // remove inline
                    unset($this->inline[$inline_token]);

                    // make terminal
                    $subject->setIsTerminal(true);
                    if (isset($this->symbols[$name][!$subject->isHidden()])) {
                        $this->symbols[$name][!$subject->isHidden()]->setIsTerminal(true);
                    }

                    $fixed[$name] = $inline_token;
                }
            }

            return new Grammar(
                $rules,
                $this->inline,
                $fixed,
                $this->regexpMap,
                $this->whitespaces,
                $this->defines,
                $this->modifiers ?? ''
            );
        } catch (UnknownCharacterException | UnexpectedInputAfterEndException | UnexpectedTokenException $e) {
            throw new GrammarException(
                'Cannot parse grammar: ' . $e->getMessage() . ' at offset ' . $e->getOffset(),
                0,
                $e
            );
        } catch (AbortedException $e) {
            for ($inner = $e; $inner; $inner = $inner->getPrevious()) {
                if ($inner instanceof GrammarException || $inner instanceof InternalException) {
                    throw $inner;
                }
            }
            throw new InternalException('Failure while parsing your grammar text', 0, $e);
        } finally {
            $this->symbols = null;
            $this->inline = null;
            $this->regexpMap = null;
            $this->defines = null;
            $this->whitespaces = null;
            $this->modifiers = null;
        }
    }
323842
public function parse($text, array $data, $purgePlaceholders = true)
    {
        foreach ($this->parsers as $parser) {
            $text = $parser->parse($text, $data, false);
        }

        return $purgePlaceholders ? $this->purge($text) : $text;
    }
1091000
public static function serialize(array $args)
    {
        return array_reduce(
            $args,
            function ($msg, $arg) {
                return $msg.'$'.strlen($arg)."\r\n".$arg."\r\n";
            },
            '*'.count($args)."\r\n"
        );
    }
902303
static function parse_text($text)
    {
        $text = trim($text);
        $result = array();
        $lines = explode(self::$row_sep, $text);
        foreach ($lines as $line)
        {
            $result[] = self::parse_line($line);
        }
        return $result;
    }