prettier / plugin-pug

Prettier Pug Plugin
https://prettier.github.io/plugin-pug
MIT License
194 stars 44 forks source link

pugFramework: jinja2 #494

Closed mashedmonday closed 2 weeks ago

mashedmonday commented 3 weeks ago

Request / Idea

I use pug(js) in m python projects (from back in the days when it was called jade). However, i cannot apply the formatter to my pug templates right away because they come with jinja2 specific content. Jinja2 is a templating engine used alot in python. see example code below

Input

doctype html
html
 head
  meta(charset='utf-8')
  meta(http-equiv="content-language" content="en")
  meta(http-equiv='X-UA-Compatible' content='IE=edge')
  meta(name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no')
  link#favicon(rel='icon' type='image/x-icon' href='/static/img/favicon.ico')
  link(href="{{ url_for('static', path='css/main.css') }}" rel='stylesheet')
  title Site

 body(hx-ext="client-side-templates")

  // THIS PART IS WHERE THE TROUBLE BEGINS!!!!!!!!!!!!!!!!!
  {% include "navbar.pug" %}
  {% block content %}{% endblock %}
  {% include "footer.pug" %}

 script(src="{{ url_for('static', path='js/main.js') }}")
{% block javascript %}
{% endblock %}

Expected Output

The code above, really. but little more consistent style. The jinja2 tags must remain and even need to be indented accordingly.

Additional Context

mashedmonday commented 3 weeks ago

Maybe this comes handy: https://github.com/davidodenwald/prettier-plugin-jinja-template

Shinigami92 commented 3 weeks ago

Please provide a PR, because I wont create a PR

However, I did not touch the project for a while, so I might at least update the devDependencieswhen I have time

OiYouYeahYou commented 3 weeks ago

Sorry, but I'm erring to recommend this should be a won't fix 😟

On a cursory look, Jinja is a string templating tool and, as far as I understand, it only processes its own syntax before outputting a string. On the other hand, Pug enforces specific syntax rules and doesn't tolerate unexpected symbols.

This plugin relies directly on the Pug parser, which expects correct Pug syntax. Modifying the plugin to support additional syntax's (like: Jinja, Handlebars, etc) would be a significant undertaking.


As a side note: I have concerns with the example you've provided. I would recommend against your current set up and use Pug exclusively. All of the Jinja syntax you've provided is supported out of the box with Pug.

Shinigami92 commented 3 weeks ago

@OiYouYeahYou This aligns with my internal thoughts 👍

Only if @mashedmonday (or another unknown contributor) is willing to put effort into this and it does not bring breaking changes or any conflicts with it, then I consider it as mergable. Otherwise I'm near to a "wont fix" / "close as not planned".

mashedmonday commented 2 weeks ago

I am not aware i can use pug without jinja2 in python, but i will take a look. Thanks for your feedback still.

OiYouYeahYou commented 2 weeks ago

@mashedmonday just remove the percents and curlies, and you've got valid pug

doctype html
html
 body(hx-ext="client-side-templates")

  include "navbar.pug"
  block content
  include "footer.pug"

 block javascript

Ref:

mashedmonday commented 2 weeks ago

@OiYouYeahYou thanks for making this clear. I had to change some in may code due to different include searchpaths, but now this seems to work great.

Thanks a ton. Closing ticket.