Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/docs request: allow setting escape="none" in askama.toml #1060

Open
sxlijin opened this issue Jun 6, 2024 · 2 comments
Open

Feature/docs request: allow setting escape="none" in askama.toml #1060

sxlijin opened this issue Jun 6, 2024 · 2 comments

Comments

@sxlijin
Copy link

sxlijin commented Jun 6, 2024

We're using askama for codegen (thanks for the wonderful work!) and all of our templates are instantiated with #[template(path = ..., escape = "none)]. It would be nice if we could just set a blanket escape = "none" in askama.toml.

I suspect that, given askama supports custom escapers, which are much more complex, this is possible - but looking at https://djc.github.io/askama/configuration.html#custom-syntaxes I have no idea how to actually do this.

static DEFAULT_ESCAPERS: &[(&[&str], &str)] = &[
(&["html", "htm", "svg", "xml"], "::Html"),
(&["md", "none", "txt", "yml", ""], "::Text"),
(&["j2", "jinja", "jinja2"], "::Html"),
];
suggests that default_escapers is a 2-tuple of file-extension-list to rust-escaper-function - but it's unclear to me how this would be modeled in askama.toml.

@sxlijin sxlijin changed the title Allow setting escape="none" in askama.toml Feature/docs request: allow setting escape="none" in askama.toml Jun 6, 2024
@djc
Copy link
Owner

djc commented Jun 7, 2024

I think you could do it like this?

[[escaper]]
path = "::askama::Text"
extensions = ["rs"] # Add whatever extensions you need here

Internally, escapers is a Vec<(HashSet<String>, String)>, where the HashSet represents a set of extensions, and the Vec is searched in order with configured escapers coming before the default escapers.

Please try it, and if it works it would be great if you can submit a docs PR!

@sxlijin
Copy link
Author

sxlijin commented Jul 3, 2024

Thanks for the quick reply, and sorry for the slow response- this is very far off the daily work track, so it's hard to make time for this.

Unfortunately this doesn't appear to work; when using this with a client.ts.j2 template:

❯ g d ../../engine/language-client-codegen/askama.toml
diff --git a/engine/language-client-codegen/askama.toml b/engine/language-client-codegen/askama.toml
index 8c040ea1..2d296484 100644
--- a/engine/language-client-codegen/askama.toml
+++ b/engine/language-client-codegen/askama.toml
@@ -7,3 +7,7 @@ dirs = [
 # whitespace can be either preserve, suppress, or minimize
 # suppress and minimize are both too aggressive for us
 whitespace = "preserve"
+
+[[escaper]]
+path = "::askama::Text"
+extensions = ["j2"]

I get this:

❯ g d baml_client/client.ts
diff --git a/integ-tests/typescript/baml_client/client.ts b/integ-tests/typescript/baml_client/client.ts
index 4d777d02..5041c9f2 100644
--- a/integ-tests/typescript/baml_client/client.ts
+++ b/integ-tests/typescript/baml_client/client.ts
@@ -19,6 +19,8 @@ import { BamlRuntime, FunctionResult, BamlCtxManager, BamlStream, Image } from "
 import {Blah, ClassOptionalOutput, ClassOptionalOutput2, ClassWithImage, DynInputOutput, DynamicClassOne, DynamicClassTwo, DynamicOutput, Education, Email, Event, FakeImage, InnerClass, InnerClass2, NamedArgsSingleClass, OptionalTest_Prop1, OptionalTest_ReturnType, OrderInfo, Person, RaysData, ReceiptInfo, ReceiptItem, Resume, SearchParams, SomeClassNestedDynamic, TestClassAlias, TestClassNested, TestClassWithEnum, TestOutputClass, UnionTest_ReturnType, WithReasoning, Category, Category2, Category3, Color, DataType, DynEnumOne, DynEnumTwo, EnumInClass, EnumOutput, Hobby, NamedArgsSingleEnum, NamedArgsSingleEnumList, OptionalTest_CategoryType, OrderStatus, Tag, TestEnum} from "./types"
 import TypeBuilder from "./type_builder"

+&lt;div&gt;hello&lt;/div&gt;
+
 export type RecursivePartialNull<T> = T extends object
   ? {
       [P in keyof T]?: RecursivePartialNull<T[P]>;

If I find time to drill into a minimum repro, I'll let you know.

(Aside: I really hate the lack of template filename suffix standardization. I've personally standardized every codebase I use on filename.$generated-ext.j2 but this seems... not so common?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants