Last active
February 17, 2025 17:18
Revisions
-
laundmo revised this gist
Feb 17, 2025 . 1 changed file with 28 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,30 +18,44 @@ 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), "]]"), ] @@ -67,11 +81,10 @@ def main(): 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, @@ -86,7 +99,6 @@ def main(): 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) -
laundmo renamed this gist
Feb 12, 2025 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
laundmo revised this gist
Feb 12, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ def multilinefixer(match: re.Match[str]) -> str: (re.compile(r"{% ?enddef ?%}"), "[[return]]"), (re.compile(r"{% ?end.*?%}"), "[[pass]]"), (re.compile(r"{% ?(.*?) ?%}"), r"[[\g<1>]]"), (re.compile(r"\]\](\s+)(?<!\n\n)\[\[(?!end|=..)"), r"\g<1>"), ] -
laundmo revised this gist
Feb 11, 2025 . 1 changed file with 21 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,16 +17,31 @@ import djlint.formatter import djlint.reformat INBRACKETS_ADD = re.compile( r"(^(?!\s*\[\[)[ \t]*)(?!\n)(.*)(?<!\])\]?\]?|\[\[(.*)", re.MULTILINE ) def multilinefixer(match: re.Match[str]) -> str: return INBRACKETS_ADD.sub(r"\g<1>[[\g<2>\g<3>]]", match.group(1)) PY4WEB_TO_DJANGO = [ (re.compile(r"\[\[(.*?)\]\]", flags=re.DOTALL), multilinefixer), (re.compile(r"\[\[ ?def(.*?): ?\]\]"), r"{% def\g<1>:def %}"), (re.compile(r"\[\[ ?end ?\]\]"), "{% endblock %}"), (re.compile(r"\[\[ ?return ?\]\]"), "{% enddef %}"), (re.compile(r"\[\[ ?pass ?\]\]"), "{% end %}"), (re.compile(r"\[\[(.*?)\]\]"), r"{% \g<1> %}"), ] DJANGO_TO_PY4WEB = [ (re.compile(r"{% ?def(.*?):def ?%}"), r"[[def\g<1>:]]"), (re.compile(r"{% ?endblock ?%}"), "[[end]]"), (re.compile(r"{% ?enddef ?%}"), "[[return]]"), (re.compile(r"{% ?end.*?%}"), "[[pass]]"), (re.compile(r"{% ?(.*?) ?%}"), r"[[\g<1>]]"), (re.compile(r"\]\](\s+)(?<!\n\n)\[\[(?!end)"), r"\g<1>"), ] @@ -47,7 +62,7 @@ def main(): filepath: Path = args.filepath with filepath.open(encoding="utf-8") as f: rawcode = f.read() for pat, templ in PY4WEB_TO_DJANGO: @@ -56,15 +71,15 @@ def main(): 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"] = ( @@ -75,7 +90,7 @@ def main(): for pat, templ in DJANGO_TO_PY4WEB: formatted = pat.sub(templ, formatted) with filepath.open("w", encoding="utf-8") as f: f.write(formatted) -
laundmo revised this gist
Feb 3, 2025 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,4 +17,15 @@ Auto-format on save in VSCode: } ] }, ``` Configuring djlint: I have this config in my pyproject.toml - consult djlint docs for which other configs are available ```toml [tool.djlint] format_js = true indent = 2 [tool.djlint.js] indent_size = 2 break_chained_methods = true ``` -
laundmo revised this gist
Feb 3, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ Auto-format on save in VSCode: "commands": [ { "match": ".*\\.html$", "cmd": "\"${workspaceFolder}/.venv/Scripts/python.exe\" \"${workspaceFolder}/template_formatter.py\" \"${file}\"" } ] }, -
laundmo revised this gist
Feb 3, 2025 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ A script which formats py4web templates using DJLint, by partially converting them to Django syntax and back. How to use: 1. Install DJLint: `py -m pip install djlint` 2. Download the script -
laundmo revised this gist
Feb 3, 2025 . 2 changed files with 25 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ How to use: 1. Install DJLint: `py -m pip install djlint` 2. Download the script 3. Run the script with a template file: `py template_formatter.py ./templates/mytemplate.html` Auto-format on save in VSCode: 1. Get the [Run On Save](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) extension 2. If you have a venv, configure it like this. (assumes venv is located in: `./.venv` - if its not, or on another OS, adjust paths accordingly.) ```json "emeraldwalk.runonsave": { "commands": [ { "match": ".*\\.html$", "cmd": "${workspaceFolder}/.venv/Scripts/python.exe ${workspaceFolder}/template_formatter.py ${file}" } ] }, ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,12 @@ """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 -
laundmo created this gist
Feb 3, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,172 @@ """Formats py4web templates It does this by converting their syntax to Django using djlint to format, and converting them back""" import argparse import re import typing from pathlib import Path import djlint import djlint.formatter import djlint.reformat PY4WEB_TO_DJANGO = [ (re.compile(r"\[\[ ?return ?\]\]"), "{% enddef %}"), (re.compile(r"\[\[ ?pass ?\]\]"), "{% end %}"), (re.compile(r"\[\[(.*?)\]\]"), r"{% \g<1> %}"), ] DJANGO_TO_PY4WEB = [ (re.compile(r"{% ?enddef ?%}"), "[[return]]"), (re.compile(r"{% ?end.*?%}"), "[[pass]]"), (re.compile(r"{% ?(.*?) ?%}"), r"[[\g<1>]]"), ] 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", newline="") 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", blank_line_after_tag="from,include,import", ) # 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["break_chained_methods"] and config.js_config["wrap_line_length"] == 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", newline="") 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()