-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtorecipejson.cpp
163 lines (144 loc) · 4.84 KB
/
torecipejson.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "cb_database.h"
#include "time.h"
#include "unicode/schriter.h"
#include "unicode/uchar.h"
#include "unicode/unistr.h"
#include "json/json.h"
#include <fmt/core.h>
#include <memory>
#include <random>
#include <ranges>
#include <regex>
using Json::Value;
static const char hexDigits[] = "0123456789ABCDEF";
void maybe_set(Value &val, const char *key, const CB_String &s) {
if (s.size() == 0)
return;
val[key] = s.str();
}
void maybe_append(Value &val, const CB_String &s) {
if (s.size() == 0)
return;
val.append(s.str());
}
std::string lowerCase(const std::string &source) {
icu::UnicodeString lower = icu::UnicodeString::fromUTF8(source);
lower.toLower();
std::string result;
lower.toUTF8String(result);
return result;
}
std::string titleCase(const std::string &source) {
icu::UnicodeString title = icu::UnicodeString::fromUTF8(source);
title.toTitle(nullptr);
std::string result;
title.toUTF8String(result);
return result;
}
std::string urlFromTitle(const std::string &title) {
static const std::regex nonAlnum("[^a-z0-9]+");
std::string lowerTitle = lowerCase(title);
return std::regex_replace(lowerTitle, nonAlnum, "-");
}
std::string escapeKey(const std::string &unescaped) {
std::string result;
for (unsigned char c : unescaped) {
switch (c) {
default:
result += c;
break;
// These are the characters disallowed from Firebase keys.
case '.':
case '#':
case '$':
case '/':
case '[':
case ']':
case '%': // And the escape character.
result += '%';
result += hexDigits[c / 16];
result += hexDigits[c % 16];
break;
}
}
return result;
}
// If a string is only lowercase or only uppercase, we should transform it to
// titlecase. Otherwise, assume the capitalization was intentional.
bool needsTitleCasing(const icu::UnicodeString &str) {
bool hasLower = false, hasUpper = false;
for (icu::StringCharacterIterator iter(str); iter.hasNext(); iter.next32()) {
if (u_islower(iter.current32()))
hasLower = true;
if (u_isupper(iter.current32()))
hasUpper = true;
}
return !(hasLower && hasUpper);
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Pass the name of the recipe database to this program, "
"usually 'Recipe.cbd'.\n";
exit(0);
}
const Value emptyObject(Json::objectValue);
const Value emptyArray(Json::arrayValue);
auto book = std::make_unique<CB_Book>();
book->Read(argv[1]);
Value root(Json::arrayValue);
for (const CB_RecipeMap_t &recipes = book->Get_sortedByName();
const auto &[_, recipe] :
#if 0
std::views::take(recipes, 10)
#else
recipes
#endif
) {
Value &recipeJson = root.append(emptyObject);
std::string recipeName = recipe->Get_name().str();
if (auto icuRecipeName = icu::UnicodeString::fromUTF8(recipeName);
needsTitleCasing(icuRecipeName)) {
recipeName.clear();
icuRecipeName.toTitle(nullptr).toUTF8String<std::string>(recipeName);
}
recipeJson["name"] = recipeName;
maybe_set(recipeJson, "recipeYield", recipe->Get_serves());
int year, month, day;
if (3 ==
sscanf(recipe->Get_date().c_str(), "%d-%d-%d", &month, &day, &year)) {
recipeJson["dateCreated"] =
fmt::format("{:04}-{:02}-{:02}", year, month, day);
}
Value categories(Json::arrayValue);
maybe_append(categories, recipe->Get_cat1());
maybe_append(categories, recipe->Get_cat2());
maybe_append(categories, recipe->Get_cat3());
maybe_append(categories, recipe->Get_cat4());
recipeJson["recipeCategory"] = std::move(categories);
const std::vector<CB_Ingredient *> &ingredients = recipe->Get_ingredients();
Value &json_ingredients = recipeJson["recipeIngredient"] = emptyArray;
for (CB_Ingredient *ingredient : ingredients) {
Value &json_ingredient = json_ingredients.append(emptyObject);
maybe_set(json_ingredient, "quantity", ingredient->Get_quantity());
maybe_set(json_ingredient, "unit", ingredient->Get_measurement());
if (ingredient->Get_ingredient().size() == 0) {
maybe_set(json_ingredient, "name", ingredient->Get_preparation());
} else {
maybe_set(json_ingredient, "name", ingredient->Get_ingredient());
maybe_set(json_ingredient, "preparation",
ingredient->Get_preparation());
}
}
// Users aren't treating the lines as numbered steps, so just concatenate
// them into a single step.
const std::vector<CB_String> &direction_lines = recipe->Get_directions();
std::string directions;
for (const auto &direction : direction_lines) {
directions += direction.str();
directions += '\n';
}
Value &json_instructions = recipeJson["recipeInstructions"] = emptyArray;
json_instructions.append(directions);
}
Json::StyledStreamWriter(" ").write(std::cout, root);
};