| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta charset="utf-8" /> |
| 6 | <link rel="icon" href="assets/favicon.png" /> |
| 7 | <title>FAQ ▲ Prism</title> |
| 8 | <link rel="stylesheet" href="assets/style.css" /> |
| 9 | <link rel="stylesheet" href="themes/prism.css" data-noprefix /> |
| 10 | <style> |
| 11 | #toc { |
| 12 | display: block; |
| 13 | position: static; |
| 14 | max-width: 900px; |
| 15 | font-size: 100%; |
| 16 | color: black; |
| 17 | } |
| 18 | </style> |
| 19 | <script src="assets/vendor/prefixfree.min.js"></script> |
| 20 | |
| 21 | <script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script> |
| 22 | <script src="https://www.google-analytics.com/ga.js" async></script> |
| 23 | </head> |
| 24 | <body class="language-none"> |
| 25 | |
| 26 | <header> |
| 27 | <div class="intro" data-src="assets/templates/header-main.html" data-type="text/html"></div> |
| 28 | |
| 29 | <h2>FAQ</h2> |
| 30 | <p>Frequently Asked Questions, with a few Questions I want people to Frequently Ask.</p> |
| 31 | </header> |
| 32 | |
| 33 | <section> |
| 34 | <h1>This page doesn’t work in Opera!</h1> |
| 35 | |
| 36 | <p><strong>Prism works fine in Opera.</strong> However, this page might sometimes appear to not be working in Opera, due to the theme switcher triggering an Opera bug. |
| 37 | This will be fixed soon.</p> |
| 38 | </section> |
| 39 | |
| 40 | <section> |
| 41 | <h1>Isn’t it bad to do syntax highlighting with regular expressions?</h1> |
| 42 | |
| 43 | <p>It is true that to correctly handle every possible case of syntax found in the wild, one would need to write a full-blown parser. |
| 44 | However, in most web applications and websites a small error margin is usually acceptable and a rare highlighting failure is not the end of the world. |
| 45 | A syntax highlighter based on regular expressions might only be accurate 99% of the time (the actual percentage is just a guess), |
| 46 | but in exchange for the small error margin, it offers some very important benefits: |
| 47 | |
| 48 | <ul> |
| 49 | <li>Smaller filesize. Proper parsers are very big.</li> |
| 50 | <li>Extensibility. Authors can define new languages simply by knowing how to code regular expressions. |
| 51 | Writing a correct, unambiguous BNF grammar is a task at least an order of magnitude harder.</li> |
| 52 | <li>Graceful error recovery. Parsers fail on incorrect syntax, where regular expressions keep matching.</li> |
| 53 | </ul> |
| 54 | |
| 55 | <p>For this reason, most syntax highlighters on the web and on desktop, are powered by regular expressions. This includes the internal syntax |
| 56 | highlighters used by popular native applications like Espresso and Sublime Text, at the time of writing. |
| 57 | Of course, not every regex-powered syntax highlighter is created equal. The number and type of failures can be vastly different, depending on |
| 58 | the exact algorithm used. <a href="known-failures.html">Prism’s known failures are documented on this page.</a>.</p> |
| 59 | </section> |
| 60 | |
| 61 | <section> |
| 62 | <h1>Why is asynchronous highlighting disabled by default?</h1> |
| 63 | |
| 64 | <p>Web Workers are good for preventing syntax highlighting of really large code blocks from blocking the main UI thread. |
| 65 | In most cases, you will want to highlight reasonably sized chunks of code, and this will not be needed. |
| 66 | Furthermore, using Web Workers is actually <strong>slower</strong> than synchronously highlighting, due to the overhead of creating and terminating |
| 67 | the Worker. It just appears faster in these cases because it doesn’t block the main thread. |
| 68 | In addition, since Web Workers operate on files instead of objects, plugins that hook on core parts of Prism (e.g. modify language definitions) |
| 69 | will not work unless included in the same file (using the builder in the <a href="download.html">Download</a> page will protect you from this pitfall). |
| 70 | Lastly, Web Workers cannot interact with the DOM and most other APIs (e.g. the console), so they are notoriously hard to debug. |
| 71 | </p> |
| 72 | </section> |
| 73 | |
| 74 | <section> |
| 75 | <h1>Why is pre-existing HTML stripped off?</h1> |
| 76 | |
| 77 | <p>Because it would complicate the code a lot, although it’s not a crucial feature for most people. |
| 78 | If it’s very important to you, you can use the <a href="plugins/keep-markup/">Keep Markup plugin</a>.</p> |
| 79 | </section> |
| 80 | |
| 81 | <section> |
| 82 | <h1>If pre-existing HTML is stripped off, how can I highlight certain parts of the code?</h1> |
| 83 | |
| 84 | <p>There is a number of ways around it. You can always break the block of code into multiple parts, and wrap the HTML around it (or just use a <code>.highlight</code> class). |
| 85 | You can see an example of this in action at the “<a href="index.html#basic-usage">Basic usage</a>” section of the homepage.</p> |
| 86 | <p>Another way around the limitation is to use the <a href="plugins/line-highlight/">Line Highlght plugin</a>, to highlight and link to specific lines and/or line ranges. |
| 87 | </section> |
| 88 | |
| 89 | <section> |
| 90 | <h1>How do I know which tokens I can style for every language?</h1> |
| 91 | |
| 92 | <p>Every token that is highlighted gets at least two classes: <code>token</code> and a class with the token type (e.g. <code>comment</code>) plus any number of aliases. |
| 93 | Aliases can be seen as additional token types and are used to give specialized tokens a more common class for easier styling. |
| 94 | You can find the different types of tokens either by looking at the keys of the object defining the language or by using the below interface.</p> |
| 95 | |
| 96 | <p>Language: <select id="language-select"></select></p> |
| 97 | <pre class="language-none" style="height: 30em"><code id="print-tokens-output"></code></pre> |
| 98 | |
| 99 | <p>Additionally, you can find a list of standard tokens on <a href="/tokens.html">this page</a>.</p> |
| 100 | </section> |
| 101 | |
| 102 | <section> |
| 103 | <h1>How can I use different highlighting for tokens with the same name in different languages?</h1> |
| 104 | <p>Just use a descendant selector, that includes the language class. The default <code>prism.css</code> does this, to have different colors for |
| 105 | JavaScript strings (which are very common) and CSS strings (which are relatively rare). Here’s that code, simplified to illustrate the technique: |
| 106 | <pre><code class="language-css">.token.string { |
| 107 | color: #690; |
| 108 | } |
| 109 | |
| 110 | .language-css .token.string, |
| 111 | .style .token.string { |
| 112 | color: #a67f59; |
| 113 | }</code></pre> |
| 114 | |
| 115 | <p>Abbreviated language classes (e.g. <code>lang-css</code>) will be converted to their extended forms, so you don’t need to account for them.</p> |
| 116 | <p>The same technique can be used to differentiate XML tag namespaces from attribute namespaces:</p> |
| 117 | <pre><code class="language-css">.tag > .token.namespace { |
| 118 | color: #b37298; |
| 119 | } |
| 120 | .attr-name > .token.namespace { |
| 121 | color: #ab6; |
| 122 | }</code></pre> |
| 123 | </section> |
| 124 | |
| 125 | <footer data-src="assets/templates/footer.html" data-type="text/html"></footer> |
| 126 | |
| 127 | <script src="assets/vendor/utopia.js"></script> |
| 128 | <script src="prism.js"></script> |
| 129 | <script src="plugins/autoloader/prism-autoloader.min.js"></script> |
| 130 | <script> |
| 131 | Prism.plugins.autoloader.languages_path = 'components/'; |
| 132 | </script> |
| 133 | <script src="components.js"></script> |
| 134 | <script> |
| 135 | (function () { |
| 136 | $$('section > h1').forEach(function (h1) { |
| 137 | $u.element.create('p', { |
| 138 | contents: { |
| 139 | tag: 'a', |
| 140 | properties: { |
| 141 | href: '#toc' |
| 142 | }, |
| 143 | contents: '↑ Back to top' |
| 144 | }, |
| 145 | inside: h1.parentNode |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | function printTokens(grammar) { |
| 150 | function toArray(x) { |
| 151 | return Array.isArray(x) ? x : (x == undefined ? [] : [x]); |
| 152 | } |
| 153 | |
| 154 | var lines = []; |
| 155 | function log(line) { |
| 156 | if (lines.indexOf(line) === -1) { |
| 157 | lines.push(line); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | var languageMap = new Map(); |
| 162 | Object.keys(Prism.languages).filter(function (l) { return l in components.languages; }).forEach(function (l) { |
| 163 | languageMap.set(Prism.languages[l], 'Prism.languages["' + l + '"]'); |
| 164 | }); |
| 165 | |
| 166 | var stack = new Map(); |
| 167 | |
| 168 | function inner(g, prefix) { |
| 169 | if (prefix && languageMap.has(g)) { |
| 170 | log(prefix + ' > ...' + languageMap.get(g)); |
| 171 | return; |
| 172 | } |
| 173 | if (stack.has(g)) { |
| 174 | log(prefix + ' > ...' + stack.get(g)); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | stack.set(g, '(' + (prefix || ':root:') + ')'); |
| 179 | |
| 180 | for (var name in g) { |
| 181 | var element = g[name]; |
| 182 | if (name === 'rest') { |
| 183 | inner(element, (prefix ? prefix + ' > ' : '') + ':rest:'); |
| 184 | } else { |
| 185 | for (var a = toArray(element), i = 0, token; token = a[i++];) { |
| 186 | var line = (prefix ? prefix + ' > ' : '') + name + toArray(token.alias).map(function (a) { |
| 187 | return '.' + a; |
| 188 | }).join(''); |
| 189 | |
| 190 | log(line); |
| 191 | |
| 192 | if (token.inside) { |
| 193 | inner(token.inside, line); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | stack.delete(g); |
| 200 | } |
| 201 | inner(grammar, ''); |
| 202 | |
| 203 | return lines.join('\n'); |
| 204 | } |
| 205 | |
| 206 | var loadedLanguages = {}; |
| 207 | function showTokens() { |
| 208 | var language = $('#language-select').value; |
| 209 | if (Prism.languages[language]) { |
| 210 | $('#print-tokens-output').textContent = printTokens(Prism.languages[language]); |
| 211 | } else if (language in loadedLanguages) { |
| 212 | $('#print-tokens-output').textContent = '"' + language + '" doesn\'t have any tokens.'; |
| 213 | } else { |
| 214 | // load grammar |
| 215 | Prism.plugins.autoloader.loadLanguages([language], function() { |
| 216 | loadedLanguages[language] = true; |
| 217 | showTokens(); |
| 218 | }, function () { |
| 219 | $('#print-tokens-output').textContent = 'Unable to load "' + language + '"'; |
| 220 | }); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | $('#language-select').onchange = showTokens; |
| 225 | $('#language-select').innerHTML = Object.keys(components.languages) |
| 226 | .filter(function (x) { return x !== 'meta'; }) |
| 227 | .map(function (x) { |
| 228 | return '<option value="' + x + '">' + components.languages[x].title + '</option>'; |
| 229 | }).join(''); |
| 230 | showTokens(); |
| 231 | }()); |
| 232 | </script> |
| 233 | <script src="assets/code.js"></script> |
| 234 | |
| 235 | </body> |
| 236 | </html> |