Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_yaml_highlight.html · 6.1 KB · 185 lines
| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Test YAML Highlighting</title> |
| 5 | <style> |
| 6 | body { |
| 7 | background: #1e1e1e; |
| 8 | color: #d4d4d4; |
| 9 | font-family: 'Consolas', 'Monaco', monospace; |
| 10 | padding: 20px; |
| 11 | } |
| 12 | #output { |
| 13 | background: #2d2d2d; |
| 14 | padding: 20px; |
| 15 | border-radius: 5px; |
| 16 | white-space: pre; |
| 17 | font-size: 14px; |
| 18 | line-height: 1.5; |
| 19 | } |
| 20 | h1 { color: #9cdcfe; } |
| 21 | </style> |
| 22 | </head> |
| 23 | <body> |
| 24 | <h1>Testing YAML Syntax Highlighting</h1> |
| 25 | <div id="output"></div> |
| 26 | |
| 27 | <script> |
| 28 | function syntaxHighlightYAML(yaml) { |
| 29 | // Create a temporary element to safely build HTML |
| 30 | const temp = document.createElement('div'); |
| 31 | |
| 32 | // First escape the YAML for HTML safety |
| 33 | const lines = yaml.split('\n'); |
| 34 | |
| 35 | lines.forEach(line => { |
| 36 | const lineSpan = document.createElement('span'); |
| 37 | |
| 38 | // Check for comments |
| 39 | if (line.includes('#')) { |
| 40 | const commentIndex = line.indexOf('#'); |
| 41 | const beforeComment = line.substring(0, commentIndex); |
| 42 | const comment = line.substring(commentIndex); |
| 43 | |
| 44 | // Add the part before the comment |
| 45 | processLine(lineSpan, beforeComment); |
| 46 | |
| 47 | // Add the comment in green |
| 48 | const commentSpan = document.createElement('span'); |
| 49 | commentSpan.style.color = '#6a9955'; |
| 50 | commentSpan.textContent = comment; |
| 51 | lineSpan.appendChild(commentSpan); |
| 52 | } else { |
| 53 | processLine(lineSpan, line); |
| 54 | } |
| 55 | |
| 56 | temp.appendChild(lineSpan); |
| 57 | temp.appendChild(document.createTextNode('\n')); |
| 58 | }); |
| 59 | |
| 60 | function processLine(container, line) { |
| 61 | // Match YAML key-value pattern |
| 62 | const keyMatch = line.match(/^(\s*)([a-zA-Z_][\w\s_-]*?)(:)(.*)$/); |
| 63 | if (keyMatch) { |
| 64 | const [, indent, key, colon, rest] = keyMatch; |
| 65 | |
| 66 | // Add indentation |
| 67 | container.appendChild(document.createTextNode(indent)); |
| 68 | |
| 69 | // Add key in blue |
| 70 | const keySpan = document.createElement('span'); |
| 71 | keySpan.style.color = '#9cdcfe'; |
| 72 | keySpan.textContent = key; |
| 73 | container.appendChild(keySpan); |
| 74 | |
| 75 | // Add colon |
| 76 | container.appendChild(document.createTextNode(colon)); |
| 77 | |
| 78 | // Process the value |
| 79 | if (rest) { |
| 80 | processValue(container, rest); |
| 81 | } |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Match list items |
| 86 | const listMatch = line.match(/^(\s*)(-)(\s+)(.*)$/); |
| 87 | if (listMatch) { |
| 88 | const [, indent, dash, space, rest] = listMatch; |
| 89 | |
| 90 | // Add indentation |
| 91 | container.appendChild(document.createTextNode(indent)); |
| 92 | |
| 93 | // Add dash in blue |
| 94 | const dashSpan = document.createElement('span'); |
| 95 | dashSpan.style.color = '#569cd6'; |
| 96 | dashSpan.textContent = dash; |
| 97 | container.appendChild(dashSpan); |
| 98 | |
| 99 | // Add space |
| 100 | container.appendChild(document.createTextNode(space)); |
| 101 | |
| 102 | // Process the rest |
| 103 | if (rest) { |
| 104 | processValue(container, rest); |
| 105 | } |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Default: just add the text |
| 110 | container.appendChild(document.createTextNode(line)); |
| 111 | } |
| 112 | |
| 113 | function processValue(container, value) { |
| 114 | const trimmed = value.trim(); |
| 115 | |
| 116 | // Check for quoted strings |
| 117 | if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || |
| 118 | (trimmed.startsWith("'") && trimmed.endsWith("'"))) { |
| 119 | const stringSpan = document.createElement('span'); |
| 120 | stringSpan.style.color = '#ce9178'; |
| 121 | stringSpan.textContent = value; |
| 122 | container.appendChild(stringSpan); |
| 123 | } |
| 124 | // Check for booleans and null |
| 125 | else if (trimmed === 'true' || trimmed === 'false' || trimmed === 'null') { |
| 126 | const spaces = value.substring(0, value.indexOf(trimmed)); |
| 127 | container.appendChild(document.createTextNode(spaces)); |
| 128 | |
| 129 | const boolSpan = document.createElement('span'); |
| 130 | boolSpan.style.color = '#569cd6'; |
| 131 | boolSpan.textContent = trimmed; |
| 132 | container.appendChild(boolSpan); |
| 133 | } |
| 134 | // Check for numbers |
| 135 | else if (/^\s*-?\d+\.?\d*\s*$/.test(value)) { |
| 136 | const spaces = value.substring(0, value.indexOf(value.trim())); |
| 137 | container.appendChild(document.createTextNode(spaces)); |
| 138 | |
| 139 | const numSpan = document.createElement('span'); |
| 140 | numSpan.style.color = '#b5cea8'; |
| 141 | numSpan.textContent = value.trim(); |
| 142 | container.appendChild(numSpan); |
| 143 | } |
| 144 | // Default |
| 145 | else { |
| 146 | container.appendChild(document.createTextNode(value)); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return temp.innerHTML; |
| 151 | } |
| 152 | |
| 153 | // Test YAML |
| 154 | const testYaml = `# Test YAML Config |
| 155 | name: "test service" |
| 156 | version: 1.0 |
| 157 | enabled: true |
| 158 | count: 42 |
| 159 | price: 10.00 |
| 160 | description: 'Single quoted string' |
| 161 | |
| 162 | items: |
| 163 | - name: "item1" |
| 164 | value: 100 |
| 165 | active: true |
| 166 | - name: "item2" |
| 167 | value: 200 |
| 168 | active: false |
| 169 | |
| 170 | settings: |
| 171 | debug: false |
| 172 | timeout: null |
| 173 | max_retries: 3 |
| 174 | |
| 175 | # Another comment |
| 176 | database: |
| 177 | host: localhost |
| 178 | port: 5432`; |
| 179 | |
| 180 | // Apply highlighting |
| 181 | document.getElementById('output').innerHTML = syntaxHighlightYAML(testYaml); |
| 182 | console.log('Highlighting applied successfully!'); |
| 183 | </script> |
| 184 | </body> |
| 185 | </html> |