Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var fs = require('fs');
- fs.unlink('index.html', function (err,data) {
- console.log(data);
- });
- fs.readFile('code.txt', 'utf8', function (err,data) {
- if (err) {
- return console.log(err);
- }
- console.log(data);
- var splittedFile = data.split('\n');
- const content = splittedFile.map(function (line) {
- console.log(line);
- return ('<div class='+ getClassLine(line) +'>' + prepareLine(line) + '</div>')
- });
- const html = generateHtml(content.join(" "))
- fs.appendFile('index.html', html, function (err) {
- if (err) throw err;
- console.log('Saved!');
- });
- });
- http.createServer(function (req, res) {
- var html = buildHtml(req);
- res.writeHead(200, {
- 'Content-Type': 'text/html',
- 'Content-Length': html.length,
- 'Expires': new Date().toUTCString()
- });
- res.end(html);
- }).listen(4000,
- console.log('Your site available at http://localhost:4000')
- );
- function buildHtml(req) {
- var header = '';
- var body = '';
- return '<!DOCTYPE html>'
- + '<html><head>' + header + '</head><body>' + body + '</body></html>';
- };
- function generateHtml(content) {
- return '<!DOCTYPE html>'
- + '<html><head>'
- + '<style>'
- + 'html { color: #FFF; background: #081b2a; font-family: "Roboto Mono", monospace; } '
- + '.lib { color: #82c3b8;} '
- + '.hash { color: #42dff4;} '
- + '.comment { color: #577482;} '
- + '.type { color: #ffaba0;} '
- + '.condition { color: #87c94a;} '
- + '.important { color: #caa3ff;} '
- + '.printers { color: #e6cadd;} '
- + '.equals { color: #ffaba0;} '
- + '</style>'
- + '<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">'
- + '</head><body>'
- + content
- + '</body></html>';
- };
- function prepareLine(line) {
- const splittedLine = line.split(' ');
- const preparedLine = splittedLine.map(function (code) {
- return '<span class='+ getClassCode(code) +'>' + formatCode(code) + '</span>';
- });
- return preparedLine.join(" ");
- };
- function formatCode(code) {
- if(code === '') {
- return ' ';
- } else if (code[0] === '<') {
- if (code[code.length-1] === '>') {
- return "<span class='lib'><" + code.substring(1) + "</span>";
- }
- return '<';
- }
- return code;
- }
- function getClassLine(line) {
- const lineWithoutSpaces = line.split(' ').join('');
- console.log('Line', lineWithoutSpaces);
- if(line[0] === '#') return "hash"
- if(lineWithoutSpaces[0] === '/' && lineWithoutSpaces[1] === '/') return "comment"
- return "";
- }
- function getClassCode(code) {
- if(code.match(/^(int|string|void)$/)) return "type"
- if(code.match(/^(for|if|else|while)$/)) return "condition"
- if(code.match(/^(return|break;)$/)) return "important"
- if(code.match(/^(printf|print|scanf)$/)) return "printers"
- if(code.match(/^(==|===|!=)$/)) return "equals"
- return ""
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement