ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilPCSourceCode.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/COPage/classes/class.ilPCParagraph.php");
5 
18 {
22  public function init()
23  {
24  $this->setType("src");
25  }
26 
31  public static function getLangVars()
32  {
33  return array("ed_insert_code", "pc_code");
34  }
35 
39  public function modifyPageContentPostXsl($a_output, $outputmode = "presentation", $a_abstract_only = false)
40  {
41  $dom = $this->getPage()->getDom();
42 
43  $xpc = xpath_new_context($dom);
44  $path = "//Paragraph"; //"[@Characteristic = 'Code']";
45  $res = &xpath_eval($xpc, $path);
46  for ($i = 0; $i < count($res->nodeset); $i++) {
47  $context_node = $res->nodeset[$i];
48  $char = $context_node->get_attribute('Characteristic');
49 
50  if ($char != "Code") {
51  continue;
52  }
53 
54  $n = $context_node->parent_node();
55  $char = $context_node->get_attribute('Characteristic');
56  $subchar = $context_node->get_attribute('SubCharacteristic');
57  $showlinenumbers = $context_node->get_attribute('ShowLineNumbers');
58  $downloadtitle = $context_node->get_attribute('DownloadTitle');
59  $autoindent = $context_node->get_attribute('AutoIndent');
60 
61  $content = "";
62 
63  // get XML Content
64  $childs = $context_node->child_nodes();
65 
66  for ($j = 0; $j < count($childs); $j++) {
67  $content .= $dom->dump_node($childs[$j]);
68  }
69 
70  while ($context_node->has_child_nodes()) {
71  $node_del = $context_node->first_child();
72  $context_node->remove_child($node_del);
73  }
74 
75  $content = str_replace("<br />", "<br/>", utf8_decode($content));
76  $content = str_replace("<br/>", "\n", $content);
77  $rownums = count(explode("\n", $content));
78 
79  // see #23028
80  //$plain_content = html_entity_decode($content);
81  $plain_content = $content;
82 
83  $plain_content = preg_replace_callback(
84  "/\&#x([1-9a-f]{2});?/is",
85  function ($hit) {
86  return chr(base_convert($hit[1], 16, 10));
87  },
88  $plain_content
89  );
90  $plain_content = preg_replace_callback(
91  "/\&#(\d+);?/is",
92  function ($hit) {
93  return chr($hit[1]);
94  },
95  $plain_content
96  );
97  $content = utf8_encode($this->highlightText($plain_content, $subchar, $autoindent));
98 
99  $content = str_replace("&amp;lt;", "&lt;", $content);
100  $content = str_replace("&amp;gt;", "&gt;", $content);
101  // $content = str_replace("&", "&amp;", $content);
102  //var_dump($content);
103  $rows = "<tr valign=\"top\">";
104  $rownumbers = "";
105  $linenumbers = "";
106 
107  //if we have to show line numbers
108  if (strcmp($showlinenumbers, "y") == 0) {
109  $linenumbers = "<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
110  $linenumbers .= "<pre class=\"ilc_Code ilc_code_block_Code\">";
111 
112  for ($j = 0; $j < $rownums; $j++) {
113  $indentno = strlen($rownums) - strlen($j + 1) + 2;
114  $rownumeration = ($j + 1);
115  $linenumbers .= "<span class=\"ilc_LineNumber\">$rownumeration</span>";
116  if ($j < $rownums - 1) {
117  $linenumbers .= "\n";
118  }
119  }
120  $linenumbers .= "</pre>";
121  $linenumbers .= "</td>";
122  }
123 
124  $rows .= $linenumbers . "<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code ilc_code_block_Code\">" . $content . "</pre></td>";
125  $rows .= "</tr>";
126 
127  // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
128  // workaround: add a space after each br.
129  $newcontent = str_replace("\n", "<br/>", $rows);
130  // fix for IE
131  $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
132  // falls drei hintereinander...
133  $newcontent = str_replace("<br/><br/>", "<br/> <br/>", $newcontent);
134 
135  // workaround for preventing template engine
136  // from hiding paragraph text that is enclosed
137  // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
138  $newcontent = str_replace("{", "&#123;", $newcontent);
139  $newcontent = str_replace("}", "&#125;", $newcontent);
140 
141  //echo htmlentities($newcontent);
142  $a_output = str_replace("[[[[[Code;" . ($i + 1) . "]]]]]", $newcontent, $a_output);
143 
144  if ($outputmode != "presentation" && is_object($this->getPage()->getOfflineHandler())
145  && trim($downloadtitle) != "") {
146  // call code handler for offline versions
147  $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
148  }
149  }
150 
151  return $a_output;
152  }
153 
157  public function highlightText($a_text, $proglang, $autoindent = "")
158  {
159  include_once("./Services/UIComponent/SyntaxHighlighter/classes/class.ilSyntaxHighlighter.php");
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 }
static getNewLanguageId($a_old_lang_id)
Get new language id (for an old one)
xpath_new_context($dom_document)
static getLangVars()
Get lang vars needed for editing.
xpath_eval($xpath_context, $eval_str, $contextnode=null)
Class ilPCParagraph.
setType($a_type)
Set Type.
foreach($_POST as $key=> $value) $res
Class ilPCSourceCode.
$n
Definition: RandomTest.php:85
modifyPageContentPostXsl($a_output, $outputmode="presentation", $a_abstract_only=false)
$rows
Definition: xhr_table.php:10
static isSupported($a_lang)
Is language supported?
static getInstance($a_lang)
Get instance.
init()
Init page content component.
highlightText($a_text, $proglang, $autoindent="")
Highligths Text with given ProgLang.
$i
Definition: metadata.php:24