|
"""Formats py4web templates |
|
It does this by converting their syntax to Django |
|
using djlint to format, and converting them back |
|
|
|
MIT license: |
|
Copyright 2025 laund |
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.""" |
|
|
|
import argparse |
|
import re |
|
import typing |
|
from pathlib import Path |
|
|
|
import djlint |
|
import djlint.formatter |
|
import djlint.reformat |
|
|
|
INBRACKETS_ADD = re.compile( |
|
r"(?P<indent>^[ \t]*)(?!\n)(?P<open>\[\[)?(?P<text>.*)(?<!\])(?P<close>\]?\]?)", |
|
re.MULTILINE, |
|
) |
|
|
|
|
|
def multiline_fixer_inner(match: re.Match[str]) -> str: |
|
gd = match.groupdict() |
|
|
|
opening = "{%" if gd["open"] else "{%-" |
|
closing = "%}" if gd["close"] else "-%}" |
|
|
|
return f"{gd['indent']}{opening} {gd['text']} {closing}" |
|
|
|
|
|
def multilinefixer(match: re.Match[str]) -> str: |
|
inp = match.group() |
|
res = INBRACKETS_ADD.sub(multiline_fixer_inner, inp) |
|
return res |
|
|
|
|
|
PY4WEB_TO_DJANGO = [ |
|
(re.compile(r"\[\[(.*?)\]\]", flags=re.DOTALL), multilinefixer), |
|
(re.compile(r"\[\[(-?) ?def(.*?): ?(-?)\]\]"), r"{%\1 def\2:def \3%}"), |
|
(re.compile(r"{%(-?) ?end ?(-?)%}"), r"{%\1 endblock \2%}"), |
|
(re.compile(r"{%(-?) ?return ?(-?)%}"), r"{%\1 enddef \2%}"), |
|
(re.compile(r"{%(-?) ?pass ?(-?)%}"), r"{%\1 end \2%}"), |
|
] |
|
|
|
DJANGO_TO_PY4WEB = [ |
|
(re.compile(r"{%(-?) ?endblock ?(-?)%}"), r"{%\1 end \2%}"), |
|
(re.compile(r"{%(-?) ?enddef ?(-?)%}"), r"{%\1 return \2%}"), |
|
(re.compile(r"{%(-?) ?end ?(-?)%}"), r"{%\1 pass \2%}"), |
|
(re.compile(r"({%-? ?def.*?):def( ?-?%})"), r"\1:\2"), |
|
(re.compile(r"{%- ?"), ""), |
|
(re.compile(r" ?-%}"), ""), |
|
(re.compile(r"{% ?(.*?) ?%}", flags=re.DOTALL), r"[[\1]]"), |
|
(re.compile(r"(^\s)*{% ?", flags=re.MULTILINE), r"\1[["), |
|
(re.compile(r" ?%}\s*$", flags=re.MULTILINE), "]]"), |
|
] |
|
|
|
|
|
def file_arg(arg: str): |
|
path = Path(arg) |
|
if not path.exists(): |
|
raise ValueError("Path not found") |
|
if path.is_dir(): |
|
raise ValueError("Path is a folder") |
|
return path |
|
|
|
|
|
def main(): |
|
inject_jschain() # monkey-patch PR: https://github.com/beautifier/js-beautify/pull/2344 |
|
parser = argparse.ArgumentParser(description="Format py4web templates") |
|
parser.add_argument("filepath", type=file_arg, help="Path to file") |
|
args = parser.parse_args() |
|
|
|
filepath: Path = args.filepath |
|
|
|
with filepath.open(encoding="utf-8") as f: |
|
rawcode = f.read() |
|
|
|
for pat, templ in PY4WEB_TO_DJANGO: |
|
rawcode = pat.sub(templ, rawcode) |
|
config = djlint.Config( |
|
"-", |
|
custom_blocks="def", |
|
blank_line_before_tag="def,block", |
|
blank_line_after_tag="from,include,import,extend,return", |
|
) |
|
# if someone set break_chained_methods without wrap_line_length, |
|
# set wrap_line_length here for the monkey-patched method-chain wrapping |
|
# code to work. |
|
if ( |
|
config.js_config.get("break_chained_methods") |
|
and config.js_config.get("wrap_line_length", 0) == 0 |
|
): |
|
# if not explicitly set, assume js will be indented 2 indents |
|
config.js_config["wrap_line_length"] = ( |
|
config.max_line_length - config.indent_size * 2 |
|
) |
|
formatted = djlint.reformat.formatter(config, rawcode) |
|
for pat, templ in DJANGO_TO_PY4WEB: |
|
formatted = pat.sub(templ, formatted) |
|
|
|
with filepath.open("w", encoding="utf-8") as f: |
|
f.write(formatted) |
|
|
|
|
|
def inject_jschain(): |
|
"""monkey-path jsbeautifier for better function-chain formatting |
|
specifically, this breaks all calls in function-chains if any one of them would |
|
need to be broken to stay in line length""" |
|
# might break with djlint/jsbeautifier update, in that case just keep old behaviour. |
|
try: |
|
from jsbeautifier.javascript.beautifier import ( |
|
TOKEN, |
|
Beautifier, |
|
_special_word_set, |
|
reserved_array, |
|
) |
|
|
|
_peek_lloc_stop = [TOKEN.SEMICOLON, TOKEN.END_BLOCK, TOKEN.EOF, TOKEN.RESERVED] |
|
|
|
@typing.no_type_check |
|
def guess_current_lloc_len(self: Beautifier): |
|
"""Try to guess how long, in chars, this lloc would be if it was condensed to a single line.""" |
|
|
|
# empty string is the "separator" between forwards and backwards |
|
txts = [""] |
|
|
|
# lookbehind loop |
|
while True: |
|
bak_token = self._tokens.peek(-len(txts)) |
|
if bak_token is None: |
|
break |
|
if bak_token.type in [*_peek_lloc_stop, TOKEN.START_BLOCK]: |
|
break |
|
txts.append(bak_token.text) |
|
|
|
# since we looped backwards, reverse the gathered texts here |
|
txts = list(reversed(txts)) |
|
|
|
bak_tokens = len(txts) |
|
# lookahead loop |
|
while True: |
|
fwd_token = self._tokens.peek(len(txts) - bak_tokens) |
|
if fwd_token is None: |
|
break |
|
if fwd_token.type in _peek_lloc_stop: |
|
break |
|
txts.append(fwd_token.text) |
|
|
|
line = "".join(txts) |
|
# print(len(line), line) |
|
|
|
cur_line = self._output.current_line.toString() |
|
indent_chars = len(cur_line) - len(cur_line.lstrip()) |
|
return len(line) + indent_chars |
|
|
|
@typing.no_type_check |
|
def handle_dot(self: Beautifier, current_token): |
|
if self.start_of_statement(current_token): |
|
# The conditional starts the statement if appropriate. |
|
pass |
|
else: |
|
self.handle_whitespace_and_comments(current_token, True) |
|
|
|
if re.search("^([0-9])+$", self._flags.last_token.text): |
|
self._output.space_before_token = True |
|
|
|
if reserved_array(self._flags.last_token, _special_word_set): |
|
self._output.space_before_token = False |
|
else: |
|
# allow preserved newlines before dots in general |
|
# force newlines on dots after close paren when break_chained - for |
|
# bar().baz() |
|
self.allow_wrap_or_preserved_newline( |
|
current_token, |
|
( |
|
0 |
|
< self._options.wrap_line_length |
|
< guess_current_lloc_len(self) |
|
or self._flags.last_token.text == ")" |
|
) |
|
and self._options.break_chained_methods, |
|
) |
|
|
|
# Only unindent chained method dot if this dot starts a new line. |
|
# Otherwise the automatic extra indentation removal |
|
# will handle any over indent |
|
if ( |
|
self._options.unindent_chained_methods |
|
and self._output.just_added_newline() |
|
): |
|
self.deindent() |
|
|
|
self.print_token(current_token) |
|
|
|
Beautifier.handle_dot = handle_dot |
|
except Exception as e: |
|
print(f"didn't inject js-beautifier due to: {e}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |