Сообщение от ksa
|
Если сможешь повторить алгоритмы анализа текста, как это делают те редакторы
|
Как вариант...
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=windows-1251' />
<!--
<script src='https://code.jquery.com/jquery-latest.js'></script>
<script src="https://code.angularjs.org/1.3.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.9/angular-route.js"></script>
-->
<style type='text/css'>
.number {
color: blue;
}
.operation {
color: red;
}
.words {
font-style: italic;
font-weight: bold;
}
.hooks {
color: green;
}
</style>
<script type='text/javascript'>
document.addEventListener("DOMContentLoaded",function(){
var result = document.querySelector('.result');
var code = document.querySelector('.code');
var sum;
function calc(){
sum = 20 + 15 * 2 / 2;
return test(sum);
}
function test(sum){
result.innerHTML = sum;
}
calc();
var codeOut = new calc();
var html=codeOut.constructor.toString();
html=html.replace(/\t+\}$/,'}');
html=html.replace(/([-+\/*=])/g,'<span class="operation">$1</span>');
html=html.replace(/(function|return)/g,'<span class="words">$1</span>');
html=html.replace(/([\(\)\{\}\[\]])/g,'<span class="hooks">$1</span>');
html=html.replace(/(\d+)/g,'<span class="number">$1</span>');
html=html.replace(/\n/g,'<br />');
html=html.replace(/\t/g,' ');
code.innerHTML = html;
});
</script>
</head>
<body>
<div class="code"></div>
<div class="result"></div>
</body>
</html>