Skip to content

Commit 49cfefe

Browse files
committed
Sharing (WIP) and listing unsupported engines
1 parent 1d26dfa commit 49cfefe

File tree

19 files changed

+320
-58
lines changed

19 files changed

+320
-58
lines changed

package-lock.json

Lines changed: 44 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"dependencies": {
33
"bootstrap": "^5.3.3",
44
"fetch-jsonp": "^1.3.0",
5-
"next": "^14.2.13",
5+
"next": "^14.2.14",
66
"react": "^18",
77
"react-dom": "^18",
88
"react-icons": "^5.3.0"

src/app/advanced/[engine]/index.html/TestForm.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default function TestForm({ engine }: TestFormProps) {
2929
const form = event.currentTarget;
3030
const formData = new FormData(form);
3131
const localInput: TestInput = {
32+
engine: engine.handle,
3233
regex: formData.get('regex') as string,
3334
replacement: formData.get('replacement') as string,
3435
option: formData.getAll('option') as string[],

src/app/advanced/[engine]/index.html/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export async function generateMetadata({ params }: { params: { engine: string }
1111
}
1212

1313
return {
14-
title: `Test your ${engine.short_name} regular expression - RegexPlanet`,
15-
description: `Online testing for ${engine.short_name} regular expressions.`,
14+
title: `${engine.short_name} regex testing - RegexPlanet`,
15+
description: `Online testing for ${engine.short_name} (${engine.description}) regular expressions.`,
1616
}
1717
}
1818

src/app/advanced/[engine]/results.html/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export async function GET(
3030
}
3131

3232
const testInput = {
33+
engine: engine.handle,
3334
regex: "test",
3435
replacement: "test",
3536
option: [],
@@ -69,6 +70,7 @@ export async function POST(
6970
const rawData = await request.formData();
7071

7172
const testInput = {
73+
engine: engine.handle,
7274
regex: (rawData.get("regex") || "") as string,
7375
replacement: (rawData.get("replacement") || "") as string,
7476
option: (rawData.getAll("options") || "") as string[],

src/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function Home() {
3939
<div className="card-body pt-1">
4040
Share codes are an easy way to share regexes. If you have a share code, enter it here:
4141
<div className="pt-2 d-flex justify-content-center">
42-
<form action="/share/index.html" className="form-inline" method="post">
42+
<form action="/share/index.html" className="form-inline" method="get">
4343
<div className="input-group">
4444
<input type="text" className="form-control" name="share" placeholder="Share code" />
4545
<button type="submit" className="btn btn-primary">Go</button>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { TestInput } from '@/types/TestInput';
2+
import { SubmitButton } from '@/components/SubmitButton';
3+
import { getWorkingEngine, getWorkingEngineOrThrow } from '@/engines';
4+
5+
type PreviewRegexProps = {
6+
theShare: TestInput;
7+
}
8+
9+
export function PreviewRegex( {theShare}: PreviewRegexProps) {
10+
const engineCode = theShare.engine;
11+
let theEngine = getWorkingEngine(engineCode);
12+
if (!theEngine) {
13+
theEngine = getWorkingEngineOrThrow("java");
14+
}
15+
return (
16+
<>
17+
<form action={`/advanced/${theEngine.handle}/index.html`} className="" method="post">
18+
<div className="mb-3">
19+
<label htmlFor="regex" className="form-label">Regular Expression</label>
20+
<input type="text" className="form-control" id="regex" name="regex" defaultValue={theShare.regex} />
21+
</div>
22+
<div className="mb-3">
23+
<label htmlFor="replacement" className="form-label">Replacement</label>
24+
<input type="text" className="form-control" id="replacement" name="replacement" defaultValue={theShare.replacement} />
25+
</div>
26+
<SubmitButton>{`Test with ${theEngine.short_name}`}</SubmitButton>
27+
</form>
28+
<details className="mt-3"><summary>Raw data</summary><pre>{JSON.stringify(theShare, null, 2)}</pre></details>
29+
</>
30+
)
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Link from "next/link";
2+
3+
type ShareFormProps = {
4+
shareCode?: string;
5+
}
6+
7+
8+
export function ShareForm(props: ShareFormProps) {
9+
10+
return (
11+
<div className="d-flex justify-content-center">
12+
<form action="/share/index.html" className="col-8 col-lg-4 border p-3" method="get">
13+
<div className="mb-3">
14+
<label htmlFor="legacy" className="form-label">Share Code</label>
15+
<input type="text" className="form-control" id="share" name="share" defaultValue={props.shareCode} />
16+
</div>
17+
<button type="submit" className="btn btn-primary">Submit</button>
18+
<Link className="btn btn-outline-primary ms-2" href="/">Cancel</Link>
19+
</form>
20+
</div>
21+
)
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { TestInput } from "@/types/TestInput";
2+
3+
export type ShareFormState = {
4+
shareCode?: string;
5+
message?: string;
6+
messageType?: string;
7+
regex?: TestInput;
8+
};

src/app/share/index.html/page.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Metadata } from 'next';
2+
3+
import { cleanupSearchParam } from '@/functions/cleanupSearchParam';
4+
import { ShareFormState } from './ShareFormState';
5+
import { ShareForm } from './ShareForm';
6+
import { PreviewRegex } from './PreviewRegex';
7+
8+
export const metadata: Metadata = {
9+
title: "Sharing - RegexPlanet",
10+
};
11+
12+
async function lookupShareCode(shareCode: string): Promise<ShareFormState> {
13+
14+
if (!shareCode) {
15+
return {
16+
message: "Please enter a share code",
17+
messageType: "info",
18+
};
19+
}
20+
//shareCode = "yyyyfud6z4r";
21+
const response = await fetch(`https://www.regexplanet.com/share/index.json?share=${shareCode}`);
22+
const data = await response.json();
23+
console.log(`server response=${JSON.stringify(data)}`);
24+
if (data.success) {
25+
return {
26+
message: `Share code ${shareCode} found!`,
27+
messageType: 'success',
28+
shareCode,
29+
regex: data.recipe,
30+
};
31+
}
32+
return {
33+
message: `Share code ${shareCode} not found`,
34+
messageType: 'danger',
35+
shareCode,
36+
};
37+
}
38+
39+
export default async function Page({ searchParams }: { searchParams: { [key: string]: string | string[] | undefined } })
40+
{
41+
const shareCode = cleanupSearchParam(searchParams["share"]);
42+
const shareFormState = await lookupShareCode(shareCode);
43+
44+
return (
45+
<>
46+
<h1>Sharing</h1>
47+
{ shareFormState.message ? <div className={`alert alert-${shareFormState.messageType || "info"}`}>{shareFormState.message}</div> : null }
48+
{ shareFormState.regex
49+
? <PreviewRegex theShare={shareFormState.regex} />
50+
: <ShareForm shareCode={shareCode} />
51+
}
52+
</>
53+
);
54+
}

src/components/SubmitButton.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use client';
2+
3+
import { useFormStatus } from 'react-dom';
4+
5+
export function SubmitButton({ children }: { children: React.ReactNode }) {
6+
const { pending } = useFormStatus();
7+
8+
return (
9+
<button className="btn btn-primary" type="submit" aria-disabled={pending}>
10+
{children}
11+
</button>
12+
);
13+
}

src/engines/dotnet.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { RegexEngine } from "./RegexEngine";
2+
3+
export const dotnet: RegexEngine = {
4+
description: "System.Text.RegularExpressions.Regex",
5+
enabled: true,
6+
help_label: "MSDN",
7+
help_url:
8+
"https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions?view=net-8.0",
9+
handle: "dotnet",
10+
level: "alpha",
11+
links: {
12+
"Learn .NET regular expressions":
13+
"https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions",
14+
"Quick Reference":
15+
"https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions#reference",
16+
"Best Practices":
17+
"https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-regex",
18+
" Behavior details":
19+
"https://learn.microsoft.com/en-us/dotnet/standard/base-types/details-of-regular-expression-behavior",
20+
},
21+
logo_icon: "https://www.vectorlogo.zone/logos/dotnet/dotnet-icon.svg",
22+
logo_ar21: "https://www.vectorlogo.zone/logos/dotnet/dotnet-ar21.svg",
23+
options: [],
24+
short_name: ".NET",
25+
};

0 commit comments

Comments
 (0)