我正在使用php(不使用Java)开发基于Web的应用程序,我需要在网页上显示语法高亮的源代码文件(java,c,c ++,Python等)。关于如何显示源代码文件我一无所知。任何帮助将不胜感激。
答案 0 :(得分:3)
其中一个选项是使用现有的syntax highlighter like a google one。
设置非常简单。您要为basic usage所做的就是在<pre>
部分的html页面中包含代码,并应用编程语言的类属性。
<pre name="code" class="c-sharp">
class Foo
{
}
</pre>
16 Free Javascript Code Syntax Highlighters For Better Programming 有很多选项的详尽列表。如果网站出现故障,请重复此处
答案 1 :(得分:0)
语法荧光笔用于显示源代码程序是彩色的,因此读者可以在集成后轻松阅读/理解您的代码。在此程序中,我添加了各种元素(保留字,括号,注释和引号等)以突出显示。您可以基于Web应用程序/编程博客添加/修改此代码。
此语法荧光笔代码独立于任何语言,因此您可以添加与任何编程语言的集成。
请从我的技术博客中找到源代码-http://www.algonuts.info/how-to-develop-a-source-code-syntax-highlighter-using-php.html
<?php
include_once("keywords.php");
class highlighter {
private $fileName;
private $fileNameColor;
private $fileExtension;
private $parenthesisColor;
private $insideParenthesisColor;
private $keywordColor;
private $backGroundColor;
private $borderColor;
private $leftBorderColor;
private $quotesColor;
private $commentColor;
public function __construct() {
$this->fileName = "";
$this->fileExtension = "";
//Color Configuration
$this->fileNameColor = "#286090";
$this->keywordColor = "green";
$this->backGroundColor = "#fdfefe";
$this->borderColor = "#e3e3e3";
$this->leftBorderColor = "#605a56";
$this->parenthesisColor = "#ec7700";
$this->insideParenthesisColor = "#ec7700";
$this->bracketColor = "#ec7700";
$this->insideBracketColor = "#ec7700";
$this->quotesColor = "#6a2c70";
$this->commentColor = "#b8b0b0";
}
public function applycolor($fileLocation = "") {
if($fileLocation == "")
{ return; }
else
{
if(file_exists($fileLocation)) {
$temp = explode("/",$fileLocation);
$this->fileName = trim(end($temp));
$temp = explode(".",$this->fileName);
$this->fileExtension = trim(end($temp));
$fileContent = trim(file_get_contents($fileLocation, true));
$fileContent = htmlentities($fileContent,ENT_NOQUOTES);
if($fileContent == "")
{ return; }
}
else
{ return; }
}
$line = 1;
$outputContent = "<div class=\"divblock\"><b>".$line."</b> ";
$characterBuffer = "";
$blockFound = 0;
$blockFoundColor = array();
$parenthesisFound = 0;
$bracketFound = 0;
$counter = 0;
$lastCharacter = "";
$contentSize = strlen($fileContent);
while($counter < $contentSize) {
$character = $fileContent[$counter];
$code = intval(ord($character));
if($blockFound == 0 && (($code >= 97 && $code <= 122) || ($code >= 65 && $code <= 90))) //Fnd alphabetic characters
{ $characterBuffer .= $character; }
else
{
if($code == 10) { //Find EOL (End of Line)
if($this->checker($characterBuffer))
{ $characterBuffer = "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
$line++;
if($blockFound == 0)
{ $outputContent .= $characterBuffer."</div>".$character."<div class=\"divblock\"><b>".$line."</b> "; }
else
{ $outputContent .= $characterBuffer."</font></div>".$character."<div class=\"divblock\"><b>".$line."</b> <font color='".$blockFoundColor[$blockFound-1]."'>"; }
$characterBuffer = "";
}
else if($code == 32) { //Find Space
if($characterBuffer != "") {
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character; }
else
{ $outputContent .= $characterBuffer.$character; }
$characterBuffer = "";
}
else
{ $outputContent .= $character; }
}
else if($character == "\"" || $character == "'") { //Find Quotes
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
$outputContent .= "<font color='".$this->quotesColor."'>".$character;
$foundCharacter = $character;
$counter++;
while($counter < $contentSize) {
$character = $fileContent[$counter];
if($character == $foundCharacter) {
$outputContent .= $character;
if($lastCharacter == "\\") {
$lastCharacter = "";
}
else
{ break; }
}
else if($character == "\\" && $lastCharacter == "\\") {
$outputContent .= $character;
$lastCharacter = "";
}
else
{
$lastCharacter = $character;
$code = intval(ord($character));
if($code != 10)
{ $outputContent .= $character; }
else
{
$line++;
$outputContent .= "</font></div>".$character."<div class=\"divblock\"><b>".$line."</b> <font color='".$this->quotesColor."'>";
}
}
$counter++;
}
$outputContent .= "</font>";
}
else if($character == "(" || $character == ")") { //Find Parenthesis
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
if($parenthesisFound == 0) {
$blockFoundColor[$blockFound] = $this->insideParenthesisColor;
$outputContent .= "<font color='".$this->parenthesisColor."'>".$character."</font><font color='".$this->insideParenthesisColor."'>";
$parenthesisFound++;
$blockFound++;
}
else
{
if($character == "(") {
$parenthesisFound++;
}
if($character == ")") {
$parenthesisFound--;
}
if($parenthesisFound == 0) {
$outputContent .= "</font><font color='".$this->parenthesisColor."'>".$character."</font>";
$blockFound--;
unset($blockFoundColor[$blockFound]);
}
else
{ $outputContent .= $character; }
}
}
else if($character == "[" || $character == "]") { //Find Bracket
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
if($bracketFound == 0) {
$blockFoundColor[$blockFound] = $this->insideBracketColor;
$outputContent .= "<font color='".$this->bracketColor."'>".$character."</font><font color='".$this->insideBracketColor."'>";
$bracketFound++;
$blockFound++;
}
else
{
if($character == "[") {
$bracketFound++;
}
if($character == "]") {
$bracketFound--;
}
if($bracketFound == 0) {
$outputContent .= "</font><font color='".$this->bracketColor."'>".$character."</font>";
$blockFound--;
unset($blockFoundColor[$blockFound]);
}
else
{ $outputContent .= $character; }
}
}
else if($character == "/" && (isset($fileContent[$counter+1]) && ($fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/"))) { //Find Comment
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
$blockFound++;
$outputContent .= "<font color='".$this->commentColor."'>".$fileContent[$counter].$fileContent[$counter+1];
if($fileContent[$counter+1] == "*") {
$counter += 2;
$checkCharacter = "*";
while($counter < $contentSize) {
$outputContent .= $fileContent[$counter];
if($fileContent[$counter] == $checkCharacter) {
if($checkCharacter == "*")
{ $checkCharacter = "/"; }
else
{
$blockFound--;
$outputContent .= "</font>";
break;
}
}
$counter++;
}
}
else
{
$counter += 2;
while($counter < $contentSize) {
$character = $fileContent[$counter];
$code = intval(ord($character));
if($code == 10) {
$counter--;
$blockFound--;
$outputContent .= "</font>";
break;
}
$outputContent .= $character;
$counter++;
}
}
}
else if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character; }
else
{ $outputContent .= $characterBuffer.$character; }
$characterBuffer = "";
}
else
{ $outputContent .= $character; }
}
$counter++;
}
$outputContent .= "</div>";
$rerurnData = "<div class='filenamestyle' style='color:".$this->fileNameColor.";'>".$this->fileName."</div>"; //Show filename
$rerurnData .= "<div><pre><div class='codebox' style='background-color:".$this->backGroundColor.";border: 1px solid ".$this->borderColor.";border-left: 4px solid ".$this->leftBorderColor.";'>".$outputContent."</div></pre></div>";
return $rerurnData;
}
private function checker($value) {
global $languageKeywords;
if(isset($languageKeywords[$this->fileExtension])) {
$value = trim($value);
if(in_array($value,$languageKeywords[$this->fileExtension]))
{ return 1; }
else
{ return 0; }
}
}
}
?>