Google Prettify wasn't displaying HTML properly for me until I found on stackoverflow that I should replace all the <
with <
and all the >
with >
.
Is this really necessary? Is there an easier way to accomplish this?
I want to display simple code like the following:
<h1>Header</h1>
<开发者_Go百科p>This is a paragraph tag. Here is a <a href="#">LINK</a></p>
Is there something I can use that will detect everything as code between the <pre>
tags? For example, as I type code in at stackoverflow, I'm not required to replace the <
and >
.
You could create a form with PHP to take the inputted HTML and replace the HTML tags using the PHP function htmlentities() OR you could use this URL I stumbled upon looking for a solution for you. http://www.boallen.com/htmlentities.html You input your code and it will output your code with htmlentities applied.
Not sure what your knowledge of PHP is but you can read how the function works here: http://us3.php.net/manual/en/function.htmlentities.php
Google needs it like that. you can use a preproccessor language such as php to replace the necessary characters. Im sure you can do it in javascript also.
Edit: Removed my earlier comment as it was supposed to be a reply to a different prettify thread :)
However, here is a local js solution to normalize HTML strings if it helps:
// HTML escape code repurposed from http://www.htmlescape.net/htmlescape_tool.html
function htmlEscapeString(unescaped_str)
{
var escaped="";
for(i=0; i<unescaped_str.length; i++)
{
escaped += escapeBR(escapeTags(escapeCharx(unescaped_str.charAt(i))));
}
return escaped;
}
function escapeBR(original)
{
var thechar=original.charCodeAt(0);
switch(thechar) {
case 10: return "";
case '\r': return "";
}
return original;
}
function escapeNBSP(original)
{
var thechar=original.charCodeAt(0);
switch(thechar) {
case 32: return " ";
}
return original;
}
function escapeTags(original)
{
var thechar=original.charCodeAt(0);
switch(thechar) {
case 60:return "<"; //<
case 62:return ">"; //>
case 34:return """; //"
}
return original;
}
function escapeCharx(original)
{
var c = original.charCodeAt(0);
switch(c) {
case 38:return "&";
case 198:return "Æ";
case 193:return "Á";
case 194:return "Â";
case 192:return "À";
case 197:return "Å";
case 195:return "Ã";
case 196:return "Ä";
case 199:return "Ç";
case 208:return "Ð";
case 201:return "É";
case 202:return "Ê";
case 200:return "È";
case 203:return "Ë";
case 205:return "Í";
case 206:return "Î";
case 204:return "Ì";
case 207:return "Ï";
case 209:return "Ñ";
case 211:return "Ó";
case 212:return "Ô";
case 210:return "Ò";
case 216:return "Ø";
case 213:return "Õ";
case 214:return "Ö";
case 222:return "Þ";
case 218:return "Ú";
case 219:return "Û";
case 217:return "Ù";
case 220:return "Ü";
case 221:return "Ý";
case 225:return "á";
case 226:return "â";
case 230:return "æ";
case 224:return "à";
case 229:return "å";
case 227:return "ã";
case 228:return "ä";
case 231:return "ç";
case 233:return "é";
case 234:return "ê";
case 232:return "è";
case 240:return "ð";
case 235:return "ë";
case 237:return "í";
case 238:return "î";
case 236:return "ì";
case 239:return "ï";
case 241:return "ñ";
case 243:return "ó";
case 244:return "ô";
case 242:return "ò";
case 248:return "ø";
case 245:return "õ";
case 246:return "ö";
case 223:return "ß";
case 254:return "þ";
case 250:return "ú";
case 251:return "û";
case 249:return "ù";
case 252:return "ü";
case 253:return "ý";
case 255:return "ÿ";
case 162:return "¢";
default: break;
}
if( c <= 127 )
return original;
var a4=c%16;
c = Math.floor(c/16);
var a3=c%16;
c = Math.floor(c/16);
var a2=c%16;
c = Math.floor(c/16);
var a1=c%16;
return "&#x"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+";";
}
精彩评论