vendor/contao/core-bundle/src/Resources/contao/elements/ContentCode.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Highlight\Highlighter;
  11. /**
  12.  * Front end content element "code".
  13.  */
  14. class ContentCode extends ContentElement
  15. {
  16.     /**
  17.      * Template
  18.      * @var string
  19.      */
  20.     protected $strTemplate 'ce_code';
  21.     /**
  22.      * Show the raw code in the back end
  23.      *
  24.      * @return string
  25.      */
  26.     public function generate()
  27.     {
  28.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  29.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  30.         {
  31.             $return '<pre>' htmlspecialchars($this->code) . '</pre>';
  32.             if ($this->headline)
  33.             {
  34.                 $return '<' $this->hl '>' $this->headline '</' $this->hl '>' $return;
  35.             }
  36.             return $return;
  37.         }
  38.         return parent::generate();
  39.     }
  40.     /**
  41.      * Generate the content element
  42.      */
  43.     protected function compile()
  44.     {
  45.         if ($this->highlight == 'C#')
  46.         {
  47.             $this->highlight 'csharp';
  48.         }
  49.         elseif ($this->highlight == 'C++')
  50.         {
  51.             $this->highlight 'cpp';
  52.         }
  53.         $hl = new Highlighter();
  54.         try
  55.         {
  56.             $this->Template->code $hl->highlight(strtolower($this->highlight) ?: 'plaintext'$this->code)->value;
  57.         }
  58.         catch (\DomainException $e)
  59.         {
  60.             $this->Template->code htmlspecialchars($this->code);
  61.         }
  62.         $this->Template->cssClass 'hljs ' . (strtolower($this->highlight) ?: 'nohighlight');
  63.     }
  64. }
  65. class_alias(ContentCode::class, 'ContentCode');