[d24f17c] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | var high = require('highlight.js/lib/core')
|
---|
| 4 | var fault = require('fault')
|
---|
| 5 |
|
---|
| 6 | exports.highlight = highlight
|
---|
| 7 | exports.highlightAuto = highlightAuto
|
---|
| 8 | exports.registerLanguage = registerLanguage
|
---|
| 9 | exports.listLanguages = listLanguages
|
---|
| 10 | exports.registerAlias = registerAlias
|
---|
| 11 |
|
---|
| 12 | Emitter.prototype.addText = text
|
---|
| 13 | Emitter.prototype.addKeyword = addKeyword
|
---|
| 14 | Emitter.prototype.addSublanguage = addSublanguage
|
---|
| 15 | Emitter.prototype.openNode = open
|
---|
| 16 | Emitter.prototype.closeNode = close
|
---|
| 17 | Emitter.prototype.closeAllNodes = noop
|
---|
| 18 | Emitter.prototype.finalize = noop
|
---|
| 19 | Emitter.prototype.toHTML = toHtmlNoop
|
---|
| 20 |
|
---|
| 21 | var defaultPrefix = 'hljs-'
|
---|
| 22 |
|
---|
| 23 | // Highlighting `value` in the language `name`.
|
---|
| 24 | function highlight(name, value, options) {
|
---|
| 25 | var before = high.configure({})
|
---|
| 26 | var settings = options || {}
|
---|
| 27 | var prefix = settings.prefix
|
---|
| 28 | var result
|
---|
| 29 |
|
---|
| 30 | if (typeof name !== 'string') {
|
---|
| 31 | throw fault('Expected `string` for name, got `%s`', name)
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | if (!high.getLanguage(name)) {
|
---|
| 35 | throw fault('Unknown language: `%s` is not registered', name)
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | if (typeof value !== 'string') {
|
---|
| 39 | throw fault('Expected `string` for value, got `%s`', value)
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | if (prefix === null || prefix === undefined) {
|
---|
| 43 | prefix = defaultPrefix
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | high.configure({__emitter: Emitter, classPrefix: prefix})
|
---|
| 47 |
|
---|
| 48 | result = high.highlight(value, {language: name, ignoreIllegals: true})
|
---|
| 49 |
|
---|
| 50 | high.configure(before || {})
|
---|
| 51 |
|
---|
| 52 | /* istanbul ignore if - Highlight.js seems to use this (currently) for broken
|
---|
| 53 | * grammars, so let’s keep it in there just to be sure. */
|
---|
| 54 | if (result.errorRaised) {
|
---|
| 55 | throw result.errorRaised
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return {
|
---|
| 59 | relevance: result.relevance,
|
---|
| 60 | language: result.language,
|
---|
| 61 | value: result.emitter.rootNode.children
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | function highlightAuto(value, options) {
|
---|
| 66 | var settings = options || {}
|
---|
| 67 | var subset = settings.subset || high.listLanguages()
|
---|
| 68 | var prefix = settings.prefix
|
---|
| 69 | var length = subset.length
|
---|
| 70 | var index = -1
|
---|
| 71 | var result
|
---|
| 72 | var secondBest
|
---|
| 73 | var current
|
---|
| 74 | var name
|
---|
| 75 |
|
---|
| 76 | if (prefix === null || prefix === undefined) {
|
---|
| 77 | prefix = defaultPrefix
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | if (typeof value !== 'string') {
|
---|
| 81 | throw fault('Expected `string` for value, got `%s`', value)
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | secondBest = {relevance: 0, language: null, value: []}
|
---|
| 85 | result = {relevance: 0, language: null, value: []}
|
---|
| 86 |
|
---|
| 87 | while (++index < length) {
|
---|
| 88 | name = subset[index]
|
---|
| 89 |
|
---|
| 90 | if (!high.getLanguage(name)) {
|
---|
| 91 | continue
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | current = highlight(name, value, options)
|
---|
| 95 | current.language = name
|
---|
| 96 |
|
---|
| 97 | if (current.relevance > secondBest.relevance) {
|
---|
| 98 | secondBest = current
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | if (current.relevance > result.relevance) {
|
---|
| 102 | secondBest = result
|
---|
| 103 | result = current
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (secondBest.language) {
|
---|
| 108 | result.secondBest = secondBest
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | return result
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // Register a language.
|
---|
| 115 | function registerLanguage(name, syntax) {
|
---|
| 116 | high.registerLanguage(name, syntax)
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | // Get a list of all registered languages.
|
---|
| 120 | function listLanguages() {
|
---|
| 121 | return high.listLanguages()
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // Register more aliases for an already registered language.
|
---|
| 125 | function registerAlias(name, alias) {
|
---|
| 126 | var map = name
|
---|
| 127 | var key
|
---|
| 128 |
|
---|
| 129 | if (alias) {
|
---|
| 130 | map = {}
|
---|
| 131 | map[name] = alias
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | for (key in map) {
|
---|
| 135 | high.registerAliases(map[key], {languageName: key})
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | function Emitter(options) {
|
---|
| 140 | this.options = options
|
---|
| 141 | this.rootNode = {children: []}
|
---|
| 142 | this.stack = [this.rootNode]
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | function addKeyword(value, name) {
|
---|
| 146 | this.openNode(name)
|
---|
| 147 | this.addText(value)
|
---|
| 148 | this.closeNode()
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | function addSublanguage(other, name) {
|
---|
| 152 | var stack = this.stack
|
---|
| 153 | var current = stack[stack.length - 1]
|
---|
| 154 | var results = other.rootNode.children
|
---|
| 155 | var node = name
|
---|
| 156 | ? {
|
---|
| 157 | type: 'element',
|
---|
| 158 | tagName: 'span',
|
---|
| 159 | properties: {className: [name]},
|
---|
| 160 | children: results
|
---|
| 161 | }
|
---|
| 162 | : results
|
---|
| 163 |
|
---|
| 164 | current.children = current.children.concat(node)
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | function text(value) {
|
---|
| 168 | var stack = this.stack
|
---|
| 169 | var current
|
---|
| 170 | var tail
|
---|
| 171 |
|
---|
| 172 | if (value === '') return
|
---|
| 173 |
|
---|
| 174 | current = stack[stack.length - 1]
|
---|
| 175 | tail = current.children[current.children.length - 1]
|
---|
| 176 |
|
---|
| 177 | if (tail && tail.type === 'text') {
|
---|
| 178 | tail.value += value
|
---|
| 179 | } else {
|
---|
| 180 | current.children.push({type: 'text', value: value})
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | function open(name) {
|
---|
| 185 | var stack = this.stack
|
---|
| 186 | var className = this.options.classPrefix + name
|
---|
| 187 | var current = stack[stack.length - 1]
|
---|
| 188 | var child = {
|
---|
| 189 | type: 'element',
|
---|
| 190 | tagName: 'span',
|
---|
| 191 | properties: {className: [className]},
|
---|
| 192 | children: []
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | current.children.push(child)
|
---|
| 196 | stack.push(child)
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | function close() {
|
---|
| 200 | this.stack.pop()
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | function toHtmlNoop() {
|
---|
| 204 | return ''
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | function noop() {}
|
---|