|
1 | 1 | #include <internal/difflib.h> |
2 | 2 | #include <internal/string_tools.h> |
| 3 | +#include <internal/uuid.h> |
3 | 4 | #include <iostream> |
4 | 5 | #include <iomanip> |
5 | 6 | #include <fstream> |
@@ -293,7 +294,10 @@ void help() |
293 | 294 | << " | [merge \"cadabranotebook\"]\n" |
294 | 295 | << " | command = cdb-nbtool gitmerge\n" |
295 | 296 | << " 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"; |
297 | 301 | } |
298 | 302 |
|
299 | 303 | void view(const char* fname) |
@@ -447,6 +451,56 @@ void gitdiff(const char* a, const char* b, const char* relpath) |
447 | 451 | cnb_diff(af, bf); |
448 | 452 | } |
449 | 453 |
|
| 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 | + |
450 | 504 | int run(int argc, char** argv) |
451 | 505 | { |
452 | 506 | #if _MSC_VER |
@@ -491,6 +545,13 @@ int run(int argc, char** argv) |
491 | 545 | else if (strcmp(argv[1], "gitmerge") == 0) { |
492 | 546 | std::cerr << "Sorry, not yet implemented!\n"; |
493 | 547 | } |
| 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 | + } |
494 | 555 | else { |
495 | 556 | std::cerr << "Unrecognised option " << argv[1] << '\n'; |
496 | 557 | help(); |
|
0 commit comments