diff --git a/app/assets/stylesheets/_variables.scss b/app/assets/stylesheets/_variables.scss index 70c691ebb..be7e1c944 100644 --- a/app/assets/stylesheets/_variables.scss +++ b/app/assets/stylesheets/_variables.scss @@ -14,3 +14,4 @@ $autolab-white: #fff; $autolab-selected-gray: #f5f5f5; $autolab-border-gray: #f4f1f1; $autolab-gray-text: #676464; +$autolab-highlight-comments: #008000; diff --git a/app/assets/stylesheets/annotations.scss b/app/assets/stylesheets/annotations.scss index d8dc4ce9b..8cd47478c 100755 --- a/app/assets/stylesheets/annotations.scss +++ b/app/assets/stylesheets/annotations.scss @@ -937,5 +937,12 @@ span > .material-icons { font-size: 14px !important; box-shadow: 0.5px 0.5px 0.5px 0.5px grey !important; border-color: white !important; -} +} + +.hljs-comment span { + color: var($autolab-highlight-comments) !important; +} +.code-line div { + background: none !important; +} diff --git a/app/views/submissions/view.html.erb b/app/views/submissions/view.html.erb index eab17fa35..ab6619b6e 100755 --- a/app/views/submissions/view.html.erb +++ b/app/views/submissions/view.html.erb @@ -52,6 +52,59 @@ var headerPositionStr = null; <% end %> hljs.initHighlightingOnLoad(); + function highlightComments () { + // Highlights all code as a cohesive block + let combinedCode = ''; + document.querySelectorAll('pre code').forEach((block) => { + combinedCode += block.textContent + '\n'; + }); + let highlightedCode = hljs.highlightAuto(combinedCode).value; + let htmlObject = document.createElement('div'); + htmlObject.innerHTML = highlightedCode; + // Get all spans that are highlighted as comments or strings + let comments = htmlObject.getElementsByClassName('hljs-comment'); + let strings = htmlObject.getElementsByClassName('hljs-string'); + let content = [...comments, ...strings]; + let splitContent = []; + // Split each span into multiple lines + for (let con of content) { + let innerSpans = con.innerText.split("\n").filter(line => line.trim() !== ""); + innerSpans.forEach((span) => { + // Check if span starts with # and language is cpp or c + // if so we highlight it seperately and check if its a comment + // If it is not a comment, do not add it into the comments array + let highlightedSpan = hljs.highlightAuto(span); + if (span.startsWith("#") && (highlightedSpan.language === 'cpp' || highlightedSpan.language === 'c')) { + let highlightedKeyWord = highlightedSpan.value; + let htmlTestDiv = document.createElement('div'); + htmlTestDiv.innerHTML = highlightedKeyWord; + if (htmlTestDiv.getElementsByClassName('hljs-comment').length >= 1) { + splitContent.push(span); + } + } else { + splitContent.push(span); + } + }); + } + // If a line of code has content that is highlighted as a comment above, + // we wrap the content with the hljs-comment class to highlight it + document.querySelectorAll('pre code').forEach((block) => { + if (block.textContent !== null && block.textContent !== "" && splitContent.includes(block.textContent.replace(/\n/g, ''))) { + let escapedText = document.createTextNode(block.textContent); + let codeElement = document.createElement('span'); + codeElement.className = 'hljs-comment'; + codeElement.appendChild(escapedText); + block.innerHTML = ''; + block.appendChild(codeElement); + } + }); + } + window.addEventListener('DOMContentLoaded', () => { + highlightComments(); + }); + window.navigation.addEventListener("navigate", () => { + highlightComments(); + }); PDFJS.workerSrc = "<%= asset_url 'pdf.worker.js' %>"; <%= render partial: "golden-layout" %> @@ -70,12 +123,12 @@ <% end %>