Skip to content

Commit

Permalink
NEW: option: analysisLang
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhieb committed Dec 23, 2023
1 parent 72ec6e5 commit 08203f1
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 22 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ If the input is a string containing only whitespace, an empty string is returned

## Options

| Option | type | Default | Description |
| ----------- | ------------- | --------- | ----------------------------------------------------------------------------------------------------------- |
| `classes` | Array<String> | `['igl']` | An array of classes to apply to the wrapper element. |
| `scription` | Object | `{}` | Options to pass to the `scription2dlx` library. See [scription2dlx][scription2dlx] for more details. |
| `tag` | String | `'div'` | The HTML tag to wrap each interlinear gloss in. Can also be a custom tag (useful for HTML custom elements). |
| Option | type | Default | Description |
| -------------- | ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `analysisLang` | String | undefined | An [IETF language tag][lang-tags] to use as the default value of the `lang` attribute for any data in the analysis language (metadata, literal translation, free translation, glosses, literal word translation). If `undefined`, no `lang` tag is added, which means that browsers will assume that the analysis language is the same as the HTML document. |
| `classes` | Array<String> | `['igl']` | An array of classes to apply to the wrapper element. |
| `scription` | Object | `{}` | Options to pass to the `scription2dlx` library. See [scription2dlx][scription2dlx] for more details. |
| `tag` | String | `'div'` | The HTML tag to wrap each interlinear gloss in. Can also be a custom tag (useful for HTML custom elements). |
| `targetLang` | String | undefined | An [IETF language tag][lang-tags] to use as the default value of the `lang` attribute for any data in the target language. |

## HTML Structure

Expand All @@ -135,7 +137,7 @@ const options = {

Each line of the interlinear example is typically given a CSS class that matches its line type (with some exceptions, such as `timespan` instead of just `t`). For example, the `\trs` line will result in `<p class=trs>`, and the `\tln-en` line will result in `<p class=tln lang=en>`.

Whenever the language of a gloss or translation is specified, it is passed through to the `lang` attribute of the relevant analysis language elements, as just seen in the `\tln-en` example. Whenever the default language of the text is specified, it is passed through to the `lang` attribute of the relevant target language elements.
If the language of the text is specified, it is set as the value of the `lang` attribute for data in the target language wherever relevant. Whenever the language of analysis data (metadata, glosses, translations, etc.) is specified, it is passed through to the `lang` attribute of the relevant analysis language elements (`<p class=tln lang=en>`).

When the scription format allows for data in multiple orthographies, the orthography of the data is specified in the `data-ortho` attribute. For example, the following lines of scription are transformed to the HTML that follows:

Expand All @@ -159,6 +161,7 @@ When the scription format allows for data in multiple orthographies, the orthogr
**FORTHCOMING**

<!-- Links -->
[lang-tags]: https://datatracker.ietf.org/doc/html/rfc5646
[learn-Node]: https://nodejs.dev/en/learn/
[Leipzig]: https://www.eva.mpg.de/lingua/resources/glossing-rules.php
[Node]: https://nodejs.org/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"scripts": {
"build": "node build/index.js",
"stop-only": "stop-only --folder src",
"start": "mocha test/*.test.js",
"test": "mocha test/*.test.js"
},
"publishConfig": {
Expand Down
5 changes: 3 additions & 2 deletions src/lines/literal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import addEmphasis from '../utilities/addEmphasis.js'

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

if (typeof data === `string`) {
return `<p class=lit lang=en>${ addEmphasis(data) }</p>`
const lang = analysisLang ? `lang='${ analysisLang }'` : ``
return `<p class=lit ${ lang }>${ addEmphasis(data) }</p>`
}

let html = ``
Expand Down
5 changes: 3 additions & 2 deletions src/lines/metadata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default function createHeader(metadata) {
export default function createHeader(metadata, { analysisLang }) {
if (!metadata) return ``
return `<p class=ex-header>${ metadata }</p>`
const lang = analysisLang ? `lang='${ analysisLang }'` : ``
return `<p class=ex-header ${ lang }>${ metadata }</p>`
}
5 changes: 3 additions & 2 deletions src/lines/translation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import addEmphasis from '../utilities/addEmphasis.js'

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

if (typeof data === `string`) {
return `<p class=tln lang=en>${ addEmphasis(data) }</p>`
const lang = analysisLang ? `lang='${ analysisLang }'` : ``
return `<p class=tln ${ lang }>${ addEmphasis(data) }</p>`
}

let html = ``
Expand Down
4 changes: 4 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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 (`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.`)
Expand Down
10 changes: 6 additions & 4 deletions src/utterance.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import createTranscript from './lines/transcript.js'
import createTranscription from './lines/transcription.js'
import createTranslation from './lines/translation.js'

export default function convertUtterance(u, { classes, tag }) {
export default function convertUtterance(u, options) {

const { classes, tag } = options

const classString = classes.join(` `)
const header = createHeader(u.metadata)
const literal = createLiteral(u.literal)
const header = createHeader(u.metadata, options)
const literal = createLiteral(u.literal, options)
const phonetic = createPhonetic(u.phonetic)
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 translation = createTranslation(u.translation)
const translation = createTranslation(u.translation, options)

return `<${ tag } class='${ classString }'>
${ header }
Expand Down
61 changes: 55 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import convert from '../src/index.js'
import { expect } from 'chai'
import { getAttribute } from '@web/parse5-utils'
import parse from './utilities/convertAndParse.js'
import parseClassString from './utilities/parseClassString.js'
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'

import { Swahili } from '../samples/data/data.js'

describe(`scription2html`, function() {

Expand Down Expand Up @@ -62,6 +64,53 @@ describe(`scription2html`, function() {

})

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

const scription = `
# Swahili
\\txn ninakupenda
\\m ni-na-ku-pend-a
\\gl 1SG.SUBJ-PRES-2SG.OBJ-love-IND
\\lit Te estoy amando.
\\tln Te amo.
`

const { dom } = parse(scription)

const lit = findElementByClass(dom, `lit`)
const tln = findElementByClass(dom, `tln`)
const meta = findElementByClass(dom, `ex-header`)

expect(getAttribute(lit, `lang`)).to.be.undefined
expect(getAttribute(tln, `lang`)).to.be.undefined
expect(getAttribute(meta, `lang`)).to.be.undefined

})

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

const scription = `
# Swahili
\\txn ninakupenda
\\m ni-na-ku-pend-a
\\gl 1SG.SUBJ-PRES-2SG.OBJ-love-IND
\\lit Te estoy amando.
\\tln Te amo.
`

const analysisLang = `sp`
const { dom } = parse(scription, { analysisLang })

const lit = findElementByClass(dom, `lit`)
const tln = findElementByClass(dom, `tln`)
const meta = findElementByClass(dom, `ex-header`)

expect(getAttribute(lit, `lang`)).to.equal(analysisLang)
expect(getAttribute(tln, `lang`)).to.equal(analysisLang)
expect(getAttribute(meta, `lang`)).to.equal(analysisLang)

})

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

const classes = [`example`, `interlinear`]
Expand Down

0 comments on commit 08203f1

Please sign in to comment.