-
Notifications
You must be signed in to change notification settings - Fork 0
/
placeholderTemplateEngine.dart
110 lines (93 loc) · 3.44 KB
/
placeholderTemplateEngine.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:qutem/fileHandler.dart';
import 'dart:convert';
import 'dart:core';
import 'package:qutem/templateEngine.dart';
class PlaceholderTemplateEngine {
static final _logger = Logger('PlaceholderTemplateEngine');
static int replacements = 0;
static int filesCreated = 0;
static final categoryPlaceholderMap = {};
static var placeHolderMap = {}; //for currently processed category
static List<String> getCategoryKeys() {
var categoryKeys = <String>[];
categoryPlaceholderMap.keys.forEach((categoryKey) {
categoryKeys.add(categoryKey);
});
return categoryKeys;
}
static String? doReplacePlaceHolder(placeholderName, matchStr) {
if (placeHolderMap.containsKey(placeholderName)) {
PlaceholderTemplateEngine.replacements++;
return placeHolderMap[placeholderName];
}
return matchStr;
}
static void run(filePath) {
try {
_logger.fine('Input file: ' + filePath);
var file = File(filePath);
var inputFileContent = file.readAsStringSync();
var placeHolder = PlaceHolder(RegExp(r'({{.*?}})'), 2, 2);
categoryPlaceholderMap.forEach((languageKey, phMap) {
placeHolderMap = phMap;
var categoryFileContent = TemplateEngine.applyTemplate(
inputFileContent, placeHolder, doReplacePlaceHolder);
var relativeFilePath = FileHandler.getRelativeFilePath(filePath);
var categoryFilePath = 'dist' +
Platform.pathSeparator +
languageKey +
Platform.pathSeparator +
relativeFilePath;
File(categoryFilePath).createSync(recursive: true);
filesCreated++;
var categoryFile = File(categoryFilePath);
categoryFile.writeAsStringSync(categoryFileContent);
});
} on Exception catch (e) {
stdout.writeln('Error.' + e.toString());
exit(1);
}
}
//Prepares a map of categories to key/value maps
static void prepareMappingsFromTemplateFile(filePath) {
var templateFilePath = filePath + '.tmpl';
var templateFile = File(templateFilePath);
var templateFileContent = templateFile.readAsStringSync();
var ls = LineSplitter();
var lines = ls.convert(templateFileContent);
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim();
if (line.isEmpty) {
continue;
}
if (!line.contains('=')) {
continue;
}
if (line.startsWith('#')) {
continue;
}
//now we have a line containing something like this
//title.en=Name of page
var keyValueSeparator = '=';
var parts = line.split(keyValueSeparator);
var keyAndCategory = parts[0];
parts.removeAt(0);
var value =
parts.join(keyValueSeparator); //in case the value contains a =
var keyCategorySeparator = '.';
var keyAndCategoryParts = keyAndCategory.split(keyCategorySeparator);
var category = keyAndCategoryParts
.removeLast(); //in case there are . chars in the key
var key = keyAndCategoryParts.join(keyCategorySeparator);
if (!categoryPlaceholderMap.containsKey(category)) {
categoryPlaceholderMap[category] = {};
}
var placeHolderMap = categoryPlaceholderMap[category];
placeHolderMap[key] = value;
}
//now we should have a categoryPlaceholderMap where each key is a category (language)
//and each value a map of placeholders to placeholder values
}
}