20use Phiki\Grammar\Grammar;
25 public const string JAVA =
"java";
26 public const string PHP =
"php";
27 public const string C =
"c";
28 public const string CPP =
"cpp";
29 public const string HTML =
"html4strict";
30 public const string XML =
"xml";
32 public const string LATEX =
"latex";
33 public const string DELPHI =
"delphi";
34 public const string PYTHON =
"python";
35 public const string CSS =
"css";
37 public const string SQL =
"sql";
38 public const string BASH =
"bash";
44 protected static array
$langs = array(
49 self::DELPHI =>
"Delphi",
52 self::JAVASCRIPT =>
"Javascript",
53 self::LATEX =>
"LaTeX",
55 self::POWERSHELL =>
"Powershell",
56 self::PYTHON =>
"Python",
58 self::VISUAL_BASIC =>
"Visual Basic",
65 "html" =>
"html4strict"
71 $map = array_flip(self::$v51_map);
72 foreach (self::$langs as $k => $v) {
73 if (isset($map[$k])) {
81 public function init(): void
88 return array(
"ed_insert_code",
"pc_code");
93 string $a_mode =
"presentation",
94 bool $a_abstract_only =
false
96 $nodes = $this->dom_util->path($this->dom_doc,
"//Paragraph");
98 foreach ($nodes as $context_node) {
99 $char = $context_node->getAttribute(
'Characteristic');
101 if ($char !==
"Code") {
106 $n = $context_node->parentNode;
107 $char = $context_node->getAttribute(
'Characteristic');
108 $subchar = $context_node->getAttribute(
'SubCharacteristic');
109 $showlinenumbers = $context_node->getAttribute(
'ShowLineNumbers');
110 $downloadtitle = $context_node->getAttribute(
'DownloadTitle');
111 $autoindent = $context_node->getAttribute(
'AutoIndent');
115 foreach ($context_node->childNodes as $child) {
116 $content .= $this->dom_util->dump($child);
119 while ($context_node->firstChild) {
120 $node_del = $context_node->firstChild;
121 $node_del->parentNode->removeChild($node_del);
125 $content = str_replace(
"<br />",
"<br/>", $content);
126 $content = str_replace(
"<br/>",
"\n", $content);
127 $rownums = count(explode(
"\n", $content));
131 $plain_content = $content;
133 $plain_content = preg_replace_callback(
134 "/\&#x([1-9a-f]{2});?/is",
136 return chr(base_convert($hit[1], 16, 10));
140 $plain_content = preg_replace_callback(
148 if ($subchar ===
"") {
153 $content = str_replace(
"&lt;",
"<", $content);
154 $content = str_replace(
"&gt;",
">", $content);
157 $rows =
"<tr valign=\"top\">";
162 if (strcmp($showlinenumbers,
"y") == 0) {
163 $linenumbers =
"<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
164 $linenumbers .=
"<pre class=\"ilc_Code ilc_code_block_Code\"><code>";
166 for ($j = 0; $j < $rownums; $j++) {
167 $indentno = strlen($rownums) - strlen($j + 1) + 2;
168 $rownumeration = ($j + 1);
169 $linenumbers .=
"<span class=\"ilc_LineNumber\">$rownumeration</span>";
170 if ($j < $rownums - 1) {
171 $linenumbers .=
"\n";
174 $linenumbers .=
"</code></pre>";
175 $linenumbers .=
"</td>";
178 $rows .= $linenumbers .
"<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code ilc_code_block_Code\">" . $content .
"</pre></td>";
183 $newcontent = str_replace(
"\n",
"<br/>", $rows);
185 $newcontent = str_replace(
"<br/><br/>",
"<br/> <br/>", $newcontent);
187 $newcontent = str_replace(
"<br/><br/>",
"<br/> <br/>", $newcontent);
192 $newcontent = str_replace(
"{",
"{", $newcontent);
193 $newcontent = str_replace(
"}",
"}", $newcontent);
195 $a_output = str_replace(
"[[[[[Code;" . ($i + 1) .
"]]]]]", $newcontent, $a_output);
197 if ($a_mode !=
"presentation" && is_object($this->
getPage()->getOfflineHandler())
198 && trim($downloadtitle) !=
"") {
200 $this->
getPage()->getOfflineHandler()->handleCodeParagraph($this->
getPage()->
getId(), $i + 1, $downloadtitle, $plain_content);
208 protected function phikiMap(
string $lang): ?Grammar
210 $grammar = match ($lang) {
211 "java" => Grammar::Java,
212 "php" => Grammar::Php,
214 "cpp" => Grammar::Cpp,
215 "html4strict" => Grammar::Html,
216 "xml" => Grammar::Xml,
218 "latex" => Grammar::Latex,
219 "delphi" => Grammar::Pascal,
220 "python" => Grammar::Python,
221 "css" => Grammar::Css,
222 "javascript" => Grammar::Javascript,
223 "sql" => Grammar::Sql,
224 "bash" => Grammar::Shellscript,
225 "powershell" => Grammar::Powershell,
226 default => Grammar::Markdown
239 $map = [
"php3" =>
"php",
241 "html" =>
"html4strict"];
242 if (isset($map[$proglang])) {
243 $proglang = $map[$proglang];
246 $phiki =
new Phiki();
247 $grammar = $this->phikiMap($proglang);
248 if (!is_null($grammar)) {
249 $a_code = $phiki->codeToHtml(html_entity_decode($a_text), $grammar, Theme::GithubLight);
254 $a_code = substr($a_code, strpos($a_code,
">") + 1);
255 $a_code = substr($a_code, 0, strrpos($a_code,
"<"));
261 if ($tmpname !==
"") {
262 $tmpfs = $this->domain->filesystem()->temp();
264 $this->input2xml($tmpfs->read($tmpname), 0,
false)
266 $tmpfs->delete($tmpname);
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
highlightText(string $a_text, string $proglang)
Highlights Text with given ProgLang.
const string VISUAL_BASIC
static getLangVars()
Get lang vars needed for editing.
static getSupportedLanguages()
importFile(string $tmpname)
modifyPageContentPostXsl(string $a_output, string $a_mode="presentation", bool $a_abstract_only=false)
Modify page content after xsl.
setType(string $a_type)
Set Type.