ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  $content = $this->highlightText($plain_content, $subchar);
97 
98  $content = str_replace("&amp;lt;", "&lt;", $content);
99  $content = str_replace("&amp;gt;", "&gt;", $content);
100  // $content = str_replace("&", "&amp;", $content);
101  //var_dump($content);
102  $rows = "<tr valign=\"top\">";
103  $rownumbers = "";
104  $linenumbers = "";
105 
106  //if we have to show line numbers
107  if (strcmp($showlinenumbers, "y") == 0) {
108  $linenumbers = "<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
109  $linenumbers .= "<pre class=\"ilc_Code ilc_code_block_Code\">";
110 
111  for ($j = 0; $j < $rownums; $j++) {
112  $indentno = strlen($rownums) - strlen($j + 1) + 2;
113  $rownumeration = ($j + 1);
114  $linenumbers .= "<span class=\"ilc_LineNumber\">$rownumeration</span>";
115  if ($j < $rownums - 1) {
116  $linenumbers .= "\n";
117  }
118  }
119  $linenumbers .= "</pre>";
120  $linenumbers .= "</td>";
121  }
122 
123  $rows .= $linenumbers . "<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code ilc_code_block_Code\">" . $content . "</pre></td>";
124  $rows .= "</tr>";
125 
126  // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
127  // workaround: add a space after each br.
128  $newcontent = str_replace("\n", "<br/>", $rows);
129  // fix for IE
130  $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
131  // falls drei hintereinander...
132  $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
133 
134  // workaround for preventing template engine
135  // from hiding paragraph text that is enclosed
136  // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
137  $newcontent = str_replace("{", "&#123;", $newcontent);
138  $newcontent = str_replace("}", "&#125;", $newcontent);
139 
140  $a_output = str_replace("[[[[[Code;" . ($i + 1) . "]]]]]", $newcontent, $a_output);
141 
142  if ($a_mode != "presentation" && is_object($this->getPage()->getOfflineHandler())
143  && trim($downloadtitle) != "") {
144  // call code handler for offline versions
145  $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
146  }
147  $i++;
148  }
149 
150  return $a_output;
151  }
152 
156  public function highlightText(
157  string $a_text,
158  string $proglang
159  ): string {
160  $proglang = ilSyntaxHighlighter::getNewLanguageId($proglang);
161  if (ilSyntaxHighlighter::isSupported($proglang)) {
162  $highl = ilSyntaxHighlighter::getInstance($proglang);
163  $a_text = $highl->highlight($a_text);
164  }
165  return $a_text;
166  }
167 
168  public function importFile(string $tmpname): void
169  {
170  if ($tmpname !== "") {
171  $tmpfs = $this->domain->filesystem()->temp();
172  $this->setText(
173  $this->input2xml($tmpfs->read($tmpname), 0, false)
174  );
175  $tmpfs->delete($tmpname);
176  }
177  }
178 }
setType(string $a_type)
Set Type.
static getInstance(string $a_lang)
static getNewLanguageId(string $a_old_lang_id)
Get new language id (for an old one)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
modifyPageContentPostXsl(string $a_output, string $a_mode="presentation", bool $a_abstract_only=false)
importFile(string $tmpname)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setText(string $a_text, bool $a_auto_split=false)
Set (xml) content of text paragraph.
input2xml(string $a_text, bool $a_wysiwyg=false, bool $a_handle_lists=true)
static isSupported(string $a_lang)
Is language supported?
highlightText(string $a_text, string $proglang)
Highlights Text with given ProgLang.