Skip to content

Commit

Permalink
feat: repeat input when commands called with slash feature
Browse files Browse the repository at this point in the history
  • Loading branch information
DGCK81LNN committed Jun 6, 2024
1 parent 65778cb commit d40792f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-xdi8",
"description": "Shidinn tools for Koishi",
"version": "1.1.1",
"version": "1.1.2",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
11 changes: 11 additions & 0 deletions src/locales/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ commands:
options:
all: 显示隐藏结果
messages:
input: <b>希顶转换:</b>{0}
no-result: 未找到可转换的字词。
xdi8-grep:
description: 从字表正则搜索希顶词
Expand All @@ -25,7 +26,12 @@ commands:
legacy: 只搜索旧拼写
messages:
no-result: 未找到符合条件的希顶词。
no-result-with-expr: 未找到匹配正则表达式 <b>{0}</b> 的希顶词。
no-result-legacy-with-expr: 未找到匹配正则表达式 <b>{0}</b> 的希顶旧拼写。
result-header: 匹配正则表达式 <b>{0}</b> 的希顶词:
result-header-legacy: 匹配正则表达式 <b>{0}</b> 的希顶旧拼写:
invalid-pattern: 无效的正则表达式。
invalid-pattern-with-expr: 无效的正则表达式:<b>{0}</b>
result-footer: 共 {0} 条结果。
result-footer-with-legacy: 共 {0} 条结果({1} 条旧拼写)。
xegoe:
Expand Down Expand Up @@ -57,3 +63,8 @@ commands:
messages:
no-valid-phrase: 未发现可转写的词语。
cannot-compile-notation: compile 模式下输入类型不能为 notation。
header: <b>{type} LNN 中华语字:</b>{text}
header-notation: <b>LNN 中华语字:</b>{text}
header-compile: <b>{type}转 LNN 中华语字序列化格式:</b>{text}
shidinn: 希顶
mandarin: 普通话
66 changes: 43 additions & 23 deletions src/plugins/lnnzhyz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { compileMandarin, compileShidinn, draw } from "@dgck81lnn/lnnzhyz2svg"
import { deserializeText, serializeText } from "@dgck81lnn/lnnzhyz2svg/notation"
import type {} from "@koishijs/plugin-help"
import { Context, Schema, h } from "koishi"
import { stripTags } from "../utils"
import { isSlash, stripTags } from "../utils"

export const name = "lnnzhyz"
export const inject = ["component:html"]
Expand Down Expand Up @@ -45,7 +45,11 @@ export function apply(ctx: Context, config: Config) {
cmd.option("type", "-s", { value: "shidinn", hidden: true })
cmd.option("type", "-m", { value: "mandarin", hidden: true })
cmd.option("type", "-n", { value: "notation", hidden: true })
cmd.action(async ({ options: { compile: compileOnly, type }, session }, els) => {
cmd.action(async (argv, els) => {
const {
options: { compile: compileOnly, type },
session,
} = argv
const text = stripTags(els)

if (compileOnly && type === "notation")
Expand Down Expand Up @@ -90,33 +94,49 @@ export function apply(ctx: Context, config: Config) {
.replace(/\ufdd0/g, "<")

if (!someOk) return session.text(".no-valid-phrase")
if (compileOnly) return result
if (compileOnly)
return [
isSlash(argv) &&
session.text(".header-compile", { type: session.text(`.${type}`), text }),
h.escape(result),
]
.filter(Boolean)
.join("<message/>")

return (
<html>
<style>{
/*css*/ `
<>
{isSlash(argv)
? h.parse(
type === "notation"
? session.text(".header-notation", { text })
: session.text(".header", { type: session.text(`.${type}`), text })
)
: ""}
<html>
<style>{
/*css*/ `
img {
height: 1.1875em;
vertical-align: text-bottom;
}`
}</style>
<div
style={{
width: "auto",
height: "auto",
maxWidth: config.width,
lineHeight: "1",
padding: config.padding,
fontSize: `${config.fontSize}px`,
fontFamily: config.fontFamily,
whiteSpace: "pre-wrap",
overflowWrap: "break-word",
}}
>
{h.parse(result.replace(/\n/g, "<br />"))}
</div>
</html>
}</style>
<div
style={{
width: "auto",
height: "auto",
maxWidth: config.width,
lineHeight: "1",
padding: config.padding,
fontSize: `${config.fontSize}px`,
fontFamily: config.fontFamily,
whiteSpace: "pre-wrap",
overflowWrap: "break-word",
}}
>
{h.parse(result.replace(/\n/g, "<br />"))}
</div>
</html>
</>
)
})
}
30 changes: 25 additions & 5 deletions src/plugins/xdi8-grep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, Schema } from "koishi"
import type {} from "../service"
import { ahoFixes } from "../utils"
import { ahoFixes, isSlash } from "../utils"

export const name = "xdi8-grep"
export const inject = ["xdi8"]
Expand Down Expand Up @@ -45,7 +45,8 @@ export function apply(ctx: Context, config: Config) {
showWarning: true,
})
.option("legacy", "-l", { fallback: false })
.action(({ session, options }, pattern) => {
.action((argv, pattern) => {
const { session, options } = argv
let inCharClass = false
function charClass(chars: string) {
return inCharClass ? chars : `[${chars}]`
Expand All @@ -62,19 +63,27 @@ export function apply(ctx: Context, config: Config) {
return s
})

const repeatInput = isSlash(argv)

const re = (() => {
try {
return new RegExp(`^${pattern}$`)
} catch {}
})()
if (!re) return session.text(".invalid-pattern")
if (!re)
return repeatInput
? session.text(".invalid-pattern-with-expr", [pattern])
: session.text(".invalid-pattern")

let entries = ctx.xdi8.hanziToXdi8Transcriber.dict.filter(
entry =>
entry.x.match(re) &&
!(Object.hasOwn(ahoFixes, entry.x) && !ahoFixes[entry.x].includes(entry.h))
)
if (!entries.length) return session.text(".no-result")
if (!entries.length)
return repeatInput
? session.text(".no-result-with-expr", [pattern])
: session.text(".no-result")

let regularEntries = []
let legacyEntries = []
Expand All @@ -84,7 +93,10 @@ export function apply(ctx: Context, config: Config) {
}

if (options.legacy) {
if (!legacyEntries.length) return session.text(".no-result")
if (!legacyEntries.length)
return repeatInput
? session.text(".no-result-legacy-with-expr", [pattern])
: session.text(".no-result")
regularEntries = legacyEntries
legacyEntries = []
}
Expand All @@ -102,6 +114,14 @@ export function apply(ctx: Context, config: Config) {
if (entry.n) line += `(${entry.n})`
return line
})
if (repeatInput)
lines.unshift(
// prettier-ignore
session.text(
options.legacy ? ".result-header-legacy" : ".result-header",
[pattern]
)
)
lines.push(
(more ? "…" : "") +
(legacyEntries.length
Expand Down
32 changes: 20 additions & 12 deletions src/plugins/xdi8.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, Schema, Session, h } from "koishi"
import { Argv, Context, Schema, h } from "koishi"
import type { Alternation, TranscribeResult } from "xdi8-transcriber"
import type {} from "../service"
import { doAhoFix, stripTags } from "../utils"
import { doAhoFix, isSlash, stripTags } from "../utils"

export const name = "xdi8"
export const inject = ["xdi8"]
Expand All @@ -22,11 +22,13 @@ function supNum(n: number) {

export function apply(ctx: Context, config: Config) {
function stringifyResult<T extends "h" | "x">(
session: Session,
argv: Argv,
source: string,
result: TranscribeResult,
sourceType: T,
{ all = false }
) {
const { session } = argv
const showLegacy = result.length === 1 || all
const showExceptional = result.length === 1 || all || sourceType === "h"
result = result
Expand Down Expand Up @@ -81,19 +83,24 @@ export function apply(ctx: Context, config: Config) {
const source = seg[0].content.map(seg => seg[sourceType]).join("")
const alts = seg.map(alt => {
let line = alt.content.map(seg => seg.v).join("")
if (alt.note)
line += `(${alt.note.replace(/\n/g, ";")})`
if (alt.note) line += `(${alt.note.replace(/\n/g, ";")})`
return line
})
return `${source}:\n${alts.join("\n")}`
})

if (single && footnotes.length === 1) return h.escape(footnotes[0])
if (single && footnotes.length === 1)
return [isSlash(argv) && session.text(".input", [source]), h.escape(footnotes[0])]
.filter(Boolean)
.join("\n")

return [text, footnotes.map((fn, i) => `[${i + 1}] ${fn}`).join("\n")]
.map(s => h.escape(s))
return [
isSlash(argv) && session.text(".input", [source]),
h.escape(text),
h.escape(footnotes.map((fn, i) => `[${i + 1}] ${fn}`).join("\n")),
]
.filter(Boolean)
.join(config.footnotesInSeparateMessage ? "<message />" : "\n")
.join(session.resolve(config.footnotesInSeparateMessage) ? "<message />" : "\n")
}

function getResultScore(result: TranscribeResult) {
Expand All @@ -110,7 +117,8 @@ export function apply(ctx: Context, config: Config) {
checkUnknown: true,
showWarning: true,
})
cmdXdi8.option("all", "-a").action(({ options, session }, els) => {
cmdXdi8.option("all", "-a").action((argv, els) => {
const { options, session } = argv
const text = stripTags(els).replace(/[⁰¹²³⁴⁵⁶⁷⁸⁹]+/g, "")

const hxResult = ctx.xdi8.hanziToXdi8Transcriber.transcribe(text, {
Expand All @@ -124,9 +132,9 @@ export function apply(ctx: Context, config: Config) {

if (!hxScore && !xhScore) return session.text(".no-result")

if (hxScore > xhScore) return stringifyResult(session, hxResult, "h", options)
if (hxScore > xhScore) return stringifyResult(argv, text, hxResult, "h", options)

const xhResultCompact = xhResult.filter(seg => seg !== " ")
return stringifyResult(session, xhResultCompact, "x", options)
return stringifyResult(argv, text, xhResultCompact, "x", options)
})
}
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h } from "koishi"
import { Argv, h } from "koishi"
import type { Alternation } from "xdi8-transcriber"

export const ahoFixes: Record<string, string[]> = {
Expand Down Expand Up @@ -45,3 +45,7 @@ export function stripTags(els: string | h[]) {
.replace(/\ufdd1(?:\s*\ufdd1)*/g, "\n")
)
}

export function isSlash(argv: Argv): boolean {
return argv.session.event.type === "interaction/command"
}

0 comments on commit d40792f

Please sign in to comment.