Skip to content

Commit

Permalink
NEW: option: glosses (#26)
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
dwhieb committed Feb 24, 2024
1 parent 6feefc7 commit 10631c0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ If the input is a string containing only whitespace, an empty string is returned
## Options
| 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. |
| `glosses` | Boolean \| Array \| Object | `false` | Options for wrapping glosses in `<abbr>` tags.<br><br>If set to `false` (default), no `<abbr>` tags are added to the glosses.<br><br>If set to `true`, an `<abbr>` tag is wrapped around any glosses in CAPS, and any numbers.<br><br>If set to an array, any glosses listed in the array will be wrapped in `<abbr>` tags. (This is useful if you'd certain glosses to be lowercase but still wrapped in an `<abbr>` tag, such as `sg` and `pl`, which are commonly lowercased.)<br><br>If set to an object hash, the keys of the object are treated as glosses, and the values of the object are treated as definitions for those glosses. Each gloss will be wrapped in an `<abbr>` tag with a `title` attribute set to the definition. |
| `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. |
| 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. |
| `glosses` | Boolean | `false` | Options for wrapping glosses in `<abbr>` tags.<br><br>If set to `false` (default), no `<abbr>` tags are added to the glosses.<br><br>If set to `true`, an `<abbr>` tag is wrapped around any glosses in CAPS, any numbers, and any of `sg`, `du`, or `pl` (lowercased). |
| `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 Down
7 changes: 5 additions & 2 deletions src/words/glosses.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import addEmphasis from '../utilities/addEmphasis.js'
import replaceHyphens from '../utilities/replaceHyphens.js'

const glossRegExp = /(?<gloss>[1-4]|[A-Z]+)/gv
const glossRegExp = /(?<gloss>[1-4]|[A-Z]+)/gv
const numberRegExp = /\b(?<number>sg|du|pl)\b/gv

function createGlossLine(glosses, language, option) {

Expand All @@ -21,7 +22,9 @@ function createGlossLine(glosses, language, option) {
* @returns {string}
*/
function wrapGlosses(glosses) {
return glosses.replaceAll(glossRegExp, `<abbr>$1</abbr>`)
return glosses
.replaceAll(glossRegExp, `<abbr>$1</abbr>`)
.replaceAll(numberRegExp, `<abbr>$1</abbr>`)
}

export default function createGlosses(data, { analysisLang, glosses: glossesOption }) {
Expand Down
30 changes: 22 additions & 8 deletions test/words.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,34 @@ describe(`words`, function() {
I love you
`

const { dom } = await parse(scription, { glosses: true })
const glosses = findElements(dom, el => getTagName(el) === `abbr`)
const [num, caps] = glosses

expect(getTextContent(num)).to.equal(`1`)
expect(getTextContent(caps)).to.equal(`SG`)
const { dom } = await parse(scription, { glosses: true })
const glosses = findElements(dom, el => getTagName(el) === `abbr`)
const [person, num] = glosses

expect(glosses).to.have.length(8)
expect(getTextContent(person)).to.equal(`1`)
expect(getTextContent(num)).to.equal(`SG`)

})

it(`option: glosses = array`)
it(`option: glosses (lowercase glosses)`, async function() {

it(`option: glosses = object`)
const scription = `
ninakupenda
ni-na-ku-pend-a
1sg.SUBJ-PRES-2sg.OBJ-love-IND
I love you
`

const { dom } = await parse(scription, { glosses: true })
const glosses = findElements(dom, el => getTagName(el) === `abbr`)
const [person, num] = glosses

expect(glosses).to.have.length(8)
expect(getTextContent(person)).to.equal(`1`)
expect(getTextContent(num)).to.equal(`sg`)

})

})

Expand Down

0 comments on commit 10631c0

Please sign in to comment.