From 38739ca7ad89a3b743696e9b3b95c26d1a50b07a Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Sun, 20 Mar 2022 14:08:10 -0400 Subject: Adding custom markdown based on asset file --- webAO/utils/aoml.js | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 webAO/utils/aoml.js (limited to 'webAO/utils') diff --git a/webAO/utils/aoml.js b/webAO/utils/aoml.js new file mode 100644 index 0000000..6c4cafb --- /dev/null +++ b/webAO/utils/aoml.js @@ -0,0 +1,100 @@ +import request from "../services/request" + + +const aomlParser = (text) => { + let parsed = {} + let currentHeader = '' + for (const line of text.split(/\r?\n/)) { + if (line === '') { + currentHeader = '' + continue; + } + const content = line.split(' = ') + const contentName = content[0] + const contentValue = content[1] + if (currentHeader === '') { + currentHeader = contentName + parsed[currentHeader] = { + color: contentValue + } + } else { + const contentKey = contentName.split('_')[1] + parsed[currentHeader][contentKey] = contentValue + } + } + return parsed +} + + + + +const mlConfig = (AO_HOST) => { + const defaultUrl = `${AO_HOST}themes/default/chat_config.ini` + let aomlParsed = {} + + request(defaultUrl).then((data) => { + aomlParsed = aomlParser(data) + } + ); + + const createIdentifiers = () => { + const identifiers = new Map() + for (const [ruleName, value] of Object.entries(aomlParsed)) { + if (identifiers.has(value.start)) { + throw new Error() + } + else if (value.start) { + identifiers.set(value.start, value.end) + } + } + return identifiers + } + const colorIdentifiers = () => { + let colorIdentifier = {} + for (const [ruleName, value] of Object.entries(aomlParsed)) { + colorIdentifier[value.start] = value.color + } + return colorIdentifier + } + const applyMarkdown = (text) => { + const identifiers = createIdentifiers(aomlParsed) + const startIdentifiers = new Set(identifiers.keys()) + const colorIdentifier = colorIdentifiers() + const identifierClosingStack = [] + const colorStack = [[255, 255, 255]] + // each value in output will be an html element + let output = [] + for (const letter of text) { + let currentSelector = document.createElement('span') + + const lastItem = identifierClosingStack.length + const closingToLookFor = identifierClosingStack[lastItem-1] + if (letter === closingToLookFor) { + identifierClosingStack.pop() + colorStack.pop() + } + else if (startIdentifiers.has(letter)) { + identifierClosingStack.push(identifiers.get(letter)) + const colors = colorIdentifier[letter].split(',') + const r = colors[0] + const g = colors[1] + const b = colors[2] + colorStack.push([r,g,b]) + } else { + currentSelector.innerHTML = letter + } + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + output.push(currentSelector) + } + return output + } + return { + applyMarkdown + } +} + +export default mlConfig \ No newline at end of file -- cgit From 79db229a94aa3f407399158470e3d027d2b035e8 Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Mon, 21 Mar 2022 17:26:05 -0400 Subject: Fixing bugs --- webAO/utils/aoml.js | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'webAO/utils') diff --git a/webAO/utils/aoml.js b/webAO/utils/aoml.js index 6c4cafb..9506fbf 100644 --- a/webAO/utils/aoml.js +++ b/webAO/utils/aoml.js @@ -43,9 +43,12 @@ const mlConfig = (AO_HOST) => { if (identifiers.has(value.start)) { throw new Error() } - else if (value.start) { - identifiers.set(value.start, value.end) + else if (value.start && value.end) { + identifiers.set(value.start, value) + identifiers.set(value.end, value) } + + } return identifiers } @@ -56,38 +59,63 @@ const mlConfig = (AO_HOST) => { } return colorIdentifier } - const applyMarkdown = (text) => { + const applyMarkdown = (text, defaultColor) => { const identifiers = createIdentifiers(aomlParsed) const startIdentifiers = new Set(identifiers.keys()) const colorIdentifier = colorIdentifiers() const identifierClosingStack = [] + const colorStack = [[255, 255, 255]] // each value in output will be an html element let output = [] for (const letter of text) { let currentSelector = document.createElement('span') - + let letterIdentifier = identifiers.get(letter) const lastItem = identifierClosingStack.length const closingToLookFor = identifierClosingStack[lastItem-1] + const keepChar = Number(letterIdentifier?.remove) === 0 + if (letter === closingToLookFor) { identifierClosingStack.pop() + if (keepChar) { + currentSelector.innerHTML = letter + if (colorStack.length === 1){ + currentSelector.className = `text_${defaultColor}` + } else { + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + } + output.push(currentSelector) + } colorStack.pop() + continue; } else if (startIdentifiers.has(letter)) { - identifierClosingStack.push(identifiers.get(letter)) + identifierClosingStack.push(identifiers.get(letter).end) const colors = colorIdentifier[letter].split(',') const r = colors[0] const g = colors[1] const b = colors[2] colorStack.push([r,g,b]) + if (keepChar) { + currentSelector.innerHTML = letter + } + } else { currentSelector.innerHTML = letter } - const r = colorStack[colorStack.length-1][0] - const g = colorStack[colorStack.length-1][1] - const b = colorStack[colorStack.length-1][2] - const currentColor = `color: rgb(${r},${g},${b});` - currentSelector.setAttribute('style', currentColor) + if (colorStack.length === 1) { + currentSelector.className = `test_${defaultColor}` + } else { + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + } output.push(currentSelector) } return output -- cgit From a5d85f1e7cdbf144e2793da3bb5183eff9c6a7bc Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Tue, 22 Mar 2022 00:16:34 -0400 Subject: converted to typescript --- webAO/utils/aoml.js | 128 -------------------------------------------------- webAO/utils/aoml.ts | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 128 deletions(-) delete mode 100644 webAO/utils/aoml.js create mode 100644 webAO/utils/aoml.ts (limited to 'webAO/utils') diff --git a/webAO/utils/aoml.js b/webAO/utils/aoml.js deleted file mode 100644 index 9506fbf..0000000 --- a/webAO/utils/aoml.js +++ /dev/null @@ -1,128 +0,0 @@ -import request from "../services/request" - - -const aomlParser = (text) => { - let parsed = {} - let currentHeader = '' - for (const line of text.split(/\r?\n/)) { - if (line === '') { - currentHeader = '' - continue; - } - const content = line.split(' = ') - const contentName = content[0] - const contentValue = content[1] - if (currentHeader === '') { - currentHeader = contentName - parsed[currentHeader] = { - color: contentValue - } - } else { - const contentKey = contentName.split('_')[1] - parsed[currentHeader][contentKey] = contentValue - } - } - return parsed -} - - - - -const mlConfig = (AO_HOST) => { - const defaultUrl = `${AO_HOST}themes/default/chat_config.ini` - let aomlParsed = {} - - request(defaultUrl).then((data) => { - aomlParsed = aomlParser(data) - } - ); - - const createIdentifiers = () => { - const identifiers = new Map() - for (const [ruleName, value] of Object.entries(aomlParsed)) { - if (identifiers.has(value.start)) { - throw new Error() - } - else if (value.start && value.end) { - identifiers.set(value.start, value) - identifiers.set(value.end, value) - } - - - } - return identifiers - } - const colorIdentifiers = () => { - let colorIdentifier = {} - for (const [ruleName, value] of Object.entries(aomlParsed)) { - colorIdentifier[value.start] = value.color - } - return colorIdentifier - } - const applyMarkdown = (text, defaultColor) => { - const identifiers = createIdentifiers(aomlParsed) - const startIdentifiers = new Set(identifiers.keys()) - const colorIdentifier = colorIdentifiers() - const identifierClosingStack = [] - - const colorStack = [[255, 255, 255]] - // each value in output will be an html element - let output = [] - for (const letter of text) { - let currentSelector = document.createElement('span') - let letterIdentifier = identifiers.get(letter) - const lastItem = identifierClosingStack.length - const closingToLookFor = identifierClosingStack[lastItem-1] - const keepChar = Number(letterIdentifier?.remove) === 0 - - if (letter === closingToLookFor) { - identifierClosingStack.pop() - if (keepChar) { - currentSelector.innerHTML = letter - if (colorStack.length === 1){ - currentSelector.className = `text_${defaultColor}` - } else { - const r = colorStack[colorStack.length-1][0] - const g = colorStack[colorStack.length-1][1] - const b = colorStack[colorStack.length-1][2] - const currentColor = `color: rgb(${r},${g},${b});` - currentSelector.setAttribute('style', currentColor) - } - output.push(currentSelector) - } - colorStack.pop() - continue; - } - else if (startIdentifiers.has(letter)) { - identifierClosingStack.push(identifiers.get(letter).end) - const colors = colorIdentifier[letter].split(',') - const r = colors[0] - const g = colors[1] - const b = colors[2] - colorStack.push([r,g,b]) - if (keepChar) { - currentSelector.innerHTML = letter - } - - } else { - currentSelector.innerHTML = letter - } - if (colorStack.length === 1) { - currentSelector.className = `test_${defaultColor}` - } else { - const r = colorStack[colorStack.length-1][0] - const g = colorStack[colorStack.length-1][1] - const b = colorStack[colorStack.length-1][2] - const currentColor = `color: rgb(${r},${g},${b});` - currentSelector.setAttribute('style', currentColor) - } - output.push(currentSelector) - } - return output - } - return { - applyMarkdown - } -} - -export default mlConfig \ No newline at end of file diff --git a/webAO/utils/aoml.ts b/webAO/utils/aoml.ts new file mode 100644 index 0000000..898d8fb --- /dev/null +++ b/webAO/utils/aoml.ts @@ -0,0 +1,133 @@ +import request from "../services/request" + +interface Aoml { + name: string; + start: string; + end: string; + remove: number; + talking: number; + color: string; +} +const aomlParser = (text: string) => { + let parsed: {[key: string]: Aoml}= {} + let currentHeader = '' + for (const line of text.split(/\r?\n/)) { + if (line === '') { + currentHeader = '' + continue; + } + const content = line.split(' = ') + const contentName = content[0] + const contentValue = content[1] + if (currentHeader === '') { + currentHeader = contentName + parsed[currentHeader] = { + color: contentValue + } as Aoml + } else { + const contentKey = contentName.split('_')[1] + parsed[currentHeader][contentKey] = contentValue + } + } + return parsed +} + + + + +const mlConfig = (AO_HOST) => { + const defaultUrl = `${AO_HOST}themes/default/chat_config.ini` + let aomlParsed: {[key: string]: Aoml} = {} + + request(defaultUrl).then((data) => { + aomlParsed = aomlParser(data) + } + ); + + const createIdentifiers = () => { + const identifiers = new Map() + for (const [ruleName, value] of Object.entries(aomlParsed)) { + if (identifiers.has(value.start)) { + throw new Error() + } + else if (value.start && value.end) { + identifiers.set(value.start, value) + identifiers.set(value.end, value) + } + + + } + return identifiers + } + const createStartIdentifiers = () => { + const startingIdentifiers = new Set() + for (const [ruleName, value] of Object.entries(aomlParsed)) { + if (value?.start && value?.end) { + startingIdentifiers.add(value.start) + } + } + return startingIdentifiers + } + + const applyMarkdown = (text: string, defaultColor: string) => { + const identifiers = createIdentifiers() + const startIdentifiers = createStartIdentifiers() + const closingStack = [] + const colorStack = [] + // each value in output will be an html element + let output: HTMLSpanElement[] = [] + for (const letter of text) { + let currentSelector = document.createElement('span') + let currentIdentifier = identifiers.get(letter) + const currentClosingLetter = closingStack[closingStack.length-1] + const keepChar = Number(currentIdentifier?.remove) === 0 + console.log(startIdentifiers) + if (startIdentifiers.has(letter)) { + const color = identifiers.get(letter).color.split(',') + const r = color[0] + const g = color[1] + const b = color[2] + colorStack.push([r,g,b]) + closingStack.push(currentIdentifier.end) + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + if (keepChar) { + currentSelector.innerHTML = letter + } + } else if (currentClosingLetter === letter) { + if (colorStack.length === 0) { + currentSelector.className = `test_${defaultColor}` + } else { + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + } + closingStack.pop() + colorStack.pop() + if (keepChar) { + currentSelector.innerHTML = letter + } + } else { + currentSelector.innerHTML = letter + if (colorStack.length === 0) { + currentSelector.className = `text_${defaultColor}` + } else { + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) + } + } + output.push(currentSelector) + } + return output + } + return { + applyMarkdown + } +} + +export default mlConfig \ No newline at end of file -- cgit From 7c88c0bcca6e47cca4e2632df8ced778fa38c341 Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Wed, 23 Mar 2022 00:33:18 -0400 Subject: Final commit --- webAO/utils/__tests__/aoml.test.ts | 113 +++++++++++++++++++++++++++++++++++++ webAO/utils/aoml.ts | 51 ++++++----------- 2 files changed, 131 insertions(+), 33 deletions(-) create mode 100644 webAO/utils/__tests__/aoml.test.ts (limited to 'webAO/utils') diff --git a/webAO/utils/__tests__/aoml.test.ts b/webAO/utils/__tests__/aoml.test.ts new file mode 100644 index 0000000..fa8ade4 --- /dev/null +++ b/webAO/utils/__tests__/aoml.test.ts @@ -0,0 +1,113 @@ +import request from '../../services/request' +import mlConfig from '../aoml'; + +jest.mock('../../services/request') +const networkRequest = ` +c0 = 247, 247, 247 +c0_name = White +c0_talking = 1 + +c2 = 247, 0, 57 +c2_name = Red +c2_start = ~ +c2_end = ~ +c2_remove = 1 +c2_talking = 1 + +c4 = 107, 198, 247 +c4_name = Blue +c4_start = ( +c4_end = ) +c4_remove = 0 +c4_talking = 0 + +c5 = 107, 198, 247 +c5_name = Blue +c5_start = [ +c5_end = ] +c5_remove = 1 +c5_talking = 0 + +c6 = 107, 198, 247 +c6_name = Blue +c6_start = | +c6_end = | +c6_remove = 0 +c6_talking = 0 +` + +const mockRequest = request as jest.MockedFunction; +mockRequest.mockReturnValue(Promise.resolve(networkRequest)) + + describe('mlConfig', () => { + beforeEach(() => { + // Clear all instances and calls to constructor and all methods: + mockRequest.mockClear(); + + }); + + it('Should make a network request', () => { + mlConfig('localhost') + expect(mockRequest).toHaveBeenCalledTimes(1); + }); + }) + describe('applyMarkdown', () => { + const config = mlConfig('localhost') + + beforeEach(() => { + // Clear all instances and calls to constructor and all methods: + mockRequest.mockClear(); + + }); + + it('Should create an array of spans containing letters', async () => { + const word = `hello` + const actual = await config.applyMarkdown(`hello`, `blue`) + let index = 0 + for (const element of actual) { + expect(element.innerHTML).toBe(word[index]) + index++ + } + }) + it('Should add colors based on settings', async () => { + const config = mlConfig('localhost') + const actual = await config.applyMarkdown(`(heya)`, `blue`) + expect(actual[0].getAttribute('style')).toBe('color: rgb(107, 198, 247);') + }) + it('Should keep a letter if remove = 0', async () => { + const config = mlConfig('localhost') + + const actual = await config.applyMarkdown(`(What())Hey!`, `white`) + const expected = `(` + expect(actual[5].innerHTML).toBe(expected) + }) + it('Should remove a letter if remove = 1', async () => { + const config = mlConfig('localhost') + + const actual = await config.applyMarkdown(`~What~()Hey!`, `white`) + const expected = `` + expect(actual[0].innerHTML).toBe(expected) + }) + it('Should remove a letter if remove = 1', async () => { + const config = mlConfig('localhost') + + const actual = await config.applyMarkdown(`~What~()Hey!`, `white`) + const expected = `` + expect(actual[0].innerHTML).toBe(expected) + }) + it('Should keep a closing letter if remove = 0', async () => { + const config = mlConfig('localhost') + + const actual = await config.applyMarkdown(`~NO[]~!`, `white`) + const expected = `]` + expect(actual[4].innerHTML).toBe(expected) + }) + it('Should remove a closing letter if remove = 1', async () => { + const config = mlConfig('localhost') + const actual = await config.applyMarkdown(`~NO||~!`, `white`) + const expected = `` + expect(actual[5].innerHTML).toBe(expected) + }) + + }) + diff --git a/webAO/utils/aoml.ts b/webAO/utils/aoml.ts index 898d8fb..fb26db8 100644 --- a/webAO/utils/aoml.ts +++ b/webAO/utils/aoml.ts @@ -32,46 +32,34 @@ const aomlParser = (text: string) => { return parsed } - - - const mlConfig = (AO_HOST) => { const defaultUrl = `${AO_HOST}themes/default/chat_config.ini` - let aomlParsed: {[key: string]: Aoml} = {} + let aomlParsed: Promise<{[key: string]: Aoml}> = request(defaultUrl).then((data) => aomlParser(data)); - request(defaultUrl).then((data) => { - aomlParsed = aomlParser(data) - } - ); + - const createIdentifiers = () => { + const createIdentifiers = async () => { const identifiers = new Map() - for (const [ruleName, value] of Object.entries(aomlParsed)) { - if (identifiers.has(value.start)) { - throw new Error() - } - else if (value.start && value.end) { + for (const [ruleName, value] of Object.entries(await aomlParsed)) { + if (value.start && value.end) { identifiers.set(value.start, value) identifiers.set(value.end, value) - } - - + } } return identifiers } - const createStartIdentifiers = () => { + const createStartIdentifiers = async () => { const startingIdentifiers = new Set() - for (const [ruleName, value] of Object.entries(aomlParsed)) { + for (const [ruleName, value] of Object.entries(await aomlParsed)) { if (value?.start && value?.end) { startingIdentifiers.add(value.start) } } return startingIdentifiers } - - const applyMarkdown = (text: string, defaultColor: string) => { - const identifiers = createIdentifiers() - const startIdentifiers = createStartIdentifiers() + const applyMarkdown = async (text: string, defaultColor: string) => { + const identifiers = await createIdentifiers() + const startIdentifiers = await createStartIdentifiers() const closingStack = [] const colorStack = [] // each value in output will be an html element @@ -81,7 +69,8 @@ const mlConfig = (AO_HOST) => { let currentIdentifier = identifiers.get(letter) const currentClosingLetter = closingStack[closingStack.length-1] const keepChar = Number(currentIdentifier?.remove) === 0 - console.log(startIdentifiers) + console.log(currentClosingLetter, letter, keepChar) + if (startIdentifiers.has(letter)) { const color = identifiers.get(letter).color.split(',') const r = color[0] @@ -95,15 +84,11 @@ const mlConfig = (AO_HOST) => { currentSelector.innerHTML = letter } } else if (currentClosingLetter === letter) { - if (colorStack.length === 0) { - currentSelector.className = `test_${defaultColor}` - } else { - const r = colorStack[colorStack.length-1][0] - const g = colorStack[colorStack.length-1][1] - const b = colorStack[colorStack.length-1][2] - const currentColor = `color: rgb(${r},${g},${b});` - currentSelector.setAttribute('style', currentColor) - } + const r = colorStack[colorStack.length-1][0] + const g = colorStack[colorStack.length-1][1] + const b = colorStack[colorStack.length-1][2] + const currentColor = `color: rgb(${r},${g},${b});` + currentSelector.setAttribute('style', currentColor) closingStack.pop() colorStack.pop() if (keepChar) { -- cgit From efe16e1c1e7d4b0ea13c04b816d046ad21eeadb0 Mon Sep 17 00:00:00 2001 From: "caleb.mabry.15@cnu.edu" Date: Wed, 23 Mar 2022 00:38:26 -0400 Subject: Artifacts from testing --- webAO/utils/__tests__/aoml.test.ts | 2 +- webAO/utils/aoml.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'webAO/utils') diff --git a/webAO/utils/__tests__/aoml.test.ts b/webAO/utils/__tests__/aoml.test.ts index fa8ade4..90967d7 100644 --- a/webAO/utils/__tests__/aoml.test.ts +++ b/webAO/utils/__tests__/aoml.test.ts @@ -99,7 +99,7 @@ mockRequest.mockReturnValue(Promise.resolve(networkRequest)) const config = mlConfig('localhost') const actual = await config.applyMarkdown(`~NO[]~!`, `white`) - const expected = `]` + const expected = `` expect(actual[4].innerHTML).toBe(expected) }) it('Should remove a closing letter if remove = 1', async () => { diff --git a/webAO/utils/aoml.ts b/webAO/utils/aoml.ts index fb26db8..da66d0c 100644 --- a/webAO/utils/aoml.ts +++ b/webAO/utils/aoml.ts @@ -69,7 +69,6 @@ const mlConfig = (AO_HOST) => { let currentIdentifier = identifiers.get(letter) const currentClosingLetter = closingStack[closingStack.length-1] const keepChar = Number(currentIdentifier?.remove) === 0 - console.log(currentClosingLetter, letter, keepChar) if (startIdentifiers.has(letter)) { const color = identifiers.get(letter).color.split(',') -- cgit From 63410fd71e31e0de21aed41016983146405e2e9d Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Wed, 23 Mar 2022 21:52:13 +0100 Subject: ao host is a string --- webAO/utils/aoml.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'webAO/utils') diff --git a/webAO/utils/aoml.ts b/webAO/utils/aoml.ts index da66d0c..154274d 100644 --- a/webAO/utils/aoml.ts +++ b/webAO/utils/aoml.ts @@ -32,7 +32,7 @@ const aomlParser = (text: string) => { return parsed } -const mlConfig = (AO_HOST) => { +const mlConfig = (AO_HOST: string) => { const defaultUrl = `${AO_HOST}themes/default/chat_config.ini` let aomlParsed: Promise<{[key: string]: Aoml}> = request(defaultUrl).then((data) => aomlParser(data)); -- cgit