From dab9bee4b46aaa1241cdb6b565ddbe0f19137c5e Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 3 Jul 2023 14:10:06 +0200 Subject: add utility wrap method --- api/japanese.ts | 11 +++++++---- api/sentence.ts | 2 +- util/string.ts | 8 ++++++++ util/wrap.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 util/wrap.ts diff --git a/api/japanese.ts b/api/japanese.ts index 9319cdd..ffbc773 100644 --- a/api/japanese.ts +++ b/api/japanese.ts @@ -3,20 +3,23 @@ import { escape } from "https://deno.land/std@0.192.0/html/entities.ts"; import "../util/string.ts"; import "../util/japanese.ts"; import "../util/array.ts"; +import { Wrap } from "../util/wrap.ts"; const formatters = { "HTML": tokens => tokens.reduce((out, token) => { - if (token.ruby) out += `${escape(token.writing)}${escape(token.reading)}`; - else out += token.writing; + if (token.ruby) { + out += (escape(token.writing) + + escape(token.reading).wrap(Wrap.HTML.rubyText)).wrap(Wrap.HTML.ruby); + } else out += token.writing; return out; }, ""), "parenthesis": tokens => tokens.reduce((out, token) => { - if (token.ruby) out += `${token.writing}(${token.reading}) `; + if (token.ruby) out += token.writing + token.reading.wrap(Wrap.parenthesis) + " "; else out += token.writing; return out; }, ""), "refold-tools": tokens => tokens.reduce((out, token) => { - if (token.ruby) out += `[${token.writing}](${token.reading})`; + if (token.ruby) out += token.writing.wrap(Wrap.bracket) + token.reading.wrap(Wrap.parenthesis); else out += token.writing; return out; }, ""), diff --git a/api/sentence.ts b/api/sentence.ts index d0cc434..cde66a5 100644 --- a/api/sentence.ts +++ b/api/sentence.ts @@ -1,6 +1,6 @@ import { ParseResult } from "../language/types.ts"; import APIBase from "./base.ts"; -import Japanese, { JapaneseFormatter } from "./japanese.ts"; +import { JapaneseFormatter } from "./japanese.ts"; import Word from "./word.ts"; export default class Sentence extends APIBase { diff --git a/util/string.ts b/util/string.ts index bb6bc0f..b362f06 100644 --- a/util/string.ts +++ b/util/string.ts @@ -1,4 +1,5 @@ import { TokenTags, parseTags } from "../language/tags.ts"; +import { Wrapper } from "./wrap.ts"; declare global { interface String { @@ -49,6 +50,9 @@ declare global { /** @summary replace last instance of `searchString` with `replaceValue` */ replaceLast(searchString: string, replaceValue: string): string; + + /** @summary wrap string using Wrapper */ + wrap(wrapper: Wrapper): string; } } @@ -150,3 +154,7 @@ String.prototype.replaceLast = function(find, replace) { return this.substring(0, i) + replace + this.substring(i + find.length); } +String.prototype.wrap = function(wrapper) { + return wrapper[0] + this + wrapper[1]; +} + diff --git a/util/wrap.ts b/util/wrap.ts new file mode 100644 index 0000000..761a1f0 --- /dev/null +++ b/util/wrap.ts @@ -0,0 +1,41 @@ +export type Wrapper = [string, string]; + +/** @summary make Wrapper that starts and ends with `input` */ +export function WrapWith(input: string): Wrapper { + return [input, input]; +} + +/** @summary make Wrapper that starts and ends with XML tags with name `tagName` */ +export function WrapTag(tagName: string): Wrapper { + return [`<${tagName}>`, ``]; +} + +// this type is internal to this file +type WrapType = { [K: string]: Wrapper | WrapType }; + +export const Wrap = { + /** @prop (input) */ + parenthesis: ["(", ")"], + /** @prop [input] */ + bracket: ["[", "]"], + /** @prop \{input\} */ + brace: ["{", "}"], + /** @prop HTML-specifics */ + HTML: { + /** @prop \input\ */ + bold: WrapTag("b"), + /** @prop \input\ */ + italic: WrapTag("i"), + /** @prop \input\ */ + span: WrapTag("span"), + /** @prop \input\ */ + ruby: WrapTag("ruby"), + /** @prop \input\ */ + rubyText: WrapTag("rt"), + }, + /** @prop \*input\* */ + asterisk: WrapWith("*"), + /** @prop \_input\_ */ + underscore: WrapWith("_"), +} satisfies WrapType; + -- cgit v1.2.3