@blackbelt-technology/pi-dashboard-document-converter 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.pi/skills/document-converter/SKILL.md +79 -0
- package/README.md +106 -0
- package/engine/.dockerignore +4 -0
- package/engine/Dockerfile +75 -0
- package/engine/IMAGE_VERSION +1 -0
- package/engine/README.md +65 -0
- package/engine/VENDOR.md +40 -0
- package/engine/build-image.sh +29 -0
- package/engine/document_converter/LICENSE +21 -0
- package/engine/document_converter/__init__.py +365 -0
- package/engine/document_converter/__main__.py +5 -0
- package/engine/document_converter/asciidoc_converter.py +534 -0
- package/engine/document_converter/cli.py +751 -0
- package/engine/document_converter/code_renderer.py +356 -0
- package/engine/document_converter/converter.py +928 -0
- package/engine/document_converter/cover_page.py +448 -0
- package/engine/document_converter/diagram_resizer.py +311 -0
- package/engine/document_converter/diagram_sizing.py +357 -0
- package/engine/document_converter/docx_reader.py +627 -0
- package/engine/document_converter/field_updater.py +277 -0
- package/engine/document_converter/font_manager.py +850 -0
- package/engine/document_converter/frontmatter_parser.py +644 -0
- package/engine/document_converter/graphviz_renderer.py +354 -0
- package/engine/document_converter/image_extractor.py +281 -0
- package/engine/document_converter/image_style.py +237 -0
- package/engine/document_converter/logo_manager.py +1021 -0
- package/engine/document_converter/manifest_manager.py +288 -0
- package/engine/document_converter/mermaid_renderer.py +371 -0
- package/engine/document_converter/meta_mapper.py +398 -0
- package/engine/document_converter/pandoc_styles.py +222 -0
- package/engine/document_converter/pdf_converter.py +735 -0
- package/engine/document_converter/pdf_server.py +283 -0
- package/engine/document_converter/plantuml_renderer.py +460 -0
- package/engine/document_converter/plantuml_server.py +273 -0
- package/engine/document_converter/pyproject.toml +80 -0
- package/engine/document_converter/round_trip.py +312 -0
- package/engine/document_converter/spacing_applier.py +332 -0
- package/engine/document_converter/spacing_config.py +323 -0
- package/engine/document_converter/style_extractor.py +420 -0
- package/engine/document_converter/svg_embedder.py +294 -0
- package/engine/document_converter/system_variables.py +158 -0
- package/engine/document_converter/table_formatter.py +450 -0
- package/engine/document_converter/template_extractor.py +1334 -0
- package/engine/document_converter/template_manager.py +427 -0
- package/engine/document_converter/textbox_extractor.py +551 -0
- package/engine/document_converter/toc_generator.py +559 -0
- package/engine/document_converter/toc_style.py +290 -0
- package/engine/document_converter/tree_renderer.py +328 -0
- package/engine/document_converter/utils.py +134 -0
- package/engine/document_converter/variable_manager.py +394 -0
- package/engine/engine_cli.py +371 -0
- package/engine/frontmatter_filler/doc-meta.sample.yaml +106 -0
- package/engine/frontmatter_filler/fill.py +517 -0
- package/engine/markdown_table_profiler/profile.py +442 -0
- package/engine/nano-banana-styles.yaml +59 -0
- package/engine/tests/test_styled_diagrams.py +109 -0
- package/package.json +32 -0
- package/src/__tests__/engine.test.ts +72 -0
- package/src/__tests__/facade.test.ts +120 -0
- package/src/__tests__/integration.test.ts +101 -0
- package/src/__tests__/ocr.test.ts +34 -0
- package/src/__tests__/provenance.test.ts +89 -0
- package/src/__tests__/routing.test.ts +40 -0
- package/src/engine.ts +187 -0
- package/src/errors.ts +43 -0
- package/src/index.ts +179 -0
- package/src/ocr.ts +43 -0
- package/src/provenance.ts +79 -0
- package/src/routing.ts +49 -0
- package/src/schema.json +69 -0
- package/src/schema.ts +107 -0
- package/src/types.ts +90 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Utility functions for document converter."""
|
|
2
|
+
import subprocess
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import tempfile
|
|
5
|
+
import shutil
|
|
6
|
+
from typing import Dict, List, Tuple
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def check_dependencies() -> bool:
|
|
10
|
+
"""Check if required external dependencies are installed.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
bool: True if mmdc (Mermaid CLI) is available, False otherwise.
|
|
14
|
+
"""
|
|
15
|
+
try:
|
|
16
|
+
subprocess.run(
|
|
17
|
+
['mmdc', '--version'],
|
|
18
|
+
capture_output=True,
|
|
19
|
+
text=True
|
|
20
|
+
)
|
|
21
|
+
return True
|
|
22
|
+
except FileNotFoundError:
|
|
23
|
+
return False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def check_all_dependencies() -> Dict[str, bool]:
|
|
27
|
+
"""Check all external dependencies and return their status.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
Dict mapping dependency name to availability status.
|
|
31
|
+
"""
|
|
32
|
+
dependencies = {}
|
|
33
|
+
|
|
34
|
+
# Check mmdc (Mermaid CLI)
|
|
35
|
+
try:
|
|
36
|
+
subprocess.run(['mmdc', '--version'], capture_output=True, text=True)
|
|
37
|
+
dependencies['mmdc'] = True
|
|
38
|
+
except FileNotFoundError:
|
|
39
|
+
dependencies['mmdc'] = False
|
|
40
|
+
|
|
41
|
+
# Check pandoc
|
|
42
|
+
try:
|
|
43
|
+
subprocess.run(['pandoc', '--version'], capture_output=True, text=True)
|
|
44
|
+
dependencies['pandoc'] = True
|
|
45
|
+
except FileNotFoundError:
|
|
46
|
+
dependencies['pandoc'] = False
|
|
47
|
+
|
|
48
|
+
return dependencies
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_missing_dependencies() -> List[Tuple[str, str]]:
|
|
52
|
+
"""Get list of missing dependencies with installation instructions.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
List of tuples (dependency_name, installation_instructions).
|
|
56
|
+
"""
|
|
57
|
+
deps = check_all_dependencies()
|
|
58
|
+
missing = []
|
|
59
|
+
|
|
60
|
+
if not deps.get('mmdc'):
|
|
61
|
+
missing.append((
|
|
62
|
+
'mmdc (Mermaid CLI)',
|
|
63
|
+
'npm install -g @mermaid-js/mermaid-cli'
|
|
64
|
+
))
|
|
65
|
+
|
|
66
|
+
if not deps.get('pandoc'):
|
|
67
|
+
missing.append((
|
|
68
|
+
'pandoc',
|
|
69
|
+
'macOS: brew install pandoc\n'
|
|
70
|
+
' Linux (Debian/Ubuntu): sudo apt-get install pandoc\n'
|
|
71
|
+
' Linux (Fedora): sudo dnf install pandoc\n'
|
|
72
|
+
' Or download from: https://pandoc.org/installing.html'
|
|
73
|
+
))
|
|
74
|
+
|
|
75
|
+
return missing
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def print_dependency_status() -> bool:
|
|
79
|
+
"""Print dependency status and return True if all are installed.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
bool: True if all dependencies are available.
|
|
83
|
+
"""
|
|
84
|
+
deps = check_all_dependencies()
|
|
85
|
+
all_ok = True
|
|
86
|
+
|
|
87
|
+
print("Dependency Status:")
|
|
88
|
+
print("-" * 40)
|
|
89
|
+
|
|
90
|
+
for name, installed in deps.items():
|
|
91
|
+
status = "✓ installed" if installed else "✗ MISSING"
|
|
92
|
+
print(f" {name}: {status}")
|
|
93
|
+
if not installed:
|
|
94
|
+
all_ok = False
|
|
95
|
+
|
|
96
|
+
if not all_ok:
|
|
97
|
+
print("\nInstallation Instructions:")
|
|
98
|
+
print("-" * 40)
|
|
99
|
+
for name, instructions in get_missing_dependencies():
|
|
100
|
+
print(f"\n{name}:")
|
|
101
|
+
print(f" {instructions}")
|
|
102
|
+
|
|
103
|
+
return all_ok
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def validate_file(path: Path) -> bool:
|
|
107
|
+
"""Validate that a file exists and is readable.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
path: Path to the file to validate.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
bool: True if file exists and is readable, False otherwise.
|
|
114
|
+
"""
|
|
115
|
+
return path.exists() and path.is_file() and path.stat().st_size > 0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def create_temp_dir() -> Path:
|
|
119
|
+
"""Create a temporary directory for intermediate files.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
Path: Path to the created temporary directory.
|
|
123
|
+
"""
|
|
124
|
+
return Path(tempfile.mkdtemp(prefix='docconv_'))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def cleanup_temp_files(temp_dir: Path) -> None:
|
|
128
|
+
"""Remove temporary directory and all its contents.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
temp_dir: Path to temporary directory to remove.
|
|
132
|
+
"""
|
|
133
|
+
if temp_dir.exists() and temp_dir.is_dir():
|
|
134
|
+
shutil.rmtree(temp_dir)
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"""Variable management for document conversion with properties file support.
|
|
2
|
+
|
|
3
|
+
DEPRECATED: Properties file support is deprecated. Use YAML frontmatter instead.
|
|
4
|
+
This module will be removed in a future version.
|
|
5
|
+
"""
|
|
6
|
+
import logging
|
|
7
|
+
import warnings
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Dict, Optional, List, Any
|
|
10
|
+
from xml.etree import ElementTree as ET
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
# Deprecation message
|
|
16
|
+
DEPRECATION_MESSAGE = (
|
|
17
|
+
"Properties file support is deprecated. "
|
|
18
|
+
"Please migrate to YAML frontmatter in your source files. "
|
|
19
|
+
"Properties files will be removed in a future version."
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_properties_path(document_path: Path) -> Path:
|
|
24
|
+
"""Get the properties file path for a document.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
document_path: Path to the Markdown or DOCX file.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
Path to the corresponding _variables.properties file.
|
|
31
|
+
"""
|
|
32
|
+
return document_path.parent / f"{document_path.stem}_variables.properties"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def parse_properties(content: str) -> Dict[str, str]:
|
|
36
|
+
"""Parse a properties file content into a dictionary.
|
|
37
|
+
|
|
38
|
+
Handles:
|
|
39
|
+
- Key=value pairs
|
|
40
|
+
- Comments starting with # or !
|
|
41
|
+
- Continuation lines with backslash
|
|
42
|
+
- Unicode escapes
|
|
43
|
+
- UTF-8 encoding
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
content: Properties file content as string.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Dictionary of variable name to value.
|
|
50
|
+
"""
|
|
51
|
+
variables = {}
|
|
52
|
+
lines = content.split('\n')
|
|
53
|
+
i = 0
|
|
54
|
+
|
|
55
|
+
while i < len(lines):
|
|
56
|
+
line = lines[i].strip()
|
|
57
|
+
|
|
58
|
+
# Skip empty lines and comments
|
|
59
|
+
if not line or line.startswith('#') or line.startswith('!'):
|
|
60
|
+
i += 1
|
|
61
|
+
continue
|
|
62
|
+
|
|
63
|
+
# Handle continuation lines
|
|
64
|
+
full_line = line
|
|
65
|
+
while full_line.endswith('\\') and i + 1 < len(lines):
|
|
66
|
+
full_line = full_line[:-1] # Remove backslash
|
|
67
|
+
i += 1
|
|
68
|
+
full_line += lines[i].strip()
|
|
69
|
+
|
|
70
|
+
# Parse key=value or key:value
|
|
71
|
+
match = re.match(r'^([^=:]+)[=:](.*)$', full_line)
|
|
72
|
+
if match:
|
|
73
|
+
key = match.group(1).strip()
|
|
74
|
+
value = match.group(2).strip()
|
|
75
|
+
|
|
76
|
+
# Handle unicode escapes like \uXXXX
|
|
77
|
+
value = decode_unicode_escapes(value)
|
|
78
|
+
|
|
79
|
+
variables[key] = value
|
|
80
|
+
|
|
81
|
+
i += 1
|
|
82
|
+
|
|
83
|
+
return variables
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def decode_unicode_escapes(s: str) -> str:
|
|
87
|
+
"""Decode unicode escape sequences in a string.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
s: String potentially containing \\uXXXX escapes.
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
String with unicode escapes decoded.
|
|
94
|
+
"""
|
|
95
|
+
def replace_unicode(match):
|
|
96
|
+
return chr(int(match.group(1), 16))
|
|
97
|
+
|
|
98
|
+
return re.sub(r'\\u([0-9a-fA-F]{4})', replace_unicode, s)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def encode_unicode_escapes(s: str) -> str:
|
|
102
|
+
"""Encode non-ASCII characters as unicode escapes.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
s: String to encode.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
String with non-ASCII chars as \\uXXXX.
|
|
109
|
+
"""
|
|
110
|
+
result = []
|
|
111
|
+
for char in s:
|
|
112
|
+
if ord(char) > 127:
|
|
113
|
+
result.append(f'\\u{ord(char):04x}')
|
|
114
|
+
else:
|
|
115
|
+
result.append(char)
|
|
116
|
+
return ''.join(result)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def format_properties(variables: Dict[str, str], header_comment: Optional[str] = None) -> str:
|
|
120
|
+
"""Format a dictionary as properties file content.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
variables: Dictionary of variable name to value.
|
|
124
|
+
header_comment: Optional comment to add at the top.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
Properties file content as string.
|
|
128
|
+
"""
|
|
129
|
+
lines = []
|
|
130
|
+
|
|
131
|
+
if header_comment:
|
|
132
|
+
for line in header_comment.split('\n'):
|
|
133
|
+
lines.append(f"# {line}")
|
|
134
|
+
lines.append("")
|
|
135
|
+
|
|
136
|
+
for key, value in sorted(variables.items()):
|
|
137
|
+
# Escape special characters in value
|
|
138
|
+
escaped_value = value.replace('\\', '\\\\')
|
|
139
|
+
lines.append(f"{key}={escaped_value}")
|
|
140
|
+
|
|
141
|
+
return '\n'.join(lines) + '\n'
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def load_properties_file(properties_path: Path) -> Dict[str, str]:
|
|
145
|
+
"""Load variables from a properties file.
|
|
146
|
+
|
|
147
|
+
DEPRECATED: Use YAML frontmatter instead.
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
properties_path: Path to the properties file.
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
Dictionary of variable name to value.
|
|
154
|
+
Empty dict if file doesn't exist.
|
|
155
|
+
"""
|
|
156
|
+
warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
|
|
157
|
+
logger.warning(DEPRECATION_MESSAGE)
|
|
158
|
+
|
|
159
|
+
if not properties_path.exists():
|
|
160
|
+
return {}
|
|
161
|
+
|
|
162
|
+
content = properties_path.read_text(encoding='utf-8')
|
|
163
|
+
return parse_properties(content)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def save_properties_file(
|
|
167
|
+
properties_path: Path,
|
|
168
|
+
variables: Dict[str, str],
|
|
169
|
+
document_name: Optional[str] = None
|
|
170
|
+
) -> None:
|
|
171
|
+
"""Save variables to a properties file.
|
|
172
|
+
|
|
173
|
+
DEPRECATED: Use YAML frontmatter instead.
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
properties_path: Path to the properties file.
|
|
177
|
+
variables: Dictionary of variable name to value.
|
|
178
|
+
document_name: Optional document name for header comment.
|
|
179
|
+
"""
|
|
180
|
+
warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
|
|
181
|
+
logger.warning(DEPRECATION_MESSAGE)
|
|
182
|
+
|
|
183
|
+
header = f"Variables for {document_name}" if document_name else "Document variables"
|
|
184
|
+
content = format_properties(variables, header)
|
|
185
|
+
properties_path.parent.mkdir(parents=True, exist_ok=True)
|
|
186
|
+
properties_path.write_text(content, encoding='utf-8')
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def load_template_variables(template_dir: Path) -> Dict[str, Dict[str, Any]]:
|
|
190
|
+
"""Load variable definitions from a template's variables.xml.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
template_dir: Path to the template directory.
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
Dictionary of variable name to variable info (default, description).
|
|
197
|
+
"""
|
|
198
|
+
variables_path = template_dir / 'variables.xml'
|
|
199
|
+
if not variables_path.exists():
|
|
200
|
+
return {}
|
|
201
|
+
|
|
202
|
+
tree = ET.parse(variables_path)
|
|
203
|
+
root = tree.getroot()
|
|
204
|
+
|
|
205
|
+
variables = {}
|
|
206
|
+
for var_elem in root.findall('variable'):
|
|
207
|
+
name = var_elem.get('name', '')
|
|
208
|
+
if not name:
|
|
209
|
+
continue
|
|
210
|
+
|
|
211
|
+
var_info = {
|
|
212
|
+
'name': name,
|
|
213
|
+
'default': '',
|
|
214
|
+
'description': ''
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
default_elem = var_elem.find('default')
|
|
218
|
+
if default_elem is not None and default_elem.text:
|
|
219
|
+
var_info['default'] = default_elem.text
|
|
220
|
+
|
|
221
|
+
desc_elem = var_elem.find('description')
|
|
222
|
+
if desc_elem is not None and desc_elem.text:
|
|
223
|
+
var_info['description'] = desc_elem.text
|
|
224
|
+
|
|
225
|
+
variables[name] = var_info
|
|
226
|
+
|
|
227
|
+
return variables
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def get_missing_variables(
|
|
231
|
+
required: Dict[str, Dict[str, Any]],
|
|
232
|
+
provided: Dict[str, str]
|
|
233
|
+
) -> List[Dict[str, Any]]:
|
|
234
|
+
"""Identify variables that are required but not provided.
|
|
235
|
+
|
|
236
|
+
Args:
|
|
237
|
+
required: Dictionary of required variables from template.
|
|
238
|
+
provided: Dictionary of provided variable values.
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
List of missing variable info dicts.
|
|
242
|
+
"""
|
|
243
|
+
missing = []
|
|
244
|
+
for name, info in required.items():
|
|
245
|
+
if name not in provided:
|
|
246
|
+
missing.append(info)
|
|
247
|
+
return missing
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def validate_variables(
|
|
251
|
+
template_dir: Path,
|
|
252
|
+
provided: Dict[str, str]
|
|
253
|
+
) -> Dict[str, Any]:
|
|
254
|
+
"""Validate provided variables against template requirements.
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
template_dir: Path to the template directory.
|
|
258
|
+
provided: Dictionary of provided variable values.
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
Dictionary with 'valid', 'missing', and 'extra' keys.
|
|
262
|
+
"""
|
|
263
|
+
required = load_template_variables(template_dir)
|
|
264
|
+
|
|
265
|
+
missing = get_missing_variables(required, provided)
|
|
266
|
+
extra = [k for k in provided if k not in required]
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
'valid': len(missing) == 0,
|
|
270
|
+
'missing': missing,
|
|
271
|
+
'extra': extra,
|
|
272
|
+
'required': required
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def prompt_for_variable(var_info: Dict[str, Any]) -> str:
|
|
277
|
+
"""Prompt user for a variable value interactively.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
var_info: Variable info dict with name, default, description.
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
User-provided value.
|
|
284
|
+
"""
|
|
285
|
+
name = var_info.get('name', 'unknown')
|
|
286
|
+
default = var_info.get('default', '')
|
|
287
|
+
description = var_info.get('description', '')
|
|
288
|
+
|
|
289
|
+
prompt_text = f" {name}"
|
|
290
|
+
if description:
|
|
291
|
+
prompt_text += f" ({description})"
|
|
292
|
+
if default:
|
|
293
|
+
prompt_text += f" [{default}]"
|
|
294
|
+
prompt_text += ": "
|
|
295
|
+
|
|
296
|
+
value = input(prompt_text).strip()
|
|
297
|
+
return value if value else default
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def prompt_all_missing(
|
|
301
|
+
missing: List[Dict[str, Any]],
|
|
302
|
+
existing: Dict[str, str]
|
|
303
|
+
) -> Dict[str, str]:
|
|
304
|
+
"""Prompt user for all missing variable values.
|
|
305
|
+
|
|
306
|
+
Args:
|
|
307
|
+
missing: List of missing variable info dicts.
|
|
308
|
+
existing: Existing variable values to merge with.
|
|
309
|
+
|
|
310
|
+
Returns:
|
|
311
|
+
Complete dictionary of all variable values.
|
|
312
|
+
"""
|
|
313
|
+
result = dict(existing)
|
|
314
|
+
|
|
315
|
+
if missing:
|
|
316
|
+
print("\nTemplate requires additional variables:")
|
|
317
|
+
for var_info in missing:
|
|
318
|
+
value = prompt_for_variable(var_info)
|
|
319
|
+
result[var_info['name']] = value
|
|
320
|
+
|
|
321
|
+
return result
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def get_defaults_for_missing(
|
|
325
|
+
missing: List[Dict[str, Any]],
|
|
326
|
+
existing: Dict[str, str]
|
|
327
|
+
) -> Dict[str, str]:
|
|
328
|
+
"""Get default values for missing variables (non-interactive mode).
|
|
329
|
+
|
|
330
|
+
Args:
|
|
331
|
+
missing: List of missing variable info dicts.
|
|
332
|
+
existing: Existing variable values to merge with.
|
|
333
|
+
|
|
334
|
+
Returns:
|
|
335
|
+
Complete dictionary with defaults for missing variables.
|
|
336
|
+
"""
|
|
337
|
+
result = dict(existing)
|
|
338
|
+
|
|
339
|
+
for var_info in missing:
|
|
340
|
+
result[var_info['name']] = var_info.get('default', '')
|
|
341
|
+
|
|
342
|
+
return result
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def resolve_variables(
|
|
346
|
+
template_dir: Path,
|
|
347
|
+
properties_path: Path,
|
|
348
|
+
interactive: bool = True,
|
|
349
|
+
provided: Optional[Dict[str, str]] = None
|
|
350
|
+
) -> Dict[str, str]:
|
|
351
|
+
"""Resolve all variables for a conversion, prompting if needed.
|
|
352
|
+
|
|
353
|
+
Args:
|
|
354
|
+
template_dir: Path to the template directory.
|
|
355
|
+
properties_path: Path to the properties file.
|
|
356
|
+
interactive: Whether to prompt for missing variables.
|
|
357
|
+
provided: Optional dictionary of already-provided variables (e.g., from frontmatter).
|
|
358
|
+
|
|
359
|
+
Returns:
|
|
360
|
+
Complete dictionary of all variable values.
|
|
361
|
+
"""
|
|
362
|
+
# Load provided variables from properties file, merge with any already provided
|
|
363
|
+
file_vars = load_properties_file(properties_path)
|
|
364
|
+
if provided is None:
|
|
365
|
+
provided = file_vars
|
|
366
|
+
else:
|
|
367
|
+
# Merge: provided (frontmatter) takes priority over file vars
|
|
368
|
+
provided = {**file_vars, **provided}
|
|
369
|
+
|
|
370
|
+
# Validate against template requirements
|
|
371
|
+
validation = validate_variables(template_dir, provided)
|
|
372
|
+
|
|
373
|
+
if validation['valid']:
|
|
374
|
+
return provided
|
|
375
|
+
|
|
376
|
+
# Handle missing variables
|
|
377
|
+
missing = validation['missing']
|
|
378
|
+
|
|
379
|
+
if interactive:
|
|
380
|
+
result = prompt_all_missing(missing, provided)
|
|
381
|
+
# Save updated variables back to properties file
|
|
382
|
+
save_properties_file(
|
|
383
|
+
properties_path,
|
|
384
|
+
result,
|
|
385
|
+
properties_path.stem.replace('_variables', '')
|
|
386
|
+
)
|
|
387
|
+
return result
|
|
388
|
+
else:
|
|
389
|
+
# Non-interactive: use defaults
|
|
390
|
+
print("\nUsing default values for missing variables:")
|
|
391
|
+
for var_info in missing:
|
|
392
|
+
default = var_info.get('default', '')
|
|
393
|
+
print(f" {var_info['name']}: \"{default}\" (default)")
|
|
394
|
+
return get_defaults_for_missing(missing, provided)
|