Skip to content

Commit

Permalink
NEW: option: targetLang
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhieb committed Dec 23, 2023
1 parent 08203f1 commit a8399e6
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/lines/literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import addEmphasis from '../utilities/addEmphasis.js'

export default function createLiteral(data, { analysisLang }) {

if (!data) return ``

if (typeof data === `string`) {
const lang = analysisLang ? `lang='${ analysisLang }'` : ``
return `<p class=lit ${ lang }>${ addEmphasis(data) }</p>`
Expand Down
6 changes: 4 additions & 2 deletions src/lines/phonetic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import addEmphasis from '../utilities/addEmphasis.js'

export default function createPhonetic(phonetic) {
export default function createPhonetic(phonetic, { targetLang }) {
if (!phonetic) return ``
return `<p class=phon>${ addEmphasis(phonetic) }</p>`
const lang = targetLang ? `lang='${ targetLang }-fonipa'` : ``
// NB: Don't add phonetic brackets. These can be added with CSS.
return `<p class=phon ${ lang }>${ addEmphasis(phonetic) }</p>`
}
2 changes: 2 additions & 0 deletions src/lines/source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default function createSource(speaker, source) {

if (!(speaker ?? source)) return ``

speaker ||= ``
source = source ? `(${ source })` : ``

Expand Down
7 changes: 5 additions & 2 deletions src/lines/transcript.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import addEmphasis from '../utilities/addEmphasis.js'

export default function createTranscript(data) {
export default function createTranscript(data, { targetLang }) {

if (!data) return ``

let html = ``

for (const ortho in data) {
const transcript = data[ortho]
html += `<p class=trs data-ortho='${ ortho }'>${ addEmphasis(transcript) }</p>`
const lang = targetLang ? `lang='${ targetLang }'` : ``
html += `<p class=trs data-ortho='${ ortho }' ${ lang }>${ addEmphasis(transcript) }</p>`
}

return html
Expand Down
9 changes: 6 additions & 3 deletions src/lines/transcription.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import addEmphasis from '../utilities/addEmphasis.js'

export default function createTranscription(data) {
export default function createTranscription(data, { targetLang }) {

if (!data) return ``

let html = ``

for (const ortho in data) {
const txn = data[ortho]
html += `<p class=txn data-ortho='${ ortho }'>${ addEmphasis(txn) }</p>`
const txn = data[ortho]
const lang = targetLang ? `lang='${ targetLang }'` : ``
html += `<p class=txn data-ortho='${ ortho }' ${ lang }>${ addEmphasis(txn) }</p>`
}

return html
Expand Down
2 changes: 2 additions & 0 deletions src/lines/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import addEmphasis from '../utilities/addEmphasis.js'

export default function createTranslation(data, { analysisLang }) {

if (!data) return ``

if (typeof data === `string`) {
const lang = analysisLang ? `lang='${ analysisLang }'` : ``
return `<p class=tln ${ lang }>${ addEmphasis(data) }</p>`
Expand Down
16 changes: 12 additions & 4 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
export default function validateOptions(opts) {

if (`analysisLang` in opts) {
if (typeof opts.analysisLang !== `string`) throw new TypeError(`The 'analysisLang' option must be a String.`)
if (`analysisLang` in opts && typeof opts.analysisLang !== `string`) {
throw new TypeError(`The 'analysisLang' option must be a string.`)
}

if (`classes` in opts) {
if (!Array.isArray(opts.classes)) throw new TypeError(`The 'classes' option must be an array.`)
if (!opts.classes.every(cls => typeof cls === `string`)) throw new TypeError(`Each of the values of the 'classes' array must be a string.`)
}

if (`scription` in opts && typeof opts.scription !== `object`) throw new TypeError(`The 'scription' option must be an object.`)
if (`scription` in opts && typeof opts.scription !== `object`) {
throw new TypeError(`The 'scription' option must be an object.`)
}

if (`tag` in opts && typeof opts.tag !== `string`) {
throw new TypeError(`The 'tag' option must be a string.`)
}

if (`tag` in opts && typeof opts.tag !== `string`) throw new TypeError(`The 'tag' option must be a string.`)
if (`targetLang` in opts && typeof opts.targetLang !== `string`) {
throw new TypeError(`The 'targetLang' option must be a string.`)
}

}
6 changes: 3 additions & 3 deletions src/utterance.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default function convertUtterance(u, options) {
const classString = classes.join(` `)
const header = createHeader(u.metadata, options)
const literal = createLiteral(u.literal, options)
const phonetic = createPhonetic(u.phonetic)
const phonetic = createPhonetic(u.phonetic, options)
const source = createSource(u.speaker, u.source)
const timespan = createTimespan(u.startTime, u.endTime)
const transcript = createTranscript(u.transcript)
const transcription = createTranscription(u.transcription)
const transcript = createTranscript(u.transcript, options)
const transcription = createTranscription(u.transcription, options)
const translation = createTranslation(u.translation, options)

return `<${ tag } class='${ classString }'>
Expand Down
37 changes: 34 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import convert from '../src/index.js'
import { expect } from 'chai'
import findElementByClass from './utilities/findElementByClass.js'
import { getAttribute } from '@web/parse5-utils'
import { getTextContent } from '../node_modules/@web/parse5-utils/src/index.js'
import parse from './utilities/convertAndParse.js'
import parseClassString from './utilities/parseClassString.js'
import { Swahili } from '../samples/data/data.js'


describe(`scription2html`, function() {

it(`no input → empty string`, function() {
Expand Down Expand Up @@ -163,8 +161,41 @@ describe(`scription2html`, function() {
})

it(`option: tag (validates)`, function() {
const test = () => convert(Swahili, { tag: 0 })
const test = () => parse(Swahili, { tag: 0 })
expect(test).to.throw(`tag`)
})

it(`option: targetLang = undefined`, function() {

const scription = `
\\trs Ninakupenda.↗
`

const { dom } = parse(scription)

const transcript = findElementByClass(dom, `trs`)

expect(getAttribute(transcript, `lang`)).to.be.undefined


})

it(`option: targetLang`, function() {

const scription = `
\\trs Ninakupenda.↗
\\txn ninakupenda
`

const targetLang = `swa`
const { dom } = parse(scription, { targetLang })
const trs = findElementByClass(dom, `trs`)
const txn = findElementByClass(dom, `txn`)

expect(getAttribute(trs, `lang`)).to.equal(targetLang)
expect(getAttribute(txn, `lang`)).to.equal(targetLang)


})

})
30 changes: 30 additions & 0 deletions test/lines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,36 @@ describe(`lines`, function() {

})

it(`option: targetLang = undefined`, function() {

const scription = `
\\phon ɔ́gɔ̀tɛ́ɛ́rɛ̀rà
\\m ó-ko-tɛ́ɛr-er-a
\\gl pp-5-sing-appl-ind
`

const { dom } = parse(scription)
const phon = findElementByClass(dom, `phon`)

expect(getAttribute(phon, `lang`)).to.be.undefined

})

it(`option: targetLang`, function() {

const scription = `
\\phon ɔ́gɔ̀tɛ́ɛ́rɛ̀rà
\\m ó-ko-tɛ́ɛr-er-a
\\gl pp-5-sing-appl-ind
`

const { dom } = parse(scription, { targetLang: `guz` })
const phon = findElementByClass(dom, `phon`)

expect(getAttribute(phon, `lang`)).to.equal(`guz-fonipa`)

})

})

describe(`phonemic transcription`, function() {
Expand Down

0 comments on commit a8399e6

Please sign in to comment.