Ejemplo n.º 1
0
 protected function parse(Tokens $tokens)
 {
     $tokens = $this->benchmark(function () use($tokens) {
         return $this->_language->parse($tokens);
     }, $this->_times['parsing']);
     $this->_counts['after'] = count($tokens);
     if ($this->wants('tree-after')) {
         $this->_tree($tokens, 'after parsing', true);
     }
     return $tokens;
 }
Ejemplo n.º 2
0
 public function transform($text)
 {
     $parser = new MarkdownExtra();
     $stylesAdded = false;
     if (class_exists('Kadet\\Highlighter\\Language\\Language')) {
         $parser->code_block_content_func = function ($code, $language) use(&$stylesAdded) {
             if (!$stylesAdded) {
                 $this->pageStack->getPageResponse()->addCssFile('@Jarves/keylighter/default.scss');
                 $stylesAdded = true;
             }
             return Highlighter\highlight($code, Language::byName($language));
         };
     }
     return $parser->transform($text);
 }
Ejemplo n.º 3
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @param                 $filename
  * @param                 $formatter
  */
 protected function process(InputInterface $input, OutputInterface $output, $filename, $formatter)
 {
     $language = $input->getOption('language') ? KeyLighter::get()->getLanguage($input->getOption('language')) : Language::byFilename($filename);
     if (!($source = $this->content($filename))) {
         throw new InvalidArgumentException(sprintf('Specified file %s doesn\'t exist, check if given path is correct.', $filename));
     }
     if ($output->isVerbose()) {
         $output->writeln(sprintf("Used file: <path>%s</path>, Language: <language>%s</language>, Formatter: <formatter>%s</formatter>", $filename, $language->getFQN(), get_class($formatter)));
         $verbose = new VerboseOutput($output, $input, $language, $formatter, $source);
         $formatted = $verbose->process();
     } else {
         $formatted = KeyLighter::get()->highlight($source, $language, $formatter);
     }
     if (!$input->getOption('no-output')) {
         $output->writeln($formatted);
     }
 }
 /**
  * @param OutputInterface $output
  * @param                 $class
  *
  * @return array|false
  */
 protected function process(OutputInterface $output, $class)
 {
     $reflection = new \ReflectionClass($class);
     if ($reflection->isAbstract()) {
         $output->writeln(sprintf('<language>%s</language> is abstract, skipping...', $reflection->name), OutputInterface::VERBOSITY_VERBOSE);
         return false;
     }
     if (!$reflection->isSubclassOf(Language::class)) {
         $output->writeln(sprintf('<language>%s</language> is not Language, skipping...', $reflection->name), OutputInterface::VERBOSITY_VERBOSE);
         return false;
     }
     if ($reflection->getMethod('getMetadata')->getDeclaringClass()->name !== $reflection->name) {
         $output->writeln(sprintf('<language>%s</language>::<info>getAliases</info> is not declared, skipping...', $reflection->name), OutputInterface::VERBOSITY_VERBOSE);
         return false;
     }
     $result = array_merge([$reflection->name], array_replace(Language::getMetadata(), call_user_func([$reflection->name, 'getMetadata'])));
     $output->writeln(var_export($result, true), OutputInterface::VERBOSITY_VERBOSE);
     return $result;
 }
Ejemplo n.º 5
0
 public function highlight($source, Language $language, FormatterInterface $formatter = null)
 {
     $formatter = $formatter ?: $this->getDefaultFormatter();
     return $formatter->format($language->parse($source));
 }
Ejemplo n.º 6
0
 public function testByFilename()
 {
     KeyLighter::get()->register(MockGreedyLanguage::class, ['extension' => ['*.mock']]);
     $this->assertInstanceOf(MockGreedyLanguage::class, Language::byFilename('file.mock'));
 }