ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilPCSourceCode.php
Go to the documentation of this file.
1<?php
2
19use Phiki\Phiki;
20use Phiki\Grammar\Grammar;
21use Phiki\Theme\Theme;
22
24{
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";
31 public const string VISUAL_BASIC = "vb";
32 public const string LATEX = "latex";
33 public const string DELPHI = "delphi";
34 public const string PYTHON = "python";
35 public const string CSS = "css";
36 public const string JAVASCRIPT = "javascript";
37 public const string SQL = "sql";
38 public const string BASH = "bash";
39 public const string POWERSHELL = "powershell";
40
44 protected static array $langs = array(
45 self::BASH => "Bash",
46 self::C => "C",
47 self::CPP => "C++",
48 self::CSS => "CSS",
49 self::DELPHI => "Delphi",
50 self::HTML => "HTML",
51 self::JAVA => "Java",
52 self::JAVASCRIPT => "Javascript",
53 self::LATEX => "LaTeX",
54 self::PHP => "PHP",
55 self::POWERSHELL => "Powershell",
56 self::PYTHON => "Python",
57 self::SQL => "SQL",
58 self::VISUAL_BASIC => "Visual Basic",
59 self::XML => "XML"
60 );
61
62 protected static array $v51_map = array(
63 "php3" => "php",
64 "java122" => "java",
65 "html" => "html4strict"
66 );
67
68 public static function getSupportedLanguages(): array
69 {
70 $langs = array();
71 $map = array_flip(self::$v51_map);
72 foreach (self::$langs as $k => $v) {
73 if (isset($map[$k])) {
74 $k = $map[$k];
75 }
76 $langs[$k] = $v;
77 }
78 return $langs;
79 }
80
81 public function init(): void
82 {
83 $this->setType("src");
84 }
85
86 public static function getLangVars(): array
87 {
88 return array("ed_insert_code", "pc_code");
89 }
90
91 public function modifyPageContentPostXsl(
92 string $a_output,
93 string $a_mode = "presentation",
94 bool $a_abstract_only = false
95 ): string {
96 $nodes = $this->dom_util->path($this->dom_doc, "//Paragraph");
97 $i = 0;
98 foreach ($nodes as $context_node) {
99 $char = $context_node->getAttribute('Characteristic');
100
101 if ($char !== "Code") {
102 $i++;
103 continue;
104 }
105
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');
112
113 // get XML Content
114 $content = "";
115 foreach ($context_node->childNodes as $child) {
116 $content .= $this->dom_util->dump($child);
117 }
118
119 while ($context_node->firstChild) {
120 $node_del = $context_node->firstChild;
121 $node_del->parentNode->removeChild($node_del);
122 }
123
124 //$content = str_replace("<br />", "<br/>", utf8_decode($content));
125 $content = str_replace("<br />", "<br/>", $content);
126 $content = str_replace("<br/>", "\n", $content);
127 $rownums = count(explode("\n", $content));
128
129 // see #23028
130 //$plain_content = html_entity_decode($content);
131 $plain_content = $content;
132
133 $plain_content = preg_replace_callback(
134 "/\&#x([1-9a-f]{2});?/is",
135 function ($hit) {
136 return chr(base_convert($hit[1], 16, 10));
137 },
138 $plain_content
139 );
140 $plain_content = preg_replace_callback(
141 "/\&#(\d+);?/is",
142 function ($hit) {
143 return chr($hit[1]);
144 },
145 $plain_content
146 );
147 //$content = utf8_encode($this->highlightText($plain_content, $subchar));
148 if ($subchar === "") {
149 $subchar = "other";
150 }
151 $content = $this->highlightText($plain_content, $subchar);
152
153 $content = str_replace("&amp;lt;", "&lt;", $content);
154 $content = str_replace("&amp;gt;", "&gt;", $content);
155 // $content = str_replace("&", "&amp;", $content);
156 //var_dump($content);
157 $rows = "<tr valign=\"top\">";
158 $rownumbers = "";
159 $linenumbers = "";
160
161 //if we have to show line numbers
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>";
165
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";
172 }
173 }
174 $linenumbers .= "</code></pre>";
175 $linenumbers .= "</td>";
176 }
177
178 $rows .= $linenumbers . "<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code ilc_code_block_Code\">" . $content . "</pre></td>";
179 $rows .= "</tr>";
180
181 // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
182 // workaround: add a space after each br.
183 $newcontent = str_replace("\n", "<br/>", $rows);
184 // fix for IE
185 $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
186 // falls drei hintereinander...
187 $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
188
189 // workaround for preventing template engine
190 // from hiding paragraph text that is enclosed
191 // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
192 $newcontent = str_replace("{", "&#123;", $newcontent);
193 $newcontent = str_replace("}", "&#125;", $newcontent);
194
195 $a_output = str_replace("[[[[[Code;" . ($i + 1) . "]]]]]", $newcontent, $a_output);
196
197 if ($a_mode != "presentation" && is_object($this->getPage()->getOfflineHandler())
198 && trim($downloadtitle) != "") {
199 // call code handler for offline versions
200 $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
201 }
202 $i++;
203 }
204
205 return $a_output;
206 }
207
208 protected function phikiMap(string $lang): ?Grammar
209 {
210 $grammar = match ($lang) {
211 "java" => Grammar::Java,
212 "php" => Grammar::Php,
213 "c" => Grammar::C,
214 "cpp" => Grammar::Cpp,
215 "html4strict" => Grammar::Html,
216 "xml" => Grammar::Xml,
217 "vb" => Grammar::Vb,
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
227 };
228
229 return $grammar;
230 }
231
235 public function highlightText(
236 string $a_text,
237 string $proglang
238 ): string {
239 $map = ["php3" => "php",
240 "java122" => "java",
241 "html" => "html4strict"];
242 if (isset($map[$proglang])) {
243 $proglang = $map[$proglang];
244 }
245
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);
250 } else {
251 $a_code = $a_text;
252 }
253
254 $a_code = substr($a_code, strpos($a_code, ">") + 1);
255 $a_code = substr($a_code, 0, strrpos($a_code, "<"));
256 return $a_code;
257 }
258
259 public function importFile(string $tmpname): void
260 {
261 if ($tmpname !== "") {
262 $tmpfs = $this->domain->filesystem()->temp();
263 $this->setText(
264 $this->input2xml($tmpfs->read($tmpname), 0, false)
265 );
266 $tmpfs->delete($tmpname);
267 }
268 }
269}
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.
phikiMap(string $lang)
const string JAVASCRIPT
static getSupportedLanguages()
const string POWERSHELL
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.