From 40b89bc43c05d443a36f9726444c44f549736913 Mon Sep 17 00:00:00 2001 From: Vigilans Date: Fri, 23 Aug 2019 13:06:20 +0800 Subject: [PATCH] Bring markdown engine into submission provider --- src/webview/leetCodeSubmissionProvider.ts | 68 ++++++++++++++++++++--- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/webview/leetCodeSubmissionProvider.ts b/src/webview/leetCodeSubmissionProvider.ts index f717a38a..f4f18fd4 100644 --- a/src/webview/leetCodeSubmissionProvider.ts +++ b/src/webview/leetCodeSubmissionProvider.ts @@ -9,10 +9,10 @@ import { markdownEngine } from "./markdownEngine"; class LeetCodeSubmissionProvider extends LeetCodeWebview { protected readonly viewType: string = "leetcode.submission"; - private result: string; + private result: IResult; - public show(result: string): void { - this.result = result; + public show(resultString: string): void { + this.result = this.parseResult(resultString); this.showWebviewInternal(); this.showKeybindingsHint(); } @@ -25,18 +25,36 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview { } protected getWebviewContent(): string { - return ` - + const styles: string = markdownEngine.getStyles(); + const title: string = `## ${this.result.messages[0]}`; + const messages: string[] = this.result.messages.slice(1).map((m: string) => `* ${m}`); + const sections: string[] = Object.keys(this.result) + .filter((key: string) => key !== "messages") + .map((key: string) => [ + `### ${key}`, + "```", + this.result[key].join("\n"), + "```", + ].join("\n")); + const body: string = markdownEngine.render([ + title, + ...messages, + ...sections, + ].join("\n")); + return ` + + - ${markdownEngine.getStyles()} + ${styles} - -
${this.result.trim()}
+ + ${body} - `; + + `; } protected onDidDisposeWebview(): void { @@ -52,6 +70,38 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview { (): Promise => openKeybindingsEditor("leetcode solution"), ); } + + private parseResult(raw: string): IResult { + raw = raw.concat(" √ "); // Append a dummy sentinel to the end of raw string + const regSplit: RegExp = / [√×✔✘vx] ([^]+?)\n(?= [√×✔✘vx] )/g; + const regKeyVal: RegExp = /(.+?): ([^]*)/; + const result: IResult = { messages: [] }; + let entry: RegExpExecArray | null; + do { + entry = regSplit.exec(raw); + if (!entry) { + continue; + } + const kvMatch: RegExpExecArray | null = regKeyVal.exec(entry[1]); + if (kvMatch) { + const [key, value] = kvMatch.slice(1); + if (value) { // Do not show empty string + if (!result[key]) { + result[key] = []; + } + result[key].push(value); + } + } else { + result.messages.push(entry[1]); + } + } while (entry); + return result; + } +} + +interface IResult { + [key: string]: string[]; + messages: string[]; } export const leetCodeSubmissionProvider: LeetCodeSubmissionProvider = new LeetCodeSubmissionProvider();