mw-dict
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -6,18 +6,32 @@ const micro = require('micro') | ||
const API_KEY = require('./config') | ||
const dict = new CollegiateDictionary(key = API_KEY) | ||
const dict = new CollegiateDictionary((key = API_KEY)) | ||
function renderDefinition(senses) { | ||
return senses.map(({ number, meanings, synonyms, illustrations, senses }) => ` | ||
return senses | ||
.map( | ||
({ number, meanings, synonyms, illustrations, senses }) => ` | ||
<li> | ||
${number ? ('<strong>' + number + '</strong>') : ''} | ||
${number ? '<strong>' + number + '</strong>' : ''} | ||
${meanings && meanings.length ? meanings.join(' ') : ''} | ||
${synonyms && synonyms.length ? synonyms.map(s => '<a>' + s + '</a>').join(', ') : ''} | ||
${illustrations && illustrations.length ? ('<ul>' + illustrations.map(i => '<li>' + i + '</li>').join('\n') + '</ul>') : ''} | ||
${senses && senses.length ? ('<ol>' + renderDefinition(senses) + '</ol>') : ''} | ||
${synonyms && synonyms.length | ||
? synonyms.map(s => '<a>' + s + '</a>').join(', ') | ||
: ''} | ||
${illustrations && illustrations.length | ||
? '<ul>' + | ||
illustrations.map(i => '<li>' + i + '</li>').join('\n') + | ||
'</ul>' | ||
: ''} | ||
${senses && senses.length | ||
? '<ol>' + renderDefinition(senses) + '</ol>' | ||
: ''} | ||
</li> | ||
`).join('\n') | ||
` | ||
) | ||
.join('\n') | ||
} | ||
function render(definitions) { | ||
return definitions.map(({ word, functional_label, definition }) => ` | ||
return definitions | ||
.map( | ||
({ word, functional_label, definition }) => ` | ||
<h2>${word}</h2> | ||
@@ -28,17 +42,19 @@ <p><i>${functional_label}</i><p> | ||
</ol> | ||
`).join('\n') | ||
` | ||
) | ||
.join('\n') | ||
} | ||
const server = micro(async (req, res) => { | ||
let matches = req.url.match(/\/word\/(.*)/) | ||
if (matches) { | ||
let word = matches[1] | ||
const definitions = await dict.lookup(word) | ||
res.setHeader('Content-Type', 'text/html') | ||
let extra = `<style>body {font-family: Garamond Premier Pro; font-size: 1.3rem; line-height: 1.35; padding: 2rem;} a {color: blue; text-decoration: none;} ol, ul {list-style: none;} li + li, li > ul {margin-top: .8rem;} ul li:before{content: '•'; color:#5690b1; margin-right: .5rem;}</style>` | ||
return extra + render(definitions) | ||
} | ||
return '' | ||
let matches = req.url.match(/\/word\/(.*)/) | ||
if (matches) { | ||
let word = matches[1] | ||
const definitions = await dict.lookup(word) | ||
res.setHeader('Content-Type', 'text/html') | ||
let extra = `<style>body {font-family: Garamond Premier Pro; font-size: 1.3rem; line-height: 1.35; padding: 2rem;} a {color: blue; text-decoration: none;} ol, ul {list-style: none;} li + li, li > ul {margin-top: .8rem;} ul li:before{content: '•'; color:#5690b1; margin-right: .5rem;}</style>` | ||
return extra + render(definitions) | ||
} | ||
return '' | ||
}) | ||
server.listen(3000) | ||
server.listen(3000) |
@@ -87,7 +87,27 @@ /* | ||
function getBulletType(bullet) { | ||
if (isNumeric(bullet)) { | ||
return 0 | ||
} | ||
if (bullet.match(/^[a-z]$/)) { | ||
return 1 | ||
} | ||
if (bullet.startsWith('(')) { | ||
return 2 | ||
} | ||
} | ||
function pop(stack) { | ||
// empty stack & create a new list | ||
// only pop out the same type of number | ||
let list = [] | ||
let type | ||
while (stack.length) { | ||
list.push(stack.pop()) | ||
let s = stack.pop() | ||
type = type || getBulletType(s.number) | ||
if (getBulletType(s.number) === type) { | ||
list.push(s) | ||
} else { | ||
stack.push(s) | ||
break | ||
} | ||
} | ||
@@ -113,25 +133,22 @@ return list | ||
} | ||
matches = number.match(/([a-z])\s(\(\d+\))/) | ||
if (matches) { | ||
// 'a (1)' | ||
sense.number = matches[2] | ||
stack.push(sense) | ||
let s = new Sense({ | ||
number: matches[1], | ||
senses: pop(stack) | ||
}) | ||
stack.push(s) | ||
let bullets = number.split(' ') | ||
if (bullets.length === 1) { | ||
continue | ||
} | ||
matches = number.match(/(\d+)\s([a-z]+)/) | ||
if (matches) { | ||
// '1 a' | ||
sense.number = matches[2] | ||
stack.push(sense) | ||
let s = new Sense({ | ||
number: matches[1], | ||
transitivity: sense.transitivity, | ||
// '1 a' or 'a (1)' or '1 a (1)' | ||
for (let l = bullets.length, j = l - 1; j >= 0; j--) { | ||
if (j === l - 1) { | ||
sense.number = bullets[j] | ||
stack.push(sense) | ||
continue | ||
} | ||
sense = new Sense({ | ||
number: bullets[j], | ||
senses: pop(stack) | ||
}) | ||
delete sense.transitivity | ||
senses[i] = s | ||
if (getBulletType(sense.number)) { | ||
stack.push(sense) | ||
} else { | ||
senses[i] = sense | ||
} | ||
} | ||
@@ -138,0 +155,0 @@ } |
{ | ||
"name": "mw-dict", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
94480
441