ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  function init()
23  {
24  $this->setType("src");
25  }
26 
31  static function getLangVars()
32  {
33  return array("ed_insert_code", "pc_code");
34  }
35 
42  function modifyPageContentPostXsl($a_output, $outputmode = "presentation")
43  {
44  $dom = $this->getPage()->getDom();
45 
46  $xpc = xpath_new_context($dom);
47  $path = "//Paragraph"; //"[@Characteristic = 'Code']";
48  $res = & xpath_eval($xpc, $path);
49  for($i = 0; $i < count($res->nodeset); $i++)
50  {
51  $context_node = $res->nodeset[$i];
52  $char = $context_node->get_attribute('Characteristic');
53 
54  if ($char != "Code")
55  continue;
56 
57  $n = $context_node->parent_node();
58  $char = $context_node->get_attribute('Characteristic');
59  $subchar = $context_node->get_attribute('SubCharacteristic');
60  $showlinenumbers = $context_node->get_attribute('ShowLineNumbers');
61  $downloadtitle = $context_node->get_attribute('DownloadTitle');
62  $autoindent = $context_node->get_attribute('AutoIndent');
63 
64  $content = "";
65 
66  // get XML Content
67  $childs = $context_node->child_nodes();
68 
69  for($j=0; $j<count($childs); $j++)
70  {
71  $content .= $dom->dump_node($childs[$j]);
72  }
73 
74  while ($context_node->has_child_nodes ())
75  {
76  $node_del = $context_node->first_child ();
77  $context_node->remove_child ($node_del);
78  }
79 
80  $content = str_replace("<br />", "<br/>", utf8_decode($content) );
81  $content = str_replace("<br/>", "\n", $content);
82  $rownums = count(split ("\n",$content));
83 
84  $plain_content = html_entity_decode($content);
85  $plain_content = preg_replace ("/\&#x([1-9a-f]{2});?/ise","chr (base_convert (\\1, 16, 10))",$plain_content);
86  $plain_content = preg_replace ("/\&#(\d+);?/ise","chr (\\1)",$plain_content);
87  $content = utf8_encode($this->highlightText($plain_content, $subchar, $autoindent));
88 
89  $content = str_replace("&amp;lt;", "&lt;", $content);
90  $content = str_replace("&amp;gt;", "&gt;", $content);
91 // $content = str_replace("&", "&amp;", $content);
92 //var_dump($content);
93  $rows = "<tr valign=\"top\">";
94  $rownumbers = "";
95  $linenumbers= "";
96 
97  //if we have to show line numbers
98  if (strcmp($showlinenumbers,"y")==0)
99  {
100  $linenumbers = "<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
101  $linenumbers .= "<pre class=\"ilc_Code\">";
102 
103  for ($j=0; $j < $rownums; $j++)
104  {
105  $indentno = strlen($rownums) - strlen($j+1) + 2;
106  $rownumeration = ($j+1);
107  $linenumbers .= "<span class=\"ilc_LineNumber\">$rownumeration</span>";
108  if ($j < $rownums-1)
109  {
110  $linenumbers .= "\n";
111  }
112  }
113  $linenumbers .= "</pre>";
114  $linenumbers .= "</td>";
115  }
116 
117  $rows .= $linenumbers."<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code\">".$content."</pre></td>";
118  $rows .= "</tr>";
119 
120  // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
121  // workaround: add a space after each br.
122  $newcontent = str_replace("\n", "<br/>",$rows);
123  // fix for IE
124  $newcontent = str_replace("<br/><br/>", "<br/> <br/>",$newcontent);
125  // falls drei hintereinander...
126  $newcontent = str_replace("<br/><br/>", "<br/> <br/>",$newcontent);
127 
128  // workaround for preventing template engine
129  // from hiding paragraph text that is enclosed
130  // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
131  $newcontent = str_replace("{", "&#123;", $newcontent);
132  $newcontent = str_replace("}", "&#125;", $newcontent);
133 
134 //echo htmlentities($newcontent);
135  $a_output = str_replace("[[[[[Code;".($i + 1)."]]]]]", $newcontent, $a_output);
136 
137  if ($outputmode != "presentation" && is_object($this->getPage()->getOfflineHandler())
138  && trim($downloadtitle) != "")
139  {
140  // call code handler for offline versions
141  $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
142  }
143  }
144 
145  return $a_output;
146  }
147 
151  function highlightText($a_text, $proglang, $autoindent)
152  {
153 
154  if (!$this->hasHighlighter($proglang))
155  {
156  $proglang="plain";
157  }
158 
159  require_once("./Services/COPage/syntax_highlight/php/HFile/HFile_".$proglang.".php");
160  $classname = "HFile_$proglang";
161  $h_instance = new $classname();
162  if ($autoindent == "n") {
163  $h_instance ->notrim = 1;
164  $h_instance ->indent = array ("");
165  $h_instance ->unindent = array ("");
166  }
167 
168  $highlighter = new Core($h_instance, new Output_css());
169  $a_text = $highlighter->highlight_text(html_entity_decode($a_text));
170 
171  return $a_text;
172  }
173 
174  function hasHighlighter ($hfile_ext)
175  {
176  return file_exists ("Services/COPage/syntax_highlight/php/HFile/HFile_".$hfile_ext.".php");
177  }
178 
179 }
180 ?>