public function highlight($source, $lexer)
 {
     $hash = sha1($source);
     if (!file_exists($this->kernel->getCacheDir() . '/highlight')) {
         mkdir($this->kernel->getCacheDir() . '/highlight');
     }
     $cache = $this->kernel->getCacheDir() . '/highlight/' . $hash;
     if (file_exists($cache)) {
         $highlight = file_get_contents($cache);
     } else {
         $highlight = Pygmentize::format($source, $lexer);
         file_put_contents($cache, $highlight);
     }
     return '<div class="highlight"><pre><code class="' . $lexer . '">' . $highlight . '</code></pre></div>';
 }
 public function indexAction()
 {
     $lexers = Pygmentize::lexers();
     $lexers['objcpp'] = $lexers['objc++'];
     unset($lexers['objc++']);
     unset($lexers['obj-c++']);
     $lexers['objectivecpp'] = $lexers['objectivec++'];
     unset($lexers['objective-c++']);
     foreach ($lexers as $k => $v) {
         unset($lexers[$k]);
         $k = str_replace('-', '', $k);
         $k = str_replace('+', '', $k);
         $k = str_replace(' ', '', $k);
         $lexers[$k] = $v;
     }
     unset($lexers['antlrc#']);
     unset($lexers['antlr-c#']);
     unset($lexers['c++']);
     unset($lexers['c++-objdumb']);
     unset($lexers['c#']);
     ksort($lexers);
     return $this->render('HighlightBundle:docs:index.html.twig', array('lexers' => $lexers));
 }
 /**
  * {@inheritdoc}
  *
  * @throws \InvalidArgumentException When the target directory does not exist or symlink cannot be used
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $targetArg = rtrim($input->getArgument('target'), '/');
     if (!is_dir($targetArg)) {
         throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target')));
     }
     /** @var Filesystem $filesystem */
     $filesystem = $this->getContainer()->get('filesystem');
     // Create the bundles directory otherwise symlink will fail.
     $bundlesDir = $targetArg . '/bundles/';
     $filesystem->mkdir($bundlesDir, 0777);
     $output->writeln('Installing Highlight assets.');
     $targetDir = $bundlesDir . 'highlight-extra';
     $namespaceParts = explode('\\', __NAMESPACE__);
     array_pop($namespaceParts);
     $output->writeln(sprintf('Installing assets for <comment>%s</comment> into <comment>%s</comment>', implode('\\', $namespaceParts), $targetDir));
     $filesystem->remove($targetDir);
     $filesystem->mkdir($targetDir, 0777);
     $filesystem->mkdir($targetDir . '/css', 0777);
     foreach (Pygmentize::styles() as $style => $styleName) {
         file_put_contents($targetDir . '/css/' . $style . '.css', Pygmentize::getStyle($style));
     }
 }
 /**
  * Parses a token and returns a node.
  *
  * @param \Twig_Token $token A Twig_Token instance
  * @throws \Exception
  * @throws \Twig_Error_Syntax
  * @return \Twig_NodeInterface A Twig_NodeInterface instance
  */
 public function parse(\Twig_Token $token)
 {
     $lineNo = $token->getLine();
     $stream = $this->parser->getStream();
     $pygmentizeLexers = array_keys(Pygmentize::lexers());
     $acceptedLexers = array();
     foreach ($pygmentizeLexers as $lexer) {
         $k = str_replace('+', '', $lexer);
         $k = str_replace('-', '', $k);
         $k = str_replace(' ', '', $k);
         $acceptedLexers[$k] = $lexer;
     }
     $additionalLexers = array('twig' => 'jinja', 'twightml' => 'html+jinja', 'htmltwig' => 'html+jinja', 'composer' => 'json', 'objcpp' => 'obj-c++', 'objectivecpp' => 'objective-c++');
     $lexer = null;
     if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::BLOCK_END_TYPE)) {
             throw new \Twig_Error_Syntax('A lexer is needed for highlighting. ', $stream->getCurrent()->getLine(), $stream->getFilename());
         }
         $lexer = $this->parser->getExpressionParser()->parseExpression();
         if (array_key_exists($lexer->getAttribute('name'), $acceptedLexers)) {
             $lexer->setAttribute('name', $acceptedLexers[$lexer->getAttribute('name')]);
         } else {
             if (array_key_exists($lexer->getAttribute('name'), $additionalLexers)) {
                 $lexer->setAttribute('name', $additionalLexers[$lexer->getAttribute('name')]);
             } else {
                 throw new \Twig_Error_Syntax('Unexpected lexer: ' . $lexer->getAttribute('name'), $stream->getCurrent()->getLine(), $stream->getFilename());
             }
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $source = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
     if (!$source instanceof \Twig_Node && !$source instanceof \Twig_Node_Text && !$source instanceof \Twig_Node_Expression) {
         throw new \Twig_Error_Syntax('A message inside a highlight tag must be a simple text.', $source->getLine(), $stream->getFilename());
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     return new HighlightNode($source, $lexer, $lineNo, $this->getTag());
 }