Skip to content

Commit 1482b07

Browse files
author
Dominic Price
committed
Add clean function to cdb-nbtool
1 parent 33d345a commit 1482b07

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

core/cdb-nbtool.cc

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <internal/difflib.h>
22
#include <internal/string_tools.h>
3+
#include <internal/uuid.h>
34
#include <iostream>
45
#include <iomanip>
56
#include <fstream>
@@ -293,7 +294,10 @@ void help()
293294
<< " | [merge \"cadabranotebook\"]\n"
294295
<< " | command = cdb-nbtool gitmerge\n"
295296
<< " and add the following line to your .gitattributes file:\n"
296-
<< " | *.cnb merge=cadabranotebook\n";
297+
<< " | *.cnb merge=cadabranotebook\n"
298+
<< " -- cdb-nbtool clean <file>\n"
299+
<< " Ensures that all input cells are visible and deletes all output cells. This can fix\n"
300+
<< " problems with corrupt notebooks. The original notebook is saved as <file>~\n";
297301
}
298302

299303
void view(const char* fname)
@@ -447,6 +451,56 @@ void gitdiff(const char* a, const char* b, const char* relpath)
447451
cnb_diff(af, bf);
448452
}
449453

454+
void clean(const char* a)
455+
{
456+
std::ifstream ifs(a);
457+
if (!ifs.is_open()) {
458+
std::cerr << "Could not open file " << a << "\n";
459+
exit(1);
460+
}
461+
462+
// Copy the file to a~
463+
{
464+
std::ofstream ofs(std::string(a) + "~");
465+
if (!ofs.is_open()) {
466+
std::cerr << "Could not create backup at " << a << "~\n";
467+
exit(1);
468+
}
469+
ofs << ifs.rdbuf();
470+
}
471+
472+
// Return to beginning of file and read as JSON
473+
ifs.clear();
474+
ifs.seekg(0);
475+
nlohmann::json nb;
476+
ifs >> nb;
477+
478+
// Clean cells
479+
auto& cell_list = nb["cells"];
480+
for (auto it = cell_list.begin(), end = cell_list.end(); it != end; ++it) {
481+
if (it->contains("cells"))
482+
it->erase("cells");
483+
if (it->value("cell_type", "") == "latex") {
484+
(*it)["hidden"] = false;
485+
std::map<std::string, nlohmann::json> output_cell;
486+
output_cell["cell_id"] = cadabra::generate_uuid<uint64_t>();
487+
output_cell["cell_origin"] = "client";
488+
output_cell["cell_type"] = "latex_view";
489+
output_cell["source"] = "~";
490+
(*it)["cells"] = std::vector<nlohmann::json>(1, output_cell);
491+
}
492+
}
493+
494+
// Write file
495+
ifs.close();
496+
std::ofstream ofs(a);
497+
if (!ofs.is_open()) {
498+
std::cerr << "Could not open " << a << " for writing\n";
499+
exit(1);
500+
}
501+
ofs << std::setw(4) << nb << '\n';
502+
}
503+
450504
int run(int argc, char** argv)
451505
{
452506
#if _MSC_VER
@@ -491,6 +545,13 @@ int run(int argc, char** argv)
491545
else if (strcmp(argv[1], "gitmerge") == 0) {
492546
std::cerr << "Sorry, not yet implemented!\n";
493547
}
548+
else if (strcmp(argv[1], "clean") == 0) {
549+
if (argc != 3) {
550+
std::cerr << "Wrong number of arguments passed to clean, expected 1 filename\n";
551+
exit(1);
552+
}
553+
clean(argv[2]);
554+
}
494555
else {
495556
std::cerr << "Unrecognised option " << argv[1] << '\n';
496557
help();

0 commit comments

Comments
 (0)