ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilSyntaxHighlighter.php
Go to the documentation of this file.
1 <?php
2 
27 {
28  public const JAVA = "java";
29  public const PHP = "php";
30  public const C = "c";
31  public const CPP = "cpp";
32  public const HTML = "html4strict";
33  public const XML = "xml";
34  public const VISUAL_BASIC = "vb";
35  public const LATEX = "latex";
36  public const DELPHI = "delphi";
37  public const PYTHON = "python";
38  public const CSS = "css";
39  public const JAVASCRIPT = "javascript";
40  public const SQL = "sql";
41  public const BASH = "bash";
42  public const POWERSHELL = "powershell";
43 
47  protected static array $langs = array(
48  self::BASH => "Bash",
49  self::C => "C",
50  self::CPP => "C++",
51  self::CSS => "CSS",
52  self::DELPHI => "Delphi",
53  self::HTML => "HTML",
54  self::JAVA => "Java",
55  self::JAVASCRIPT => "Javascript",
56  self::LATEX => "LaTeX",
57  self::PHP => "PHP",
58  self::POWERSHELL => "Powershell",
59  self::PYTHON => "Python",
60  self::SQL => "SQL",
61  self::VISUAL_BASIC => "Visual Basic",
62  self::XML => "XML"
63  );
64 
68  protected static array $v51_map = array(
69  "php3" => "php",
70  "java122" => "java",
71  "html" => "html4strict"
72  );
73 
74  protected string $lang;
75 
76  protected function __construct(string $a_lang)
77  {
78  $this->lang = $a_lang;
79  }
80 
81  public static function getInstance(string $a_lang): self
82  {
83  return new self($a_lang);
84  }
85 
90  public static function getSupportedLanguages(): array
91  {
92  return self::$langs;
93  }
94 
98  public static function isSupported(string $a_lang): bool
99  {
100  return isset(self::$langs[$a_lang]);
101  }
102 
106  public static function getNewLanguageId(string $a_old_lang_id): string
107  {
108  return self::$v51_map[$a_old_lang_id] ?? $a_old_lang_id;
109  }
110 
111 
115  public static function getSupportedLanguagesV51(): array
116  {
117  $langs = array();
118  $map = array_flip(self::$v51_map);
119  foreach (self::$langs as $k => $v) {
120  if (isset($map[$k])) {
121  $k = $map[$k];
122  }
123  $langs[$k] = $v;
124  }
125  return $langs;
126  }
127 
128  public function highlight(string $a_code): string
129  {
130  include_once("./vendor/composer/vendor/geshi/geshi/src/geshi.php");
131  $geshi = new Geshi(html_entity_decode($a_code), $this->lang);
132 
133  //var_dump($geshi->get_supported_languages()); exit;
134 
135  //$geshi->set_header_type(GESHI_HEADER_NONE); // does not work as expected, see below
136  $a_code = $geshi->parse_code();
137 
138  // remove geshi pre tag (setting GESHI_HEADER_NONE gives us undesired br tags)
139  $a_code = substr($a_code, strpos($a_code, ">") + 1);
140  $a_code = substr($a_code, 0, strrpos($a_code, "<"));
141 
142  return $a_code;
143  }
144 }
static getInstance(string $a_lang)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getNewLanguageId(string $a_old_lang_id)
Get new language id (for an old one)
static getSupportedLanguages()
Get supported languages (keys are internal values, values are for representation) ...
static getSupportedLanguagesV51()
Get supported languages (keys are ILIAS <= 5.1 internal values, values are for representation) ...
static isSupported(string $a_lang)
Is language supported?