-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
145 lines (130 loc) · 7.19 KB
/
parser.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "parser.h"
int Parser::parseInt(string value, YAML::Node variables) {
if (value[0] == '$' && variables[value.substr(1)].IsDefined()) {
return variables[value.substr(1)].as<int>();
}
return std::stoi(value);
}
string Parser::parseString(string value, YAML::Node variables) {
QString text = QString::fromStdString(value);
if (text.contains("+")) {
string result;
QStringList list = text.split("+");
for (int i = 0; i < list.length(); i++) {
QString curr = list[i].trimmed();
if (curr[0] == '$' && variables[curr.toStdString().substr(1)].IsDefined()) {
result.append(variables[curr.toStdString().substr(1)].as<string>());
} else {
result.append(curr.toStdString());
}
}
return result;
} else if (value[0] == '$' && variables[value.substr(1)].IsDefined()) {
return variables[value.substr(1)].as<string>();
}
return value;
}
vector<spv::File*> Parser::ParseYaml(string yaml) {
YAML::Node variables;
YAML::Node instructions = YAML::Load(yaml);
vector<spv::File*> result;
if (instructions.IsSequence()) {
for (auto && instruction : instructions) {
if (instruction.IsMap() && instruction["part"].IsDefined()) {
string type = parseString(instruction["part"]["type"].as<string>(), variables);
string path = parseString(instruction["part"]["path"].as<string>(""), variables);
ScaleType scale = SCALE_NONE;
vector<Text *> text;
if (instruction["part"]["scale"].IsDefined()) {
QString scaleString = QString::fromStdString(
parseString(instruction["part"]["scale"].as<string>(), variables));
if (boost::iequals(scaleString.toStdString(), "crop")) {
scale = SCALE_CROP;
} else if (boost::iequals(scaleString.toStdString(), "fit")) {
scale = SCALE_FIT;
} else if (boost::iequals(scaleString.toStdString(), "stretch")) {
scale = SCALE_STRETCH;
} else {
scale = SCALE_NONE;
}
}
YAML::Node labels = instruction["part"]["text"];
if (labels.IsSequence()) {
for (auto && label : labels) {
string value = parseString(label["part"]["value"].as<string>(), variables);
string position = parseString(label["part"]["position"].as<string>(), variables);
string font = parseString(label["part"]["font"].as<string>(), variables);
string color = parseString(label["part"]["color"].as<string>(), variables);
int size = parseInt(label["part"]["size"].as<string>(), variables);
text.push_back(new Text(value, size, font, color, position));
}
}
if (boost::iequals(type, "video")) {
uint start = 0;
uint end = 0;
if (instruction["part"]["start"].IsDefined()) {
start = parseInt(instruction["part"]["start"].as<string>(), variables);
}
if (instruction["part"]["end"].IsDefined()) {
end = parseInt(instruction["part"]["end"].as<string>(), variables);
}
AudioFile *audio = nullptr;
if (instruction["part"]["audio"].IsMap()) {
string audioPath = parseString(instruction["part"]["audio"]["path"]
.as<string>(), variables);
uint audioStart = parseInt(instruction["part"]["audio"]["start"]
.as<string>(), variables);
audio = new AudioFile(audioPath, audioStart, audioStart + end - start);
}
result.push_back(new VideoFile(path, start, end, audio, text, scale));
} else if (boost::iequals(type, "image")) {
uint length = parseInt(instruction["part"]["length"].as<string>(), variables);
string animation = parseString(instruction["part"]["animation"]
.as<string>("none"), variables);
AudioFile *audio = nullptr;
if (instruction["part"]["audio"].IsMap()) {
string audioPath = parseString(instruction["part"]["audio"]["path"]
.as<string>(), variables);
uint audioStart = parseInt(instruction["part"]["audio"]["start"]
.as<string>(), variables);
audio = new AudioFile(audioPath, audioStart, audioStart + length);
}
result.push_back(new ImageFile(path, length, audio, text, scale, animation));
} else if (boost::iequals(type, "text")) {
string blackImagePath = QSettings().fileName().left(QSettings().fileName().lastIndexOf("/") + 1).append("black.jpg").toStdString();
uint length = parseInt(instruction["part"]["length"].as<string>(), variables);
AudioFile *audio = nullptr;
if (instruction["part"]["audio"].IsMap()) {
string audioPath = parseString(instruction["part"]["audio"]["path"]
.as<string>(), variables);
uint audioStart = parseInt(instruction["part"]["audio"]["start"]
.as<string>(), variables);
audio = new AudioFile(audioPath, audioStart, audioStart + length);
}
string value = parseString(instruction["part"]["value"].as<string>(), variables);
string position = parseString(instruction["part"]["position"].as<string>(), variables);
string font = parseString(instruction["part"]["font"].as<string>(), variables);
string color = parseString(instruction["part"]["color"].as<string>(), variables);
int size = parseInt(instruction["part"]["size"].as<string>(), variables);
text.push_back(new Text(value, size, font, color, position));
result.push_back(new ImageFile(path, length, audio, text, scale, "none"));
}
} else if (instruction.IsMap() && instruction["variables"].IsDefined()) {
variables = instruction["variables"];
}
}
}
return result;
}
string Parser::getString(string value, string yaml) {
YAML::Node variables;
YAML::Node instructions = YAML::Load(yaml);
if (instructions.IsSequence()) {
for (auto &&instruction : instructions) {
if (instruction.IsMap() && instruction["variables"].IsDefined()) {
variables = instruction["variables"];
}
}
}
return Parser::parseString(value, variables);
}