├── .gitignore ├── History.md ├── Makefile ├── Readme.md ├── component.json ├── lib └── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | components/ -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.0.3 / 2015-09-17 3 | ================== 4 | 5 | * guarding for loop 6 | * fix elements 7 | 8 | 0.0.2 - March 5, 2013 9 | --------------------- 10 | * properly highlight all 11 | 12 | 0.0.1 - February 20, 2013 13 | ------------------------- 14 | :sparkles: 15 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | build: components lib/* 3 | @component build --dev 4 | 5 | clean: 6 | @rm -rf build components 7 | 8 | components: component.json 9 | @component install --dev 10 | 11 | test: build 12 | @component test phantom 13 | 14 | .PHONY: clean test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # highlight 2 | 3 | A simple, pluggable API for syntax highlighting. 4 | 5 | Syntax highlighters tend to have pretty opinionated APIs, both in terms of when to highlight, and how to determine the language. And lots bundle the languages directly into the core library, which makes it much harder to reason about them individually, or to have the smallest possible file size if you don't need the esoteric ones. 6 | 7 | So... we made this one. The API is very simple, yet still gives you full control. The language definitions are all [separate plugins](#languages), so you get the smallest possible build size, and so that they're simpler for everyone to contribute to. Because regexes are already hard enough to read as it is! 8 | 9 | ## Installation 10 | 11 | $ component install segmentio/highlight 12 | 13 | ## Example 14 | 15 | ```js 16 | var Highlight = require('highlight') 17 | var html = require('highlight-xml'); 18 | var js = require('highlight-javascript'); 19 | 20 | var highlight = new Highlight() 21 | .use(html) 22 | .use(js); 23 | 24 | var el = document.querySelector('.code-sample'); 25 | highlight.element(el); 26 | ``` 27 | 28 | ...or if you're lazy, you can just pass a selector string: 29 | 30 | ```js 31 | highlight.element('.code-sample'); 32 | ``` 33 | 34 | ...or if you're _incredibly_ lazy, you can just highlight everything: 35 | 36 | ```js 37 | highlight.all(); 38 | ``` 39 | 40 | ## Languages 41 | 42 | - [Bash](https://github.com/segmentio/highlight-bash) 43 | - [CSS](https://github.com/segmentio/highlight-css) 44 | - [C#](https://github.com/segmentio/highlight-csharp) 45 | - [Go](https://github.com/segmentio/highlight-go) 46 | - [HTML](https://github.com/segmentio/highlight-xml) 47 | - [Java](https://github.com/segmentio/highlight-java) 48 | - [Javascript](https://github.com/segmentio/highlight-javascript) 49 | - [JSON](https://github.com/segmentio/highlight-json) 50 | - [.NET](https://github.com/segmentio/highlight-csharp) 51 | - [Objective-C](https://github.com/segmentio/highlight-objective-c) 52 | - [PHP](https://github.com/segmentio/highlight-php) 53 | - [Python](https://github.com/segmentio/highlight-python) 54 | - [Ruby](https://github.com/segmentio/highlight-ruby) 55 | - [SQL](https://github.com/segmentio/highlight-sql) 56 | - [XML](https://github.com/segmentio/highlight-xml) 57 | - [YAML](https://github.com/segmentio/highlight-yaml) 58 | 59 | ## API 60 | 61 | #### new Highlight() 62 | 63 | Create a new `Highlight` instance. 64 | 65 | #### #use(plugin) 66 | 67 | Apply a `plugin` function, for example language syntaxes. 68 | 69 | #### #string(string, language) 70 | 71 | Highlight a `string` of code of a given `language`. 72 | 73 | #### #element(el, [language]) 74 | 75 | Highlight an `el`. If you don't pass a `language`, it will use the `data-language` attribute: 76 | 77 | ```html 78 |
YOUR CODE HERE
79 | ``` 80 | 81 | #### #elements(els, [language]) 82 | 83 | Highlight a series of `els`. 84 | 85 | #### #all() 86 | 87 | Highlight all of the elements in the DOM that have a `data-language` attribute. 88 | 89 | #### #prefix(string) 90 | 91 | Set the CSS class name prefix `string`. 92 | 93 | #### #language(name, grammar) 94 | 95 | Define a new language by `name` with a `grammar`. 96 | 97 | #### #parse(string, language) 98 | 99 | Return an AST for a given `string` and `language`. 100 | 101 | #### #stringify(ast) 102 | 103 | Convert an `AST` into a string of HTML. 104 | 105 | ## License 106 | 107 | The MIT License 108 | 109 | Copyright © 2014 Segment.io 110 | 111 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 112 | 113 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 114 | 115 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 116 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "highlight", 3 | "version": "0.0.3", 4 | "repo": "segmentio/highlight", 5 | "description": "A simple, pluggable API for syntax highlighting.", 6 | "keywords": [], 7 | "license": "MIT", 8 | "main": "lib/index.js", 9 | "scripts": [ 10 | "lib/index.js" 11 | ], 12 | "dependencies": { 13 | "component/escape-html": "*" 14 | }, 15 | "development": { 16 | "component/assert": "*", 17 | "component/domify": "*" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 2 | var escape = require('escape-html'); 3 | 4 | /** 5 | * Expose `Highlight`. 6 | */ 7 | 8 | module.exports = Highlight; 9 | 10 | /** 11 | * Initialize a new `Highlight` instance. 12 | */ 13 | 14 | function Highlight(){ 15 | if (!(this instanceof Highlight)) return new Highlight(); 16 | this.languages = {}; 17 | this.prefix('Highlight-'); 18 | } 19 | 20 | /** 21 | * Use a `plugin` function. 22 | * 23 | * @param {Function} plugin 24 | * @return {Highlight} 25 | */ 26 | 27 | Highlight.prototype.use = function(plugin){ 28 | plugin(this); 29 | return this; 30 | }; 31 | 32 | /** 33 | * Get or set the highlighted class `prefix`. 34 | * 35 | * @param {String} prefix 36 | * @return {Highlight or String} 37 | */ 38 | 39 | Highlight.prototype.prefix = function(prefix){ 40 | if (!arguments.length) return this._prefix; 41 | this._prefix = prefix; 42 | return this; 43 | } 44 | 45 | /** 46 | * Define a new `language` with a `grammar`. 47 | * 48 | * @param {String} language 49 | * @param {Object} grammar 50 | * @return {Highlight} 51 | */ 52 | 53 | Highlight.prototype.language = function(language, grammar){ 54 | this.languages[language] = grammar; 55 | return this; 56 | }; 57 | 58 | /** 59 | * Highlight an HTML `string` of a given `language`. 60 | * 61 | * @param {String} string 62 | * @param {String} language 63 | * @return {String} 64 | */ 65 | 66 | Highlight.prototype.string = function(string, language){ 67 | var ast = this.parse(string, language); 68 | var str = this.stringify(ast); 69 | return str; 70 | }; 71 | 72 | /** 73 | * Highlight an `el`, with optional `language`. 74 | * 75 | * @param {Element or String} el 76 | * @param {String} language (optional) 77 | */ 78 | 79 | Highlight.prototype.element = function(el, language){ 80 | if ('string' == typeof el) el = document.querySelector(el); 81 | var str = this.string(el.textContent, language || lang(el)); 82 | el.innerHTML = str; 83 | }; 84 | 85 | /** 86 | * Highlight an array of `els`, with optional `language`. 87 | * 88 | * @param {Array or String} els 89 | * @param {String} language (optional) 90 | */ 91 | 92 | Highlight.prototype.elements = function(els, language){ 93 | if ('string' == typeof els) els = document.querySelectorAll(els); 94 | for (var i = 0, el; el = els[i]; i++) this.element(el, language); 95 | }; 96 | 97 | /** 98 | * Highlight all elements in the DOM with language attributes. 99 | */ 100 | 101 | Highlight.prototype.all = function(){ 102 | this.elements('[data-language]'); 103 | this.elements('[class*="language-"]'); 104 | this.elements('[class*="lang-"]'); 105 | }; 106 | 107 | /** 108 | * Parse a `string` with a given language's `grammar`, returning an AST. 109 | * 110 | * @param {String} string 111 | * @param {String or Object} grammar 112 | * @return {Array} 113 | */ 114 | 115 | Highlight.prototype.parse = function(string, grammar){ 116 | if ('string' == typeof grammar) { 117 | var lang = grammar; 118 | grammar = this.languages[lang]; 119 | if (!grammar) throw new Error('unknown language "' + lang + '"'); 120 | } 121 | 122 | if (!grammar) throw new Error('must provide a grammar'); 123 | if (!string) return []; 124 | var ret = [string]; 125 | 126 | for (var key in grammar) { 127 | if (!grammar.hasOwnProperty(key)) continue; 128 | 129 | var rule = grammar[key]; 130 | var regexp = rule.pattern || rule; 131 | 132 | for (var i = 0; i < ret.length; i++) { 133 | var str = ret[i]; 134 | if ('object' == typeof str) continue; 135 | var m = regexp.exec(str); 136 | if (!m) continue; 137 | 138 | var contents = m[0]; 139 | var before = str.slice(0, m.index); 140 | var after = str.slice(m.index + contents.length); 141 | var args = [i, 1]; 142 | var token = { 143 | type: key, 144 | value: rule.children ? this.parse(contents, rule.children) : contents 145 | }; 146 | 147 | if (before) args.push(before); 148 | args.push(token); 149 | if (after) args.push(after); 150 | ret.splice.apply(ret, args); 151 | } 152 | } 153 | 154 | return ret; 155 | } 156 | 157 | /** 158 | * Stringify a given `ast`. 159 | * 160 | * @param {Array} ast 161 | * @return {String} 162 | */ 163 | 164 | Highlight.prototype.stringify = function(ast){ 165 | var prefix = this.prefix(); 166 | var self = this; 167 | 168 | return ast.map(function(t){ 169 | if ('string' == typeof t) return escape(t); 170 | var type = t.type; 171 | var value = 'object' == typeof t.value 172 | ? self.stringify(t.value) 173 | : escape(t.value); 174 | return '' + value + ''; 175 | }).join(''); 176 | }; 177 | 178 | /** 179 | * Language class matcher. 180 | */ 181 | 182 | var matcher = /\blang(?:uage)?-([\w-.]+)\b/i; 183 | 184 | /** 185 | * Get the code language for a given `el`. First look for a `data-language` 186 | * attribute, then a `language-*` class, then search up the DOM tree for them. 187 | * 188 | * @param {Element} el 189 | * @return {String} 190 | */ 191 | 192 | function lang(el){ 193 | if (!el) return; 194 | var m; 195 | if (el.hasAttribute('data-language')) return el.getAttribute('data-language'); 196 | if (m = matcher.exec(el.className)) return m[1]; 197 | return language(el.parentNode); 198 | } 199 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "syntax-highlighter", 3 | "version": "0.0.3", 4 | "repository": "segmentio/highlight", 5 | "description": "A simple, pluggable API for syntax highlighting.", 6 | "license": "MIT", 7 | "main": "lib/index.js", 8 | "dependencies": { 9 | "escape-html": "^1.0.3" 10 | }, 11 | "devDependencies": { 12 | "component/assert": "*", 13 | "component/domify": "*" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | var assert = require('assert'); 3 | var domify = require('domify'); 4 | var Highlight = require('highlight'); 5 | 6 | describe('highlight', function(){ 7 | it('should expose a constructor', function(){ 8 | assert.equal('function', typeof Highlight); 9 | }); 10 | 11 | it('should not require the new keyword', function(){ 12 | var h = Highlight(); 13 | assert(h instanceof Highlight); 14 | }); 15 | 16 | it('should create a languages dictionary', function(){ 17 | var h = Highlight(); 18 | assert.deepEqual(h.languages, {}); 19 | }); 20 | 21 | describe('#use', function(){ 22 | it('should use a plugin function', function(done){ 23 | var h = Highlight(); 24 | h.use(plugin); 25 | 26 | function plugin(instance){ 27 | assert.equal(h, instance); 28 | done(); 29 | } 30 | }); 31 | }); 32 | 33 | describe('#prefix', function(){ 34 | it('should set a class name prefix', function(){ 35 | var h = Highlight(); 36 | h.prefix('prefix'); 37 | assert.equal(h._prefix, 'prefix'); 38 | }); 39 | }); 40 | 41 | describe('#language', function(){ 42 | it('should define a new language with a grammar', function(){ 43 | var h = Highlight(); 44 | var grammar = {}; 45 | assert.equal(h, h.language('css', grammar)); 46 | assert.equal(h.languages.css, grammar); 47 | }); 48 | }); 49 | 50 | describe('#parse', function(){ 51 | it('should parse a basic grammar', function(){ 52 | var lang = 'handlebars'; 53 | var grammar = { interpolation: /(\{\{\s*\w+\s*\}\})/ }; 54 | var h = Highlight().language(lang, grammar); 55 | var str = 'an {{ interpolation }} in a {{ size }} string'; 56 | var ast = h.parse(str, lang); 57 | assert.deepEqual(ast, [ 58 | 'an ', 59 | { 60 | type: 'interpolation', 61 | value: '{{ interpolation }}' 62 | }, 63 | ' in a ', 64 | { 65 | type: 'interpolation', 66 | value: '{{ size }}' 67 | }, 68 | ' string' 69 | ]); 70 | }); 71 | 72 | it('should parse a nested grammar', function(){ 73 | var lang = 'handlebars'; 74 | var grammar = { 75 | block: { 76 | pattern: /(\{\{#\s*\w+\s*\}\}.*?\{\{\/\s*\w+\s*\}\})/, 77 | children: { 78 | open: /(\{\{#\s*\w+\s*\}\})/, 79 | close: /(\{\{\/\s*\w+\s*\}\})/ 80 | } 81 | } 82 | }; 83 | var h = Highlight().language(lang, grammar); 84 | var str = 'a {{#block}} in a {{/block}} string'; 85 | var ast = h.parse(str, lang); 86 | assert.deepEqual(ast, [ 87 | 'a ', 88 | { 89 | type: 'block', 90 | value: [{ 91 | type: 'open', 92 | value: '{{#block}}', 93 | }, 94 | ' in a ', 95 | { 96 | type: 'close', 97 | value: '{{/block}}' 98 | }] 99 | }, 100 | ' string' 101 | ]); 102 | }); 103 | }); 104 | 105 | describe('#stringify', function(){ 106 | it('should stringify an ast', function(){ 107 | var h = Highlight().use(fixture); 108 | var code = h.stringify([ 109 | 'an ', 110 | { 111 | type: 'interpolation', 112 | value: '{{interpolation}}' 113 | }, 114 | ' and a ', 115 | { 116 | type: 'block', 117 | value: [{ 118 | type: 'open', 119 | value: '{{#block}}', 120 | }, 121 | ' in a ', 122 | { 123 | type: 'close', 124 | value: '{{/block}}' 125 | }] 126 | }, 127 | ' string' 128 | ]); 129 | 130 | assert.equal(code, '' 131 | + 'an ' 132 | + '{{interpolation}}' 133 | + ' and a ' 134 | + '' 135 | + '{{#block}}' 136 | + ' in a ' 137 | + '{{/block}}' 138 | + '' 139 | + ' string'); 140 | }); 141 | 142 | it('should escape the output', function(){ 143 | var h = Highlight(); 144 | var code = h.stringify([ 145 | '<', 146 | { 147 | type: 'tag', 148 | value: 'script' 149 | }, 150 | '>' 151 | ]); 152 | assert.equal(code, '<script>'); 153 | }); 154 | }); 155 | 156 | describe('#string', function(){ 157 | it('should highlight a string of a given language', function(){ 158 | var h = Highlight().use(fixture); 159 | var code = h.string('an {{ interpolated }} string', 'fixture'); 160 | assert.equal(code, '' 161 | + 'an ' 162 | + '{{ interpolated }}' 163 | + ' string'); 164 | }); 165 | }); 166 | 167 | describe('#element', function(){ 168 | it('should highlight the text content of an element', function(){ 169 | var h = Highlight().use(fixture); 170 | var el = domify('
an {{ interpolated }} string
'); 171 | h.element(el); 172 | assert.equal(el.innerHTML, '' 173 | + 'an ' 174 | + '{{ interpolated }}' 175 | + ' string'); 176 | }); 177 | 178 | it('should guess the language from a class', function(){ 179 | var h = Highlight().use(fixture); 180 | var el = domify('
an {{ interpolated }} string
'); 181 | h.element(el); 182 | assert.equal(el.innerHTML, '' 183 | + 'an ' 184 | + '{{ interpolated }}' 185 | + ' string'); 186 | }); 187 | 188 | it('should support language names with non alphabetic characters', function(){ 189 | var h = Highlight().use(fixture); 190 | var el = domify('
an {{ interpolated }} string
'); 191 | h.element(el); 192 | assert.equal(el.innerHTML, '' 193 | + 'an ' 194 | + '{{ interpolated }}' 195 | + ' string'); 196 | }); 197 | 198 | it('should support language names with non alphabetic characters', function(){ 199 | var h = Highlight().use(fixture); 200 | var el = domify('
an {{ interpolated }} string
'); 201 | h.element(el); 202 | assert.equal(el.innerHTML, '' 203 | + 'an ' 204 | + '{{ interpolated }}' 205 | + ' string'); 206 | }); 207 | 208 | it('should use a passed in languge', function(){ 209 | var h = Highlight().use(fixture); 210 | var el = domify('
an {{ interpolated }} string
'); 211 | h.element(el, 'fixture'); 212 | assert.equal(el.innerHTML, '' 213 | + 'an ' 214 | + '{{ interpolated }}' 215 | + ' string'); 216 | }); 217 | }); 218 | 219 | describe('#elements', function(){ 220 | it('should highlight the text content of multiple elements', function(){ 221 | var h = Highlight().use(fixture); 222 | var el = domify('
' 223 | + 'a {{#block}} in a {{/block}} string' 224 | + 'an {{ interpolated }} string' 225 | + '
'); 226 | 227 | h.elements(el.querySelectorAll('code')); 228 | assert.equal(el.innerHTML, '' 229 | + '' 230 | + 'a ' 231 | + '' 232 | + '{{#block}}' 233 | + ' in a ' 234 | + '{{/block}}' 235 | + '' 236 | + ' string' 237 | + '' 238 | + '' 239 | + 'an ' 240 | + '{{ interpolated }}' 241 | + ' string' 242 | + ''); 243 | }); 244 | }); 245 | }); 246 | 247 | /** 248 | * Plugin fixture. 249 | * 250 | * @param {Highlight} highlight 251 | */ 252 | 253 | function fixture(highlight){ 254 | var obj = {}; 255 | highlight.language('objective-c', obj); 256 | highlight.language('fixture', obj); 257 | highlight.language('.net', obj); 258 | obj.interpolation = /(\{\{\s*\w+\s*\}\})/; 259 | obj.block = { 260 | pattern: /(\{\{#\s*\w+\s*\}\}.*?\{\{\/\s*\w+\s*\}\})/, 261 | children: { 262 | open: /(\{\{#\s*\w+\s*\}\})/, 263 | close: /(\{\{\/\s*\w+\s*\}\})/ 264 | } 265 | }; 266 | } --------------------------------------------------------------------------------