Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix: allow sheet closing text in SVS recipes (#377)

Chi-Wei Leeef62b07e155b
diff --git a/packages/svs/src/parser.ts b/packages/svs/src/parser.ts
index db9d3416..e1fce3cc 100644
--- a/packages/svs/src/parser.ts
+++ b/packages/svs/src/parser.ts
@@ -241,16 +241,12 @@ export function parseSvs(sourceName: string, source: string): ParsedSvsSheet {
if (openEnd < 0) fail(sourceName, "SVS_ROOT", "Opening <sheet> is not closed.", cursor);
const attributes = parseAttributes(sourceName, text.slice(cursor + 6, openEnd), cursor + 6);
cursor = openEnd + 1;
- const close = text.indexOf("</sheet>", cursor);
- if (close < 0) fail(sourceName, "SVS_ROOT_UNCLOSED", "Source is missing </sheet>.", source.length);
- if (text.slice(close + "</sheet>".length).trim().length > 0) {
- fail(sourceName, "SVS_TRAILING", "Only trivia may follow </sheet>.", close + "</sheet>".length);
- }
const recipes: ParsedSvsRecipe[] = [];
const paths = new Set<string>();
- while (cursor < close) {
+ while (true) {
cursor = skipSpace(text, cursor);
- if (cursor >= close) break;
+ if (cursor >= text.length) fail(sourceName, "SVS_ROOT_UNCLOSED", "Source is missing </sheet>.", source.length);
+ if (text.startsWith("</sheet>", cursor)) break;
const match = RULE.exec(text.slice(cursor));
if (!match) fail(sourceName, "SVS_RULE", "Expected a dotted recipe path.", cursor);
const path = match[0];
@@ -261,8 +257,8 @@ export function parseSvs(sourceName: string, source: string): ParsedSvsSheet {
cursor = skipSpace(text, cursor);
if (text[cursor] !== "{") fail(sourceName, "SVS_RULE_OPEN", `Recipe ${path} requires '{'.`, cursor);
const blockStart = cursor + 1;
- const blockEnd = closingBrace(text, blockStart, close);
- if (blockEnd < 0 || blockEnd > close) fail(sourceName, "SVS_RULE_UNCLOSED", `Recipe ${path} is not closed.`, start);
+ const blockEnd = closingBrace(text, blockStart, text.length);
+ if (blockEnd < 0) fail(sourceName, "SVS_RULE_UNCLOSED", `Recipe ${path} is not closed.`, start);
const parsedProperties = parseProperties(sourceName, text.slice(blockStart, blockEnd), blockStart);
const value: SvsRecipe = {
@@ -272,6 +268,8 @@ export function parseSvs(sourceName: string, source: string): ParsedSvsSheet {
recipes.push({ value, range: { start, end: blockEnd + 1 }, properties: parsedProperties.parsed });
cursor = blockEnd + 1;
}
+ const end = cursor + "</sheet>".length;
+ if (text.slice(end).trim().length > 0) fail(sourceName, "SVS_TRAILING", "Only trivia may follow </sheet>.", end);
return {
...(attributes.id === undefined ? {} : { id: attributes.id }),
recipes,
diff --git a/packages/svs/test/source-closure.test.ts b/packages/svs/test/source-closure.test.ts
index c0b6391d..9273ac41 100644
--- a/packages/svs/test/source-closure.test.ts
+++ b/packages/svs/test/source-closure.test.ts
@@ -313,6 +313,19 @@ test("quoted Recipe values may contain Prompt punctuation without changing SVS s
assert.equal(sheet.recipes[0]?.value.properties.text, "first; second } /* literal */");
});
+test("quoted Recipe text may contain a sheet closing tag", () => {
+ const prompt = "Show a literal </sheet> tag in the output";
+ const source = `<sheet version="1">demo.prompt { text: ${formatSvsValue(prompt)}; }</sheet>`;
+ const sheet = parseSvs("prompt.svs", source);
+ assert.equal(sheet.recipes[0]?.value.properties.text, prompt);
+ assert.throws(() => parseSvs("trailing.svs", `${source} extra`),
+ (error: unknown) => error instanceof SvsSyntaxError && error.code === "SVS_TRAILING");
+ assert.throws(() => parseSvs("twice.svs", `${source}</sheet>`),
+ (error: unknown) => error instanceof SvsSyntaxError && error.code === "SVS_TRAILING");
+ assert.throws(() => parseSvs("unclosed.svs", source.replace(/<\/sheet>$/u, "")),
+ (error: unknown) => error instanceof SvsSyntaxError && error.code === "SVS_ROOT_UNCLOSED");
+});
+
test("SVS parses and serializes canonical arrays and objects as one Recipe value", () => {
const source = `<sheet version="1">
ranking.column {
diff --git a/packages/text/test/text.test.ts b/packages/text/test/text.test.ts
index 04e1c02b..28ed3cf0 100644
--- a/packages/text/test/text.test.ts
+++ b/packages/text/test/text.test.ts
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import { parseStructuredElement } from "@hypit/markup";
import type { SurfaceResolvedReference } from "@hypit/markup";
-import { svsRecipeType } from "@hypit/svs";
+import { formatSvsValue, parseSvs, svsRecipeType } from "@hypit/svs";
import {
bindText,
@@ -15,9 +15,21 @@ import {
sealTextBindings,
sealTextTemplate,
textTypes,
+ textTemplateFromSvsRecipes,
verifyTextTemplate,
} from "@hypit/text";
+test("Text Templates keep a sheet closing tag in prompt text", () => {
+ const prompt = "Show the literal </sheet> tag in the video";
+ const source = `<sheet version="1">
+ text-template.demo {}
+ text-template.demo.block.example { kind: fixed; order: 0; text: ${formatSvsValue(prompt)}; }
+ </sheet>`;
+ const recipes = parseSvs("template.svs", source).recipes.map((recipe) => recipe.value);
+ const template = textTemplateFromSvsRecipes(recipes, "demo");
+ assert.equal(renderText(template, sealTextBindings({})).value, prompt);
+});
+
test("text programs compose nested templates, choices, lists and transforms", () => {
const template = sealTextTemplate({