Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2. var fs = require('fs');
  3.  
  4. fs.unlink('index.html', function (err,data) {
  5.   console.log(data);
  6. });
  7.  
  8. fs.readFile('code.txt', 'utf8', function (err,data) {
  9.   if (err) {
  10.     return console.log(err);
  11.   }
  12.   console.log(data);
  13.   var splittedFile = data.split('\n');
  14.  
  15.   const content = splittedFile.map(function (line) {
  16.     console.log(line);
  17.     return ('<div class='+ getClassLine(line) +'>' + prepareLine(line) + '</div>')
  18.   });
  19.  
  20.   const html = generateHtml(content.join(" "))
  21.  
  22.      fs.appendFile('index.html', html, function (err) {
  23.        if (err) throw err;
  24.        console.log('Saved!');
  25.      });
  26. });
  27.  
  28. http.createServer(function (req, res) {
  29.   var html = buildHtml(req);
  30.  
  31.   res.writeHead(200, {
  32.     'Content-Type': 'text/html',
  33.     'Content-Length': html.length,
  34.     'Expires': new Date().toUTCString()
  35.   });
  36.  
  37.   res.end(html);
  38. }).listen(4000,
  39.   console.log('Your site available at http://localhost:4000')
  40. );
  41.  
  42. function buildHtml(req) {
  43.   var header = '';
  44.   var body = '';
  45.  
  46.   return '<!DOCTYPE html>'
  47.        + '<html><head>' + header + '</head><body>' + body + '</body></html>';
  48. };
  49.  
  50. function generateHtml(content) {
  51.   return '<!DOCTYPE html>'
  52.        + '<html><head>'
  53.        + '<style>'
  54.        + 'html { color: #FFF; background: #081b2a; font-family: "Roboto Mono", monospace; } '
  55.        + '.lib { color: #82c3b8;} '
  56.        + '.hash { color: #42dff4;} '
  57.        + '.comment { color: #577482;} '
  58.        + '.type { color: #ffaba0;} '
  59.        + '.condition { color: #87c94a;} '
  60.        + '.important { color: #caa3ff;} '
  61.        + '.printers { color: #e6cadd;} '
  62.        + '.equals { color: #ffaba0;} '
  63.        + '</style>'
  64.        + '<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">'
  65.        + '</head><body>'
  66.        + content
  67.        + '</body></html>';
  68. };
  69.  
  70. function prepareLine(line) {
  71.   const splittedLine = line.split(' ');
  72.   const preparedLine = splittedLine.map(function (code) {
  73.     return '<span class='+ getClassCode(code) +'>' + formatCode(code) + '</span>';
  74.   });
  75.   return preparedLine.join(" ");
  76. };
  77.  
  78. function formatCode(code) {
  79.   if(code === '') {
  80.     return '&nbsp;';
  81.   } else if (code[0] === '<') {
  82.     if (code[code.length-1] === '>')  {
  83.       return "<span class='lib'>&lt;" + code.substring(1) + "</span>";
  84.     }
  85.     return '&lt;';
  86.   }
  87.   return code;
  88. }
  89.  
  90. function getClassLine(line) {
  91.   const lineWithoutSpaces = line.split(' ').join('');
  92.   console.log('Line', lineWithoutSpaces);
  93.   if(line[0] === '#') return "hash"
  94.   if(lineWithoutSpaces[0] === '/' && lineWithoutSpaces[1] === '/') return "comment"
  95.   return "";
  96. }
  97.  
  98. function getClassCode(code) {
  99.   if(code.match(/^(int|string|void)$/)) return "type"
  100.   if(code.match(/^(for|if|else|while)$/)) return "condition"
  101.   if(code.match(/^(return|break;)$/)) return "important"
  102.   if(code.match(/^(printf|print|scanf)$/)) return "printers"
  103.   if(code.match(/^(==|===|!=)$/)) return "equals"
  104.   return ""
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement