Skip to content

Commit

Permalink
fix: remove dedent extension
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Mar 24, 2021
1 parent 174911e commit ab0797a
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 52 deletions.
21 changes: 1 addition & 20 deletions beet/toolchain/template.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
__all__ = [
"TemplateError",
"TemplateManager",
"DedentExtension",
]


from contextlib import contextmanager
from textwrap import dedent
from typing import Any, Dict, List, Optional, TypeVar

from jinja2 import (
Expand All @@ -19,8 +17,7 @@
PrefixLoader,
)
from jinja2.ext import DebugExtension # type: ignore
from jinja2.ext import ExprStmtExtension, Extension, LoopControlExtension, WithExtension
from jinja2.lexer import Token
from jinja2.ext import ExprStmtExtension, LoopControlExtension, WithExtension
from jinja2.loaders import BaseLoader

from beet.core.file import TextFileBase
Expand Down Expand Up @@ -68,7 +65,6 @@ def __init__(self, templates: List[FileSystemPath], cache_dir: FileSystemPath):
ExprStmtExtension,
LoopControlExtension,
WithExtension,
DedentExtension,
],
)

Expand Down Expand Up @@ -107,18 +103,3 @@ def error_handler(self, message: str):
exc = exc.with_traceback(tb)
tb = tb.tb_next
raise TemplateError(message) from exc


class DedentExtension(Extension):
"""Extension that removes indention from templates."""

def filter_stream(self, stream: Any):
lineno = 0
for token in stream:
if token.type == "data":
prefix, newline, value = token.value.partition("\n")
if token.lineno > lineno:
prefix = dedent(prefix)
token = Token(token.lineno, "data", prefix + newline + dedent(value))
yield token
lineno = token.lineno
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

function #demo:thing
function #demo:thing

Original file line number Diff line number Diff line change
@@ -1 +1 @@
say this is an implementation detail
say this is an implementation detail
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
say begin inner
say begin inner

say calling demo:nested_0
function demo:nested_0
say calling demo:nested_0
function demo:nested_0

say calling demo:nested_1
function demo:nested_1
say calling demo:nested_1
function demo:nested_1

say calling demo:nested_2
function demo:nested_2
say calling demo:nested_2
function demo:nested_2

say calling demo:nested_3
function demo:nested_3
say calling demo:nested_3
function demo:nested_3

say calling demo:nested_4
function demo:nested_4
say calling demo:nested_4
function demo:nested_4

say end inner
say end inner
Original file line number Diff line number Diff line change
@@ -1 +1 @@
say from nested 0
say from nested 0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
say from nested 1
say from nested 1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
say from nested 2
say from nested 2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
say from nested 3
say from nested 3
Original file line number Diff line number Diff line change
@@ -1 +1 @@
say from nested 4
say from nested 4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
say hello Steve
say hello Steve
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
say 0
say 1
say 2
say 3
say 4
say 5
say 6
say 7
say 8
say 9
say 0
say 1
say 2
say 3
say 4
say 5
say 6
say 7
say 8
say 9
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
say this is from base

say before first
say inside first
say inside first
say after first

say before second
say inside second
say inside second
say after second

0 comments on commit ab0797a

Please sign in to comment.