Skip to content

Commit

Permalink
NEW: Support literal word translations (#41)
Browse files Browse the repository at this point in the history
closes #41
  • Loading branch information
dwhieb committed Jan 2, 2024
1 parent 03e33a0 commit 67fbd69
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/words/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import createLiteral from './literal.js'
import createTranscription from './transcription.js'

export default function createWords(words, options) {
Expand All @@ -8,10 +9,12 @@ export default function createWords(words, options) {

for (const word of words) {

const literal = createLiteral(word.literal, options)
const transcription = createTranscription(word.transcription, options)

html += `\n<li class=word>
${ transcription }
${ literal }
</li>`

}
Expand Down
19 changes: 19 additions & 0 deletions src/words/literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function createLiteral(data, { analysisLang }) {

if (!data) return ``

if (typeof data === `string`) {
const lang = analysisLang ? `lang='${ analysisLang }'` : ``
return `<span class=w-lit ${ lang }>${ data }</span>`
}

let html = ``

for (const lang in data) {
const tln = data[lang]
html += `<span class=w-lit lang='${ lang }'>${ tln }</span>`
}

return html

}
43 changes: 42 additions & 1 deletion test/words.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import findElementByClass from './utilities/findElementByClass.js'
import { getTextContent } from '../node_modules/@web/parse5-utils/src/index.js'
import parse from './utilities/convertAndParse.js'

import {
findElement,
getTagName,
} from '@web/parse5-utils'

describe(`words`, function() {

it(`renders one HTML word per linguistic word`, async function() {
Expand Down Expand Up @@ -46,7 +51,7 @@ describe(`words`, function() {
\\tln one day a man
`

const { dom, html } = await parse(scription)
const { dom } = await parse(scription)
const wordsContainer = findElementByClass(dom, `words`)
const [firstWord, secondWord] = wordsContainer.childNodes.filter(node => node.tagName === `li`)

Expand All @@ -73,4 +78,40 @@ describe(`words`, function() {
expect(getTextContent(b)).to.equal(`waxdungu`)

})

it(`word literal translation (single language)`, async function() {

const scription = `
\\w waxdungu qasi
\\wlt one.day a.man
`

const { dom } = await parse(scription)
const wordsContainer = findElementByClass(dom, `words`)
const [firstWord, secondWord] = wordsContainer.childNodes.filter(node => node.tagName === `li`)

expect(getTextContent(firstWord)).to.include(`one.day`)
expect(getTextContent(secondWord)).to.include(`a.man`)

})

it(`word literal translation (single language)`, async function() {

const scription = `
\\w waxdungu qasi
\\wlt-en one.day a.man
\\wlt-sp un.día un.hombre
`

const { dom } = await parse(scription)
const wordsContainer = findElementByClass(dom, `words`)
const [firstWord, secondWord] = wordsContainer.childNodes.filter(node => node.tagName === `li`)

expect(getTextContent(firstWord)).to.include(`one.day`)
expect(getTextContent(firstWord)).to.include(`un.día`)
expect(getTextContent(secondWord)).to.include(`a.man`)
expect(getTextContent(secondWord)).to.include(`un.hombre`)

})

})

0 comments on commit 67fbd69

Please sign in to comment.