我一直在努力替换我网站上发布的所有代码,但不适合我。
我需要<pre><code></code></pre>
中出现的所有内容来重放所有标签,并使用css给它一个颜色,这里是我一直试图使用的代码示例
function Rep(text){
text = text.replace(/ /g," ");
text = text.replace(/<([^\>]+?)>/g, function(match, contents){
return "<span class='TagColor'><"+contents+"></span>";
}
);
text = text.replace(/\"([^\"]+?)\"/g, function(match, contents){
return '<span class="StringColor">"'+contents+'"</span>';
}
);
text = text.replace(/\(([^\)]+?)\)/g, function(match, contents){
return '<span class="OptionsColor">('+contents+')</span>';
}
);
text = text.replace(/\n/g, function(match, contents){
return '<br />';
}
);
text = text.replace(/\{/g, function(match, contents){
return '<span class="BracketsColor">{</span>';
}
);
text = text.replace(/\}/g, function(match, contents){
return '<span class="BracketsColor">}</span>';
}
);
text = text.replace(/\/\*([^\*]+?)\*\//g, function(match, contents){
return "<span class='CommentColor'>/*"+contents+"*/</span>";
}
);
text = text.replace(/\[\](.*?)\[\/\]/g, function(match, contents){
return '<span class="BBColor">'+contents+'</span>';
}
);
return text;
}
//HTML replace
//==================================================
$('code').each(function(){
var text=$(this).html().replace(/</g,'<');
var text=text.replace(/>/g,'>');
$(this).html(text);
});
//google-code-prettify
//==================================================
jQuery(window).load(function(){
prettyPrint();
});
//Still on Text format
var app = angular.module('codelab',[],['$interpolateProvider',
function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}]);
app.directive('ngPrism',['$interpolate', function ($interpolate) {
"use strict";
return {
restrict: 'E',
template: '<pre id="Mpreditor" class="code code-codes language-javascript"><code ng-transclude="ng-transclude" class="language-javascript"> </code></pre>',
replace:true,
transclude:true
};
}]);
以下是代码在我的网站中的显示方式
<pre id="Mpreditor" class="code code-codes language-javascript">
<code ng-transclude="ng-transclude" class="language-javascript">
<span>Hello</span>
You have to include the `%` signs in the `$params`, not in the query:
$query = "SELECT * FROM tbl WHERE address LIKE ? OR address LIKE ?";
$params = array("%$var1%", "%$var2%");
$stmt = $handle->prepare($query);
$stmt->execute($params);
</code></pre>
答案 0 :(得分:0)
您应该考虑使用https://jmblog.github.io/color-themes-for-google-code-prettify/,它基本上完全符合您的要求。
它利用Googles Code Prettifier:https://github.com/google/code-prettify