ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
4require_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(explode("\n",$content));
83
84 // see #23028
85 //$plain_content = html_entity_decode($content);
86 $plain_content = $content;
87
88 $plain_content = preg_replace_callback(
89 "/\&#x([1-9a-f]{2});?/is",
90 function($hit) {
91 return chr(base_convert($hit[1], 16, 10));
92 },
93 $plain_content
94 );
95 $plain_content = preg_replace_callback(
96 "/\&#(\d+);?/is",
97 function($hit) {
98 return chr($hit[1]);
99 },
100 $plain_content
101 );
102 $content = utf8_encode($this->highlightText($plain_content, $subchar, $autoindent));
103
104 $content = str_replace("&amp;lt;", "&lt;", $content);
105 $content = str_replace("&amp;gt;", "&gt;", $content);
106// $content = str_replace("&", "&amp;", $content);
107//var_dump($content);
108 $rows = "<tr valign=\"top\">";
109 $rownumbers = "";
110 $linenumbers= "";
111
112 //if we have to show line numbers
113 if (strcmp($showlinenumbers,"y")==0)
114 {
115 $linenumbers = "<td nowrap=\"nowrap\" class=\"ilc_LineNumbers\" >";
116 $linenumbers .= "<pre class=\"ilc_Code\">";
117
118 for ($j=0; $j < $rownums; $j++)
119 {
120 $indentno = strlen($rownums) - strlen($j+1) + 2;
121 $rownumeration = ($j+1);
122 $linenumbers .= "<span class=\"ilc_LineNumber\">$rownumeration</span>";
123 if ($j < $rownums-1)
124 {
125 $linenumbers .= "\n";
126 }
127 }
128 $linenumbers .= "</pre>";
129 $linenumbers .= "</td>";
130 }
131
132 $rows .= $linenumbers."<td class=\"ilc_Sourcecode\"><pre class=\"ilc_Code\">".$content."</pre></td>";
133 $rows .= "</tr>";
134
135 // fix for ie explorer which is not able to produce empty line feeds with <br /><br />;
136 // workaround: add a space after each br.
137 $newcontent = str_replace("\n", "<br/>",$rows);
138 // fix for IE
139 $newcontent = str_replace("<br/><br/>", "<br/> <br/>",$newcontent);
140 // falls drei hintereinander...
141 $newcontent = str_replace("<br/><br/>", "<br/> <br/>",$newcontent);
142
143 // workaround for preventing template engine
144 // from hiding paragraph text that is enclosed
145 // in curly brackets (e.g. "{a}", see ilLMEditorGUI::executeCommand())
146 $newcontent = str_replace("{", "&#123;", $newcontent);
147 $newcontent = str_replace("}", "&#125;", $newcontent);
148
149//echo htmlentities($newcontent);
150 $a_output = str_replace("[[[[[Code;".($i + 1)."]]]]]", $newcontent, $a_output);
151
152 if ($outputmode != "presentation" && is_object($this->getPage()->getOfflineHandler())
153 && trim($downloadtitle) != "")
154 {
155 // call code handler for offline versions
156 $this->getPage()->getOfflineHandler()->handleCodeParagraph($this->getPage()->getId(), $i + 1, $downloadtitle, $plain_content);
157 }
158 }
159
160 return $a_output;
161 }
162
166 function highlightText($a_text, $proglang, $autoindent = "")
167 {
168
169 include_once("./Services/UIComponent/SyntaxHighlighter/classes/class.ilSyntaxHighlighter.php");
170 $proglang = ilSyntaxHighlighter::getNewLanguageId($proglang);
172 {
173 $highl = ilSyntaxHighlighter::getInstance($proglang);
174 $a_text = $highl->highlight($a_text);
175 }
176 return $a_text;
177 }
178
179 function hasHighlighter ($hfile_ext)
180 {
181 return file_exists ("Services/COPage/syntax_highlight/php/HFile/HFile_".$hfile_ext.".php");
182 }
183
184}
185?>
$n
Definition: RandomTest.php:80
$path
Definition: aliased.php:25
An exception for terminatinating execution or to throw for unit testing.
Class ilPCParagraph.
Class ilPCSourceCode.
static getLangVars()
Get lang vars needed for editing.
highlightText($a_text, $proglang, $autoindent="")
Highligths Text with given ProgLang.
init()
Init page content component.
modifyPageContentPostXsl($a_output, $outputmode="presentation")
Modify page content after xsl.
hasHighlighter($hfile_ext)
setType($a_type)
Set Type.
static isSupported($a_lang)
Is language supported?
static getInstance($a_lang)
Get instance.
static getNewLanguageId($a_old_lang_id)
Get new language id (for an old one)
xpath_eval($xpath_context, $eval_str, $contextnode=null)
xpath_new_context($dom_document)