-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from nicholasyager/feature/jinja_block_support
Feature: Block-level copying of Jinja content between split projects
- Loading branch information
Showing
14 changed files
with
275 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import re | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Set, Tuple | ||
|
||
|
||
@dataclass | ||
class JinjaBlock: | ||
""" | ||
A data structure for tracking Jinja blocks of text. Includes the start and end character positions, and the content of the block | ||
""" | ||
|
||
path: Path | ||
block_type: str | ||
name: str | ||
start: int | ||
end: int | ||
content: str | ||
|
||
@staticmethod | ||
def find_block_range(file_content: str, block_type: str, name: str) -> Tuple[int, int]: | ||
"""Find the line number that a block started.""" | ||
start_line = None | ||
end_line = None | ||
|
||
for match in re.finditer( | ||
r"{%-?\s+" + block_type + r"\s+" + name + r"([(a-zA-Z0-9=,_ )]*)\s-?%}", | ||
file_content, | ||
re.MULTILINE, | ||
): | ||
start = match.span()[0] # .span() gives tuple (start, end) | ||
start_line = start # file_content[:start].count("\n") | ||
break | ||
|
||
if start_line is None: | ||
raise Exception(f"Unable to find a {block_type} block with the name {name}.") | ||
|
||
for match in re.finditer( | ||
r"{%-?\s+end" + block_type + r"\s+-?%}", file_content, re.MULTILINE | ||
): | ||
end = match.span()[1] # .span() gives tuple (start, end) | ||
new_end_line = end # file_content[:start].count("\n") | ||
|
||
if new_end_line >= start_line: | ||
end_line = new_end_line | ||
break | ||
|
||
if end_line is None: | ||
raise Exception(f"Unable to find a the closing end{block_type} block for {name}.") | ||
|
||
return start_line, end_line | ||
|
||
@staticmethod | ||
def isolate_content(file_content: str, start: int, end: int) -> str: | ||
"""Given content, a start position, and an end position, return the content of a Jinja block.""" | ||
return file_content[start:end] | ||
|
||
@classmethod | ||
def from_file(cls, path: Path, block_type: str, name: str) -> "JinjaBlock": | ||
"""Find a specific Jinja block within a file, based on the block type and the name.""" | ||
|
||
file_content = path.read_text() | ||
start, end = cls.find_block_range(file_content, block_type, name) | ||
content = cls.isolate_content(file_content=file_content, start=start, end=end) | ||
|
||
return cls( | ||
path=path, block_type=block_type, name=name, start=start, end=end, content=content | ||
) | ||
|
||
|
||
def find_doc_reference(content: str) -> Set[str]: | ||
"""Find all doc block references within a string.""" | ||
matches = re.findall(r"{{\sdoc\(\'?\"?([a-zA-Z0-9_\-\.]+)\'?\"?\)\s}}", content) | ||
|
||
return set(matches) |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
macros: | ||
- name: cents_to_dollars | ||
description: Converts cents to dollars | ||
description: Converts cents to dollars | ||
- name: dollars_to_cents | ||
description: Converts dollars to cents |
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% docs customer_id %} | ||
The unique key for each customer. | ||
{% enddocs %} |
This file contains 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 characters
This file contains 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 characters
Oops, something went wrong.