-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
189 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,4 @@ | ||
export type { Diagnostic } from './diagnostic'; | ||
export { DiagnosticSeverity } from './diagnostic'; | ||
export type { DiagnosticFix } from './diagnostic-fix'; | ||
export type { | ||
DiagnosticProvider, | ||
DiagnosticProviderConstructor, | ||
DiagnosticProviderFactory, | ||
DiagnosticsChanged, | ||
} from './diagnostic-provider'; | ||
export type { DiagnosticProvider, DiagnosticsChanged } from './diagnostic-provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ScriptureNode } from './scripture-node'; | ||
|
||
export interface ScriptureSerializer { | ||
serialize(nodes: ScriptureNode[] | ScriptureNode): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
export type { | ||
OnTypeFormattingProvider, | ||
OnTypeFormattingProviderConstructor, | ||
OnTypeFormattingProviderFactory, | ||
} from './on-type-formatting-provider'; | ||
export type { OnTypeFormattingProvider } from './on-type-formatting-provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { UsfmDocument } from './usfm-document'; | ||
export { UsfmDocumentFactory } from './usfm-document-factory'; | ||
export { UsfmScriptureSerializer } from './usfm-scripture-serializer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
ScriptureBook, | ||
ScriptureCell, | ||
ScriptureChapter, | ||
ScriptureCharacterStyle, | ||
ScriptureDocument, | ||
ScriptureMilestone, | ||
ScriptureNode, | ||
ScriptureNote, | ||
ScriptureOptBreak, | ||
ScriptureParagraph, | ||
ScriptureRef, | ||
ScriptureRow, | ||
ScriptureSerializer, | ||
ScriptureSidebar, | ||
ScriptureTable, | ||
ScriptureText, | ||
ScriptureVerse, | ||
} from '@sillsdev/lynx'; | ||
import { UsfmStylesheet, UsfmToken, UsfmTokenizer, UsfmTokenType } from '@sillsdev/machine/corpora'; | ||
|
||
export class UsfmScriptureSerializer implements ScriptureSerializer { | ||
private readonly tokenizer: UsfmTokenizer; | ||
|
||
constructor(private readonly stylesheet: UsfmStylesheet) { | ||
this.tokenizer = new UsfmTokenizer(stylesheet); | ||
} | ||
|
||
serialize(nodes: ScriptureNode[] | ScriptureNode): string { | ||
const tokens = this.toTokens(nodes); | ||
return this.tokenizer.detokenize(tokens, false, true); | ||
} | ||
|
||
private *toTokens(node: ScriptureNode | readonly ScriptureNode[]): Iterable<UsfmToken> { | ||
if (Array.isArray(node)) { | ||
for (const child of node) { | ||
yield* this.toTokens(child as ScriptureNode); | ||
} | ||
} else if (node instanceof ScriptureDocument) { | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureText) { | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.text); | ||
} else if (node instanceof ScriptureBook) { | ||
yield new UsfmToken(UsfmTokenType.Book, node.style, undefined, undefined, node.code); | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureChapter) { | ||
yield new UsfmToken(UsfmTokenType.Chapter, node.style, undefined, undefined, node.number); | ||
if (node.altNumber != null) { | ||
yield new UsfmToken(UsfmTokenType.Character, 'ca', undefined, 'ca*'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.altNumber); | ||
yield new UsfmToken(UsfmTokenType.End, 'ca*'); | ||
} | ||
if (node.pubNumber != null) { | ||
yield new UsfmToken(UsfmTokenType.Paragraph, 'cp'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.pubNumber); | ||
} | ||
} else if (node instanceof ScriptureParagraph) { | ||
yield new UsfmToken(UsfmTokenType.Paragraph, node.style, undefined, node.style + '*'); | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureVerse) { | ||
yield new UsfmToken(UsfmTokenType.Verse, node.style, undefined, undefined, node.number); | ||
if (node.altNumber != null) { | ||
yield new UsfmToken(UsfmTokenType.Character, 'va', undefined, 'va*'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.altNumber); | ||
yield new UsfmToken(UsfmTokenType.End, 'va*'); | ||
} | ||
if (node.pubNumber != null) { | ||
yield new UsfmToken(UsfmTokenType.Character, 'vp', undefined, 'vp*'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.pubNumber); | ||
yield new UsfmToken(UsfmTokenType.End, 'vp*'); | ||
} | ||
} else if (node instanceof ScriptureCharacterStyle) { | ||
yield new UsfmToken(UsfmTokenType.Character, node.style, undefined, node.style + '*'); | ||
yield* this.toTokens(node.children); | ||
yield new UsfmToken(UsfmTokenType.End, node.style + '*'); | ||
} else if (node instanceof ScriptureMilestone) { | ||
let type: UsfmTokenType; | ||
let endMarker: string | undefined; | ||
if (node.isStart) { | ||
type = UsfmTokenType.Milestone; | ||
const tag = this.stylesheet.getTag(node.style); | ||
endMarker = tag.endMarker; | ||
} else { | ||
type = UsfmTokenType.MilestoneEnd; | ||
endMarker = undefined; | ||
} | ||
yield new UsfmToken(type, node.style, undefined, endMarker); | ||
} else if (node instanceof ScriptureNote) { | ||
yield new UsfmToken(UsfmTokenType.Note, node.style, undefined, node.style + '*', node.caller); | ||
if (node.category != null) { | ||
yield new UsfmToken(UsfmTokenType.Character, 'cat', undefined, 'cat*'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.category); | ||
yield new UsfmToken(UsfmTokenType.End, 'cat*'); | ||
} | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureRef) { | ||
yield new UsfmToken(UsfmTokenType.Character, 'ref', undefined, 'ref*'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, `${node.display}|${node.target}`); | ||
yield new UsfmToken(UsfmTokenType.End, 'ref*'); | ||
} else if (node instanceof ScriptureOptBreak) { | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, '\\'); | ||
} else if (node instanceof ScriptureSidebar) { | ||
yield new UsfmToken(UsfmTokenType.Paragraph, node.style); | ||
if (node.category != null) { | ||
yield new UsfmToken(UsfmTokenType.Character, 'esbc', undefined, 'esbc*'); | ||
yield new UsfmToken(UsfmTokenType.Text, undefined, node.category); | ||
yield new UsfmToken(UsfmTokenType.End, 'esbc*'); | ||
} | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureTable) { | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureRow) { | ||
yield new UsfmToken(UsfmTokenType.Paragraph, 'tr'); | ||
yield* this.toTokens(node.children); | ||
} else if (node instanceof ScriptureCell) { | ||
let marker = node.style; | ||
if (node.colSpan > 0) { | ||
marker += '-' + node.colSpan.toString(); | ||
} | ||
yield new UsfmToken(UsfmTokenType.Character, marker); | ||
yield* this.toTokens(node.children); | ||
} | ||
} | ||
} |
Oops, something went wrong.