ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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_callback(
86  "/\&#x([1-9a-f]{2});?/is",
87  function($hit) {
88  return chr(base_convert($hit[1], 16, 10));
89  },
90  $plain_content
91  );
92  $plain_content = preg_replace_callback(
93  "/\&#(\d+);?/is",
94  function($hit) {
95  return chr($hit[1]);
96  },
97  $plain_content
98  );
99  $content = utf8_encode($this->highlightText($plain_content, $subchar, $autoindent));
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  {
112  $linenumbers = "<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
113  $linenumbers .= "<pre class=\"ilc_Code\">";
114 
115  for ($j=0; $j < $rownums; $j++)
116  {
117  $indentno = strlen($rownums) - strlen($j+1) + 2;
118  $rownumeration = ($j+1);
119  $linenumbers .= "<span class=\"ilc_LineNumber\">$rownumeration</span>";
120  if ($j < $rownums-1)
121  {
122  $linenumbers .= "\n";
123  }
124  }
125  $linenumbers .= "</pre>";
126  $linenumbers .= "</td>";
127  }
128 
129  $rows .= $linenumbers."<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code\">".$content."</pre></td>";
130  $rows .= "</tr>";
131 
132  // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
133  // workaround: add a space after each br.
134  $newcontent = str_replace("\n", "<br/>",$rows);
135  // fix for IE
136  $newcontent = str_replace("<br/><br/>", "<br/> <br/>",$newcontent);
137  // falls drei hintereinander...
138  $newcontent = str_replace("<br/><br/>", "<br/> <br/>",$newcontent);
139 
140  // workaround for preventing template engine
141  // from hiding paragraph text that is enclosed
142  // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
143  $newcontent = str_replace("{", "&#123;", $newcontent);
144  $newcontent = str_replace("}", "&#125;", $newcontent);
145 
146 //echo htmlentities($newcontent);
147  $a_output = str_replace("[[[[[Code;".($i + 1)."]]]]]", $newcontent, $a_output);
148 
149  if ($outputmode != "presentation" && is_object($this->getPage()->getOfflineHandler())
150  && trim($downloadtitle) != "")
151  {
152  // call code handler for offline versions
153  $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
154  }
155  }
156 
157  return $a_output;
158  }
159 
163  function highlightText($a_text, $proglang, $autoindent)
164  {
165 
166  if (!$this->hasHighlighter($proglang))
167  {
168  $proglang="plain";
169  }
170 
171  require_once("./Services/COPage/syntax_highlight/php/HFile/HFile_".$proglang.".php");
172  $classname = "HFile_$proglang";
173  $h_instance = new $classname();
174  if ($autoindent == "n") {
175  $h_instance ->notrim = 1;
176  $h_instance ->indent = array ("");
177  $h_instance ->unindent = array ("");
178  }
179 
180  $highlighter = new Core($h_instance, new Output_css());
181  $a_text = $highlighter->highlight_text(html_entity_decode($a_text));
182 
183  return $a_text;
184  }
185 
186  function hasHighlighter ($hfile_ext)
187  {
188  return file_exists ("Services/COPage/syntax_highlight/php/HFile/HFile_".$hfile_ext.".php");
189  }
190 
191 }
192 ?>
modifyPageContentPostXsl($a_output, $outputmode="presentation")
Modify page content after xsl.
xpath_new_context($dom_document)
static getLangVars()
Get lang vars needed for editing.
Definition: Core.php:26
xpath_eval($xpath_context, $eval_str, $contextnode=null)
highlightText($a_text, $proglang, $autoindent)
Highligths Text with given ProgLang.
Class ilPCParagraph.
setType($a_type)
Set Type.
Class ilPCSourceCode.
hasHighlighter($hfile_ext)
$n
Definition: RandomTest.php:80
init()
Init page content component.
$path
Definition: index.php:22