Use pre tag for code in live preview.

This commit is contained in:
bootstraponline
2012-05-09 16:15:48 -06:00
parent d1f90b7d44
commit fd32706f0b
3 changed files with 70 additions and 71 deletions
@@ -4,126 +4,120 @@ github.com style (c) Vasily Polovnyov <vast@whiteants.net>
*/ */
code { pre .comment,
display: block; padding: 0.5em; pre .template_comment,
color: #000; pre .diff .header,
background: #f8f8ff pre .javadoc {
}
code .comment,
code .template_comment,
code .diff .header,
code .javadoc {
color: #998; color: #998;
font-style: italic font-style: italic
} }
code .keyword, pre .keyword,
code .css .rule .keyword, pre .css .rule .keyword,
code .winutils, pre .winutils,
code .javascript .title, pre .javascript .title,
code .lisp .title, pre .lisp .title,
code .subst { pre .subst {
color: #000; color: #000;
font-weight: bold font-weight: bold
} }
code .number, pre .number,
code .hexcolor { pre .hexcolor {
color: #40a070 color: #40a070
} }
code .string, pre .string,
code .tag .value, pre .tag .value,
code .phpdoc, pre .phpdoc,
code .tex .formula { pre .tex .formula {
color: #d14 color: #d14
} }
code .title, pre .title,
code .id { pre .id {
color: #900; color: #900;
font-weight: bold font-weight: bold
} }
code .javascript .title, pre .javascript .title,
code .lisp .title, pre .lisp .title,
code .subst { pre .subst {
font-weight: normal font-weight: normal
} }
code .class .title, pre .class .title,
code .haskell .label, pre .haskell .label,
code .tex .command { pre .tex .command {
color: #458; color: #458;
font-weight: bold font-weight: bold
} }
code .tag, pre .tag,
code .tag .title, pre .tag .title,
code .rules .property, pre .rules .property,
code .django .tag .keyword { pre .django .tag .keyword {
color: #000080; color: #000080;
font-weight: normal font-weight: normal
} }
code .attribute, pre .attribute,
code .variable, pre .variable,
code .instancevar, pre .instancevar,
code .lisp .body { pre .lisp .body {
color: #008080 color: #008080
} }
code .regexp { pre .regexp {
color: #009926 color: #009926
} }
code .class { pre .class {
color: #458; color: #458;
font-weight: bold font-weight: bold
} }
code .symbol, pre .symbol,
code .ruby .symbol .string, pre .ruby .symbol .string,
code .ruby .symbol .keyword, pre .ruby .symbol .keyword,
code .ruby .symbol .keymethods, pre .ruby .symbol .keymethods,
code .lisp .keyword, pre .lisp .keyword,
code .tex .special, pre .tex .special,
code .input_number { pre .input_number {
color: #990073 color: #990073
} }
code .builtin, pre .builtin,
code .built_in, pre .built_in,
code .lisp .title { pre .lisp .title {
color: #0086b3 color: #0086b3
} }
code .preprocessor, pre .preprocessor,
code .pi, pre .pi,
code .doctype, pre .doctype,
code .shebang, pre .shebang,
code .cdata { pre .cdata {
color: #999; color: #999;
font-weight: bold font-weight: bold
} }
code .deletion { pre .deletion {
background: #fdd background: #fdd
} }
code .addition { pre .addition {
background: #dfd background: #dfd
} }
code .diff .change { pre .diff .change {
background: #0086b3 background: #0086b3
} }
code .chunk { pre .chunk {
color: #aaa color: #aaa
} }
code .tex .formula { pre .tex .formula {
opacity: 0.5; opacity: 0.5;
} }
@@ -207,22 +207,27 @@ var makePreviewHtml = function () {
previewSet(text); previewSet(text);
// highlight code blocks. // highlight code blocks.
var codeElements = preview.getElementsByTagName("code"); var codeElements = preview.getElementsByTagName("pre");
var codeElementsLength = codeElements.length; var codeElementsLength = codeElements.length;
if (codeElementsLength > 0) { if (codeElementsLength > 0) {
for (var idx = 0; idx < codeElementsLength; idx++) { for (var idx = 0; idx < codeElementsLength; idx++) {
var element = codeElements[idx]; var element = codeElements[idx];
var codeHTML = element.innerHTML; var codeHTML = element.innerHTML;
var txt = element.innerHTML.split(/\b/);
// the syntax for code highlighting means all code, even one line, contains newlines.
if (txt.length > 0 && codeHTML.match(/\n/)) {
if ($.inArray(txt[0], languages) === -1) {
continue;
}
element.className = txt[0] + " highlight"; // Only use pre tags marked containing code.
element.innerHTML = codeHTML.substring(txt[0].length); if (codeHTML[0] !== "`")
continue;
var txt = codeHTML.split(/\b/);
// the syntax for code highlighting means all code, even one line, contains newlines.
if (txt.length > 1 && codeHTML.match(/\n/)) {
// txt[0] = "`"; txt[1] = "ruby"
if ($.inArray(txt[1], languages) === -1) continue;
element.className = txt[1] + " highlight";
// length + 1 for the marker character.
element.innerHTML = codeHTML.substring(txt[1].length+1).trim();
hljs.highlightBlock(element, " "); hljs.highlightBlock(element, " ");
} else { } else {
element.className = "nohighlight"; element.className = "nohighlight";
@@ -1020,7 +1020,7 @@ else
function _DoCodeSpansGollum(text) { function _DoCodeSpansGollum(text) {
// //
// Same as _DoCode except code is wrapped in pre for proper multiline. // Gollum wraps code in one pre. Use ` to mark code pre.
// All regex set to use 'm' multiline option. // All regex set to use 'm' multiline option.
// //
text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
@@ -1030,7 +1030,7 @@ else
c = c.replace(/[ \t]*$/gm, ""); // trailing whitespace c = c.replace(/[ \t]*$/gm, ""); // trailing whitespace
c = _EncodeCode(c); c = _EncodeCode(c);
c = c.replace(/:\/\//gm, "~P"); // to prevent auto-linking. Not necessary in code *blocks*, but in code spans. Will be converted back after the auto-linker runs. c = c.replace(/:\/\//gm, "~P"); // to prevent auto-linking. Not necessary in code *blocks*, but in code spans. Will be converted back after the auto-linker runs.
return m1 + "<pre><code>" + c + "</code></pre>"; return m1 + "<pre>`" + c + "</pre>";
} }
); );