Cadabra
Computer algebra system for field theory problems
DataCell.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <string>
5 #include <mutex>
6 
7 #include "tree.hh"
8 #include "nlohmann/json.hpp"
9 
10 namespace cadabra {
11 
26 
27  class DataCell {
28  public:
29 
33 
34  enum class CellType {
35  document,
36  python,
37  latex,
38  output,
39  verbatim,
40  latex_view,
41  input_form,
42  image_png,
43  image_svg,
44  error,
45  // section
46  };
47 
51 
52  class id_t {
53  public:
54  id_t();
55 
56  uint64_t id;
58 
59  bool operator<(const id_t& other) const;
60  };
61 
63 
64  DataCell(CellType t=CellType::python, const std::string& str="", bool hidden=false);
65 
68 
69  DataCell(id_t, CellType t=CellType::python, const std::string& str="", bool hidden=false);
70 
72 
73  DataCell(const DataCell&);
74 
76 
81 
82  std::string textbuf;
83 
88 
89  bool hidden;
90  bool sensitive;
91 
94 
96 
100 
101  bool running;
102 
103  id_t id() const;
104 
105  private:
107  };
108 
109  typedef tree<DataCell> DTree;
110 
113 
114  std::string JSON_serialise(const DTree&);
115  void JSON_recurse(const DTree&, DTree::iterator, nlohmann::json&);
116 
118 
119  void JSON_deserialise(const std::string&, DTree&);
120  void JSON_in_recurse(DTree& doc, DTree::iterator loc, const nlohmann::json& cells);
121 
123 
124  std::string export_as_HTML(const DTree& doc, bool for_embedding=false, bool
125  strip_code=false, std::string title="");
126  void HTML_recurse(const DTree& doc, DTree::iterator it, std::ostringstream& str,
127  const std::string& preamble_string,
128  bool for_embedding=false, bool strip_code=false, std::string title="");
129 
132 
133  std::string latex_to_html(const std::string&);
134 
137 
138  std::string export_as_LaTeX(const DTree& doc, const std::string& image_file_base, bool for_embedding=false);
139  void LaTeX_recurse(const DTree& doc, DTree::iterator it, std::ostringstream& str,
140  const std::string& preamble_string, const std::string& image_file_base,
141  int& image_num, bool for_embedding);
142 
145 
146  std::string export_as_python(const DTree& doc);
147  void python_recurse(const DTree& doc, DTree::iterator it, std::ostringstream& str);
148 
150  // std::string replace_all(std::string, const std::string& old, const std::string& nw);
151 
154 
155  nlohmann::json ipynb2cnb(const nlohmann::json&);
156 
158 
159  nlohmann::json cnb2ipynb(const nlohmann::json&);
160 }
Each cell is identified by a serial number 'id' which is used to keep track of it across network call...
Definition: DataCell.hh:52
uint64_t id
Definition: DataCell.hh:56
bool operator<(const id_t &other) const
Definition: DataCell.cc:31
bool created_by_client
Definition: DataCell.hh:57
id_t()
Definition: DataCell.cc:38
DataCells are the basic building blocks for a document.
Definition: DataCell.hh:27
CellType cell_type
Definition: DataCell.hh:75
bool running
Indicator whether this cell is currently being evaluated by the server.
Definition: DataCell.hh:101
bool ignore_on_import
Flag indicating whether this cell should be ignored in case the notebook is imported.
Definition: DataCell.hh:95
CellType
Cells are labelled with the data type of its contents, which is stored in a textural representation b...
Definition: DataCell.hh:34
@ input_form
output: cell containing input form of preceding output cell
@ python
input : editor cell for code in python
@ latex
input : editor cell for code in latex
@ image_svg
output: cell showing an SVG image
@ output
output: cell showing python stdout, verbatim
@ latex_view
output: cell showing LaTeX text formatted using LaTeX
@ error
output: cell showing LaTeX text for errors
@ image_png
output: cell showing a base64 encoded PNG image
@ verbatim
output: cell showing other verbatim output
@ document
head node, only one per document
bool hidden
Flag indicating whether this cell should be hidden from view.
Definition: DataCell.hh:89
id_t id() const
Definition: DataCell.cc:534
id_t serial_number
Definition: DataCell.hh:106
bool sensitive
Definition: DataCell.hh:90
DataCell(CellType t=CellType::python, const std::string &str="", bool hidden=false)
Standard constructor, generates a new unique id for this DataCell.
Definition: DataCell.cc:44
std::string textbuf
Textual representation of the cell content.
Definition: DataCell.hh:82
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83
void LaTeX_recurse(const DTree &doc, DTree::iterator it, std::ostringstream &str, const std::string &preamble_string, const std::string &image_file_base, int &image_num, bool for_embedding)
Definition: DataCell.cc:562
nlohmann::json cnb2ipynb(const nlohmann::json &)
Convert the JSON of a Cadabra notebook to a Jupyter notebook.
Definition: DataCell.cc:794
std::string export_as_HTML(const DTree &doc, bool for_embedding=false, bool strip_code=false, std::string title="")
Export a document to a single self-contained HTML file containing inline CSS.
Definition: DataCell.cc:74
std::string export_as_python(const DTree &doc)
Export a document to a python-like file (converting text cells to comments and python cells to python...
Definition: DataCell.cc:693
tree< DataCell > DTree
Definition: DataCell.hh:109
nlohmann::json ipynb2cnb(const nlohmann::json &)
Replace all occurrences of a substring in the original string.
Definition: DataCell.cc:734
void JSON_deserialise(const std::string &, DTree &)
Load a document from .cnb format, i.e. the inverse of the above.
Definition: DataCell.cc:389
std::string latex_to_html(const std::string &)
Convert various LaTeX constructions to HTML-with-Mathjax, e.g.
Definition: DataCell.cc:92
void JSON_in_recurse(DTree &doc, DTree::iterator loc, const nlohmann::json &cells)
Definition: DataCell.cc:424
std::string export_as_LaTeX(const DTree &doc, const std::string &image_file_base, bool for_embedding=false)
Export a document to a single self-contained LaTeX file, with the exception of images which get saved...
Definition: DataCell.cc:539
void JSON_recurse(const DTree &, DTree::iterator, nlohmann::json &)
Definition: DataCell.cc:316
void python_recurse(const DTree &doc, DTree::iterator it, std::ostringstream &str)
Definition: DataCell.cc:701
void HTML_recurse(const DTree &doc, DTree::iterator it, std::ostringstream &str, const std::string &preamble_string, bool for_embedding=false, bool strip_code=false, std::string title="")
Definition: DataCell.cc:176
std::string JSON_serialise(const DTree &)
Serialise a document into .cj format, which is a JSON version of the document tree.
Definition: DataCell.cc:305