|
9 | 9 | from dataclasses import dataclass
|
10 | 10 | from functools import partial
|
11 | 11 | from pathlib import Path
|
12 |
| -from typing import Any, Dict, List, NewType, Optional, Set, Tuple, Union, overload |
| 12 | +from string import Template |
| 13 | +from typing import Any, List, NewType, Optional, Set, Tuple, Union, overload |
13 | 14 | from urllib.parse import urlparse
|
14 | 15 |
|
15 | 16 | from typing_extensions import Protocol
|
@@ -103,7 +104,6 @@ def module_from_template(
|
103 | 104 | **Templates**
|
104 | 105 |
|
105 | 106 | - ``react``: for modules exporting React components
|
106 |
| - - ``react-default``: for React modules that use ``export default`` |
107 | 107 |
|
108 | 108 | Parameters:
|
109 | 109 | template:
|
@@ -149,7 +149,9 @@ def module_from_template(
|
149 | 149 | if not target_file.exists():
|
150 | 150 | target_file.parent.mkdir(parents=True, exist_ok=True)
|
151 | 151 | target_file.write_text(
|
152 |
| - _resolve_template(template_file, {"$PACKAGE": package, "$CDN": cdn}) |
| 152 | + Template(template_file.read_text()).substitute( |
| 153 | + {"PACKAGE": package, "CDN": cdn} |
| 154 | + ) |
153 | 155 | )
|
154 | 156 |
|
155 | 157 | return WebModule(
|
@@ -328,24 +330,3 @@ def _web_module_path(name: str, prefix: str = "") -> Path:
|
328 | 330 | directory /= prefix
|
329 | 331 | path = directory.joinpath(*name.split("/"))
|
330 | 332 | return path.with_suffix(path.suffix)
|
331 |
| - |
332 |
| - |
333 |
| -def _resolve_template(file: Path, substitutions: Dict[str, str]) -> str: |
334 |
| - # NOTE: If this needs to be any more complex than it is, we should really |
335 |
| - # reconsider this solution. Either use a real templating solution like Jinja |
336 |
| - # or do something else entirely. |
337 |
| - resolved_lines = [] |
338 |
| - for line in file.read_text().splitlines(): |
339 |
| - if line.startswith("$TEMPLATE:"): |
340 |
| - relative_path = line.split(":", 1)[1].strip() |
341 |
| - inner_template_file = file.parent.joinpath(*relative_path.split("/")) |
342 |
| - resolved_lines.append(_resolve_template(inner_template_file, {})) |
343 |
| - else: |
344 |
| - resolved_lines.append(line) |
345 |
| - |
346 |
| - result = "\n".join(resolved_lines) |
347 |
| - if substitutions: |
348 |
| - for k, v in substitutions.items(): |
349 |
| - result = result.replace(k, v) |
350 |
| - |
351 |
| - return result |
0 commit comments