Skip to content

Commit

Permalink
Merged PR 305: v1.28.0 - support super/subscripts (#285)
Browse files Browse the repository at this point in the history
- Support subscripts and superscripts through <sub> and <sup> tags, respectively
- Put question_number before buzzes when outputting QBJ
- Bump version to 1.28.0
  • Loading branch information
alopezlago authored Mar 11, 2024
1 parent a2f4298 commit 458a25d
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modaq",
"version": "1.27.0",
"version": "1.28.0",
"description": "Quiz Bowl Reader using TypeScript, React, and MobX",
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions src/components/FormattedText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const FormattedSegment = observer(function FormattedSegment(props: IFormattedSeg
element = <u>{element}</u>;
}

if (props.segment.subscripted) {
element = <sub>{element}</sub>;
}

if (props.segment.superscripted) {
element = <sup>{element}</sup>;
}

// Obsolete, but here for back-compat with YAPP versions before 0.2.4
if (props.segment.required) {
element = (
Expand Down
32 changes: 28 additions & 4 deletions src/parser/FormattedTextParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ export function parseFormattedText(text: string, pronunciationGuideMarkers?: [st
let bolded = false;
let emphasized = false;
let underlined = false;
let subscripted = false;
let superscripted = false;
let pronunciation = false;
let startIndex = 0;

// If we need to support older browswers, use RegExp, exec, and a while loop. See
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
const matchIterator: IterableIterator<RegExpMatchArray> =
pronunciationGuideMarkers == undefined
? text.matchAll(/<\/?em>|<\/?req>|<\/?b>|<\/?u>/gi)
? text.matchAll(/<\/?em>|<\/?req>|<\/?b>|<\/?u>|<\/?sub>|<\/?sup>/gi)
: text.matchAll(
new RegExp(
`<\\/?em>|<\\/?req>|<\\/?b>|<\\/?u>|${escapeRegExp(pronunciationGuideMarkers[0])}|${escapeRegExp(
pronunciationGuideMarkers[1]
)}`,
`<\\/?em>|<\\/?req>|<\\/?b>|<\\/?u>|<\\/?sub>|<\\/?sup>|${escapeRegExp(
pronunciationGuideMarkers[0]
)}|${escapeRegExp(pronunciationGuideMarkers[1])}`,
"gi"
)
);
Expand All @@ -42,6 +44,8 @@ export function parseFormattedText(text: string, pronunciationGuideMarkers?: [st
bolded,
emphasized,
underlined,
subscripted,
superscripted,
pronunciation,
};
result.push(formattedSlice);
Expand Down Expand Up @@ -77,6 +81,18 @@ export function parseFormattedText(text: string, pronunciationGuideMarkers?: [st
case "</u>":
underlined = false;
break;
case "<sub>":
subscripted = true;
break;
case "</sub>":
subscripted = false;
break;
case "<sup>":
superscripted = true;
break;
case "</sup>":
superscripted = false;
break;
default:
if (pronunciationGuideMarkers) {
if (tag === pronunciationGuideMarkers[0].toLowerCase()) {
Expand Down Expand Up @@ -106,6 +122,8 @@ export function parseFormattedText(text: string, pronunciationGuideMarkers?: [st
bolded,
emphasized,
underlined,
subscripted,
superscripted,
pronunciation,
});
}
Expand Down Expand Up @@ -150,6 +168,8 @@ export function splitFormattedTextIntoWords(
bolded: value.bolded,
emphasized: value.emphasized,
underlined: value.underlined,
subscripted: value.subscripted,
superscripted: value.superscripted,
pronunciation: value.pronunciation,
});
splitFormattedText.push(previousWord);
Expand All @@ -167,6 +187,8 @@ export function splitFormattedTextIntoWords(
bolded: value.bolded,
emphasized: value.emphasized,
underlined: value.underlined,
subscripted: value.subscripted,
superscripted: value.superscripted,
pronunciation: value.pronunciation,
};
splitFormattedText.push([formattedWord]);
Expand All @@ -180,6 +202,8 @@ export function splitFormattedTextIntoWords(
bolded: value.bolded,
emphasized: value.emphasized,
underlined: value.underlined,
subscripted: value.subscripted,
superscripted: value.superscripted,
pronunciation: value.pronunciation,
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/IFormattedText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export interface IFormattedText {
pronunciation?: boolean;
required?: boolean;
underlined?: boolean;
subscripted?: boolean;
superscripted?: boolean;
}
2 changes: 1 addition & 1 deletion src/qbj/QBJ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ export function toQBJ(game: GameState, packetName?: string, round?: number): IMa

// We have to track tu/bonus question numbers
const matchQuestion: IMatchQuestion = {
buzzes: [],
question_number: i + 1,
buzzes: [],
tossup_question: {
parts: 1,
type: "tossup",
Expand Down
Loading

0 comments on commit 458a25d

Please sign in to comment.