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

[Question] Changing delimiters? #121

Closed
lukehsiao opened this issue Sep 25, 2018 · 10 comments
Closed

[Question] Changing delimiters? #121

lukehsiao opened this issue Sep 25, 2018 · 10 comments

Comments

@lukehsiao
Copy link

lukehsiao commented Sep 25, 2018

With the template delimiters as {{ }} {%, etc., is there a way to use askama in content that heavily uses these curly braces and %? For example, if I wanted to use askama to template a LaTeX section like this:

Trying to template name.last

\newbibmacro{name:bold_author}{%
  \ifthenelse{\equal{\namepartfamily}{ {{name.last}} }}%
    {\textbf{\ifblank{\namepartgiven}{}{\namepartgiveni\space}\namepartfamily}}%
    {\ifblank{\namepartgiven}{}{\namepartgiveni\space}\namepartfamily}%
  \ifthenelse{\value{listcount}=1 \AND\value{liststop}=2}%
    {\space and\space}
    {\ifthenelse{\value{listcount}<\value{liststop}}%
      {\addcomma\space}
      {}
    }
}

I would assume that this would fail due to the conflicting use of {} and %.

Using jinja's options in Python, I am able to switch delimiters:

block_start_string='~<',
block_end_string='>~',
variable_start_string='<<',
variable_end_string='>>',
comment_start_string='<#',
comment_end_string='#>',

which allows me to make a template that looks like the following and parses correctly:

\newbibmacro{name:bold_author}{%
  \ifthenelse{\equal{\namepartfamily}{ <<name.last>> }%
    {\textbf{\ifblank{\namepartgiven}{}{\namepartgiveni\space}\namepartfamily}}%
    {\ifblank{\namepartgiven}{}{\namepartgiveni\space}\namepartfamily}%
  \ifthenelse{\value{listcount}=1 \AND\value{liststop}=2}%
    {\space and\space}
    {\ifthenelse{\value{listcount}<\value{liststop}}%
      {\addcomma\space}
      {}
    }
}

Is similar functionality possible in askama, or am I stuck? Is it impossible to use askama in templates that already use curly braces and percentage signs?

@djc
Copy link
Collaborator

djc commented Sep 25, 2018

It is possible, but it has not been implemented. Since a few releases there is a per-crate configuration file that we could stick the configuration in, but wiring that up to the parser would need some work.

Would you be interested in working on that? If not, maybe @botika would like to help?

@zzau13
Copy link
Contributor

zzau13 commented Sep 25, 2018

Nice to help! You can assign it to me now.

@djc djc assigned djc and unassigned djc Sep 25, 2018
@djc
Copy link
Collaborator

djc commented Sep 25, 2018

I can't assign it to you, but feel free to take it. 👍 The trick here will be to figure out how to parametrize the parser's input type with the configuration.

@zzau13
Copy link
Contributor

zzau13 commented Sep 25, 2018

I've seen it xD, anything that I do not see clearly I mention it for gitter.

@djc
Copy link
Collaborator

djc commented Sep 25, 2018

Your work so far is great, I have confidence you can figure out most of it! Much appreciated.

@djc
Copy link
Collaborator

djc commented Oct 8, 2018

This has been implemented, thanks to @botika for getting it done!

@djc djc closed this as completed Oct 8, 2018
@lukehsiao
Copy link
Author

This looks great, thanks for considering this feature addition. I'm excited to try it out in one of my projects.

@lukehsiao
Copy link
Author

lukehsiao commented Oct 16, 2018

@botika

I'm getting the following error when trying to set a custom syntax:

thread 'main' panicked at 'bad delimiters block_start: ~<, comment_start: <#, expr_start: <<, needs one of the two characters in common', /home/lukehsiao/.cargo/registry/src/github.com-1ecc6299db9ec823/askama_shared-0.7.2/src/lib.rs:151:13

My askama.toml contains

[general]
# Directories to search for templates, relative to the crate root.
dirs = ["templates"]

[[syntax]]
name = "latex"
block_start = "~<"
block_end = ">~"
comment_start = "<#"
comment_end = "#>"
expr_start = "<<"
expr_end = ">>"

I'm left a little confused since the error suggests a character must be shared, which in my case is <. Do you have any pointers?

Update: Nevermind, I found my issue in the docs:

Values must be 2 characters long and start delimiters must all start with the same character. If a key is omitted, the value from the default syntax is used.

As error message mentioning the start or ending with the same character would be helpful in that it is more specific than just sharing a character.

Thanks again for implementing this.

@lukehsiao
Copy link
Author

lukehsiao commented Oct 16, 2018

Given multiple custom syntaxes like in the docs examples:

[general]
default_syntax = "foo"

[[syntax]]
name = "foo"
block_start = "%{"
comment_start = "#{"
expr_end = "^^"

[[syntax]]
name = "bar"
block_start = "%%"
block_end = "%%"
comment_start = "%#"
expr_start = "%{"

Is there an example or documentation of how to use different syntax for different template files?

Update: And once again, I should just read the docs more carefully:

syntax (as syntax = "foo"): set the syntax name for a parser defined in the configuration file. The default syntax , "default", is the one provided by Askama.

Looks like I have everything I need. Thanks!

@djc
Copy link
Collaborator

djc commented Oct 16, 2018

@lukehsiao glad you managed to figure things out! I'll see if we can improve the error message.

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

3 participants