Skip to content

Instantly share code, notes, and snippets.

@dignifiedquire
Created July 28, 2015 13:34
Show Gist options
  • Save dignifiedquire/f8ac257da72d58d56bac to your computer and use it in GitHub Desktop.
Save dignifiedquire/f8ac257da72d58d56bac to your computer and use it in GitHub Desktop.
❯ babel-node parser.js
The CPU consists of integrated circuits in the microproces>sor of the computer. The CPU is responsible for processing information and controlling the computer.
import tokenizer from 'string-tokenizer'
import chalk from 'chalk'
function tokenize (text) {
return tokenizer()
.input(text)
.token('blank', /<<<.+?(?=>>>)/, values => values[0].replace(/^<<</, ''))
.token('text', /.+?(?=<<<|$)/, values => values[0].replace(/^>>>/, ''))
.resolve()
}
function formatBlank (blank) {
return chalk.red.bold(blank)
}
function format ({text, blank}) {
let result = ''
const length = Math.max(text.length, blank.length)
for (let i = 0; i < length; i++) {
result += text[i] || ''
result += formatBlank(blank[i] || '')
}
return result
}
function run (text) {
const tokens = tokenize(text)
console.log(format(tokens))
}
run('The CPU consists of integrated circuits in the <<<microproces>sor>>> of the computer. The CPU is responsible for <<<processing>>> information and controlling the computer.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment