喜欢 Confluence 最主要的原因其实是因为我喜欢他强大的wiki语法,今天闲的无事,试着用javascript写了看看:
<html>
<head>
<title>Demo</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
Text goes here:
<br>
<textarea rows="15" cols="120" id="input" name="input" onchange="parse();">
</textarea>
<br/>
Output:
<div id="output"/>
</body>
</html>
<script type="text/javascript">
function parse() {
//{"pattern":"","replacement":""}
var line = "(.*)";
var words = "((.*\\n?)*)";
var breaklines = "\\n";
var patterns = new Array(
//Headings
{"pattern":"h1\\." + line ,"replacement":"<h1 class=\"heading1\">$1</h1>"},
{"pattern":"h2\\." + line,"replacement":"<h2 class=\"heading1\">$1</h1>"},
{"pattern":"h3\\." + line ,"replacement":"<h3 class=\"heading1\">$1</h1>"},
{"pattern":"h4\\." + line ,"replacement":"<h4 class=\"heading1\">$1</h1>"},
{"pattern":"h5\\." + line ,"replacement":"<h5 class=\"heading1\">$1</h1>"},
{"pattern":"h6\\." + line ,"replacement":"<h6 class=\"heading1\">$1</h1>"},
//Text Effects
{"pattern":"\\*" + line + "\\*","replacement":"<b>$1</b>"},
{"pattern":"_" + line + "_","replacement":"<em>$1</em>"},
{"pattern":"\\?\\?" + line + "\\?\\?","replacement":"<cite>$1</cite>"},
{"pattern":"-" + line + "-","replacement":"<del>$1</del>"},
{"pattern":"\\+" + line + "\\+","replacement":"<u>$1</u>"},
{"pattern":"\\^" + line + "\\^","replacement":"<sup>$1</sup>"},
{"pattern":"~" + line + "~","replacement":"<sub>$1</sub>"},
{"pattern":"\\{\\{" + line + "\\}\\}","replacement":"<tt class=\"monospaced\">$1</tt>"},
{"pattern":"bq\\." + line,"replacement":"<blockquote class=\"blockquote\">$1</blockquote>"},
{"pattern":"\\{quote\\}" + words + "\\{quote\\}","replacement":"<blockquote class=\"blockquote\">$1</blockquote>"},
//{"pattern":"\\{color:(\\w*)\\}" + words + "\\{color\\}","replacement":"<font color=\"$1\">$2</font>"},
//Text Breaks
//Links
// Lists
//Images
//Tables
//Advanced Formatting
{"pattern":"\\{noformat\\}((.*\\n?)*)\\{noformat\\}","replacement":"<div class=\"preformatted\"><div class=\"preformattedContent\"><pre>$1</pre></div></div>"}
//Confluence Content
//External Content
//Misc
)
;
var text = document.getElementById("input").getAttribute("value") + "\n";
alert(text);
for (var element in patterns) {
var pattern = new RegExp(patterns[element].pattern, "gi");
text = text.replace(pattern, patterns[element].replacement)
}
document.getElementById("output").innerHTML = text;
}
parse();
</script> 看起来,还不错,但是有个要命的问题是,IE运行的时候会挂
不管它,Just for Fun
明天继续.