Skip to content

Commit 16073ac

Browse files
add stubbed out compiler
1 parent 82f5e53 commit 16073ac

File tree

2 files changed

+9
-37
lines changed

2 files changed

+9
-37
lines changed

src/django_bird/compiler.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
from __future__ import annotations
22

3-
import re
4-
from typing import Match
53

6-
from .components import transformer
4+
class Compiler:
5+
def compile(self, input_string: str) -> str:
6+
tokens = self.tokenize(input_string)
7+
parsed = self.parse(tokens)
8+
return self.transform(parsed)
79

8-
BIRD_TAG_PATTERN = re.compile(r"<bird:(\w+)([^>]*)>(.*?)</bird:\1>")
10+
def tokenize(self, input_string: str) -> str: ...
911

12+
def parse(self, tokens: str) -> str: ...
1013

11-
class BirdCompiler:
12-
def __init__(self) -> None:
13-
self.transformer = transformer
14-
15-
def compile(self, content: str) -> str:
16-
parts = content.splitlines()
17-
result = []
18-
19-
for part in parts:
20-
if match := BIRD_TAG_PATTERN.search(part):
21-
transformed = [item for item in self.transform_bird_line(part, match)]
22-
result.append("".join(transformed))
23-
else:
24-
result.append(part)
25-
26-
return "\n".join(result)
27-
28-
def transform_bird_line(self, line: str, match: Match[str]):
29-
start, end = match.span()
30-
31-
yield line[:start].strip()
32-
33-
tag_name, attrs, content = match.groups()
34-
yield self.transformer.transform(tag_name, attrs, content)
35-
36-
yield line[end:].strip()
14+
def transform(self, parsed_content: str) -> str: ...

src/django_bird/loader.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@
1717
class BirdLoader(FileSystemLoader):
1818
def __init__(self, engine: Engine):
1919
super().__init__(engine)
20-
self._compiler = None
21-
22-
@property
23-
def compiler(self) -> BirdCompiler:
24-
if self._compiler is None:
25-
self._compiler = BirdCompiler()
26-
return self._compiler
20+
self.compiler = BirdCompiler()
2721

2822
@override
2923
def get_contents(self, origin: Origin) -> str:

0 commit comments

Comments
 (0)