ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPCSourceCode.php
Go to the documentation of this file.
1<?php
2
28{
29 public function init(): void
30 {
31 $this->setType("src");
32 }
33
34 public static function getLangVars(): array
35 {
36 return array("ed_insert_code", "pc_code");
37 }
38
39 public function modifyPageContentPostXsl(
40 string $a_output,
41 string $a_mode = "presentation",
42 bool $a_abstract_only = false
43 ): string {
44 $nodes = $this->dom_util->path($this->dom_doc, "//Paragraph");
45 $i = 0;
46 foreach ($nodes as $context_node) {
47 $char = $context_node->getAttribute('Characteristic');
48
49 if ($char !== "Code") {
50 $i++;
51 continue;
52 }
53
54 $n = $context_node->parentNode;
55 $char = $context_node->getAttribute('Characteristic');
56 $subchar = $context_node->getAttribute('SubCharacteristic');
57 $showlinenumbers = $context_node->getAttribute('ShowLineNumbers');
58 $downloadtitle = $context_node->getAttribute('DownloadTitle');
59 $autoindent = $context_node->getAttribute('AutoIndent');
60
61 // get XML Content
62 $content = "";
63 foreach ($context_node->childNodes as $child) {
64 $content .= $this->dom_util->dump($child);
65 }
66
67 while ($context_node->firstChild) {
68 $node_del = $context_node->firstChild;
69 $node_del->parentNode->removeChild($node_del);
70 }
71
72 //$content = str_replace("<br />", "<br/>", utf8_decode($content));
73 $content = str_replace("<br />", "<br/>", $content);
74 $content = str_replace("<br/>", "\n", $content);
75 $rownums = count(explode("\n", $content));
76
77 // see #23028
78 //$plain_content = html_entity_decode($content);
79 $plain_content = $content;
80
81 $plain_content = preg_replace_callback(
82 "/\&#x([1-9a-f]{2});?/is",
83 function ($hit) {
84 return chr(base_convert($hit[1], 16, 10));
85 },
86 $plain_content
87 );
88 $plain_content = preg_replace_callback(
89 "/\&#(\d+);?/is",
90 function ($hit) {
91 return chr($hit[1]);
92 },
93 $plain_content
94 );
95 //$content = utf8_encode($this->highlightText($plain_content, $subchar));
96 if ($subchar === "") {
97 $subchar = "other";
98 }
99 $content = $this->highlightText($plain_content, $subchar);
100
101 $content = str_replace("&amp;lt;", "&lt;", $content);
102 $content = str_replace("&amp;gt;", "&gt;", $content);
103 // $content = str_replace("&", "&amp;", $content);
104 //var_dump($content);
105 $rows = "<tr valign=\"top\">";
106 $rownumbers = "";
107 $linenumbers = "";
108
109 //if we have to show line numbers
110 if (strcmp($showlinenumbers, "y") == 0) {
111 $linenumbers = "<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
112 $linenumbers .= "<pre class=\"ilc_Code ilc_code_block_Code\">";
113
114 for ($j = 0; $j < $rownums; $j++) {
115 $indentno = strlen($rownums) - strlen($j + 1) + 2;
116 $rownumeration = ($j + 1);
117 $linenumbers .= "<span class=\"ilc_LineNumber\">$rownumeration</span>";
118 if ($j < $rownums - 1) {
119 $linenumbers .= "\n";
120 }
121 }
122 $linenumbers .= "</pre>";
123 $linenumbers .= "</td>";
124 }
125
126 $rows .= $linenumbers . "<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code ilc_code_block_Code\">" . $content . "</pre></td>";
127 $rows .= "</tr>";
128
129 // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
130 // workaround: add a space after each br.
131 $newcontent = str_replace("\n", "<br/>", $rows);
132 // fix for IE
133 $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
134 // falls drei hintereinander...
135 $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
136
137 // workaround for preventing template engine
138 // from hiding paragraph text that is enclosed
139 // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
140 $newcontent = str_replace("{", "&#123;", $newcontent);
141 $newcontent = str_replace("}", "&#125;", $newcontent);
142
143 $a_output = str_replace("[[[[[Code;" . ($i + 1) . "]]]]]", $newcontent, $a_output);
144
145 if ($a_mode != "presentation" && is_object($this->getPage()->getOfflineHandler())
146 && trim($downloadtitle) != "") {
147 // call code handler for offline versions
148 $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
149 }
150 $i++;
151 }
152
153 return $a_output;
154 }
155
159 public function highlightText(
160 string $a_text,
161 string $proglang
162 ): string {
163 $map = ["php3" => "php",
164 "java122" => "java",
165 "html" => "html4strict"];
166 if (isset($map[$proglang])) {
167 $proglang = $map[$proglang];
168 }
169
170 $geshi = new GeSHi(html_entity_decode($a_text), $proglang);
171 $a_code = $geshi->parse_code();
172 $a_code = substr($a_code, strpos($a_code, ">") + 1);
173 $a_code = substr($a_code, 0, strrpos($a_code, "<"));
174 return $a_code;
175 }
176
177 public function importFile(string $tmpname): void
178 {
179 if ($tmpname !== "") {
180 $tmpfs = $this->domain->filesystem()->temp();
181 $this->setText(
182 $this->input2xml($tmpfs->read($tmpname), 0, false)
183 );
184 $tmpfs->delete($tmpname);
185 }
186 }
187}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
static getLangVars()
Get lang vars needed for editing.
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.