@@ -745,60 +745,62 @@ nlohmann::json cadabra::ipynb2cnb(const nlohmann::json& root)
745745
746746nlohmann::json cadabra::cnb2ipynb (const nlohmann::json& root)
747747 {
748- auto nbf = root[" nbformat" ];
749- nlohmann::json json;
748+ nlohmann::json ipynb, kernelspec, lang;
750749
751- if (!nbf)
752- return json ;
750+ ipynb[ " nbformat " ]= 4 ;
751+ ipynb[ " nbformat_minor " ]= 4 ;
753752
754- json[" description" ]=" Cadabra JSON notebook format" ;
755- json[" version" ]=1.0 ;
753+ kernelspec[" display_name" ]=" Cadabra2" ;
754+ kernelspec[" language" ]=" python" ;
755+ kernelspec[" name" ]=" cadabra2" ;
756+
757+ lang[" codemirror_mode" ]=" cadabra" ;
758+ lang[" file_extension" ]=" .ipynb" ;
759+ lang[" mimetype" ]=" text/cadabra" ;
760+ lang[" name" ]=" cadabra2" ;
761+ lang[" pygments_lexer" ]=" cadabra" ;
762+
763+ ipynb[" metadata" ] = {
764+ {" kernelspec" , kernelspec},
765+ {" language_info" , lang}
766+ };
756767
757768 nlohmann::json cells=nlohmann::json::array ();
758- const nlohmann::json& jucells=root[" cells" ];
759769
760770 // Jupyter notebooks just have a single array of cells; walk
761- // through and add to our " cells" array.
771+ // through and setup our cells array.
762772
763- for (unsigned int c= 0 ; c<jucells. size (); ++c ) {
773+ for (const auto & cdb: root[ " cells " ] ) {
764774 nlohmann::json cell;
765- if (jucells[c][" cell_type" ].get <std::string>()==" markdown" )
766- cell[" cell_type" ]=" latex" ;
767- else
768- cell[" cell_type" ]=" input" ;
769- cell[" hidden" ]=false ;
770- const nlohmann::json& source=jucells[c][" source" ];
771- // Jupyter stores the source line-by-line in an array 'source'.
772- std::string block;
773- for (unsigned int l=0 ; l<source.size (); ++l) {
774- std::string line=source[l].get <std::string>();
775- if (line.size ()>0 ) {
776- if (line[0 ]==' #' ) {
777- std::string sub=" " ;
778- line=line.substr (1 );
779- int inc=0 ;
780- while (line.size ()>0 && line[0 ]==' #' ) {
781- if (inc<2 ) {
782- sub+=" sub" ;
783- inc+=1 ;
784- }
785- line=line.substr (1 );
786- }
787- if (line.size ()==0 )
788- continue ;
789- if (line[line.size ()-1 ]==' \n ' )
790- line=line.substr (0 ,line.size ()-1 );
791- block+=" \\ " +sub+" section*{" +trim (line)+" }\n " ;
792- }
793- else
794- block+=line;
795- }
775+ if (cdb[" cell_type" ]==" python" ) {
776+ cell[" cell_type" ]=" code" ;
777+ cell[" source" ]=nlohmann::json::array ();
778+ cell[" source" ].push_back (cdb[" source" ]);
779+ cell[" metadata" ]=nlohmann::json::object ();
780+ cell[" outputs" ]=nlohmann::json::array ();
781+ cell[" execution_count" ]={};
796782 }
797- cell[" source" ]=block;
798- cells.push_back (cell);
783+ if (cdb[" cell_type" ]==" input" ) {
784+ cell[" cell_type" ]=" code" ;
785+ cell[" source" ]=nlohmann::json::array ();
786+ cell[" source" ].push_back (cdb[" source" ]);
787+ cell[" metadata" ]=nlohmann::json::object ();
788+ cell[" outputs" ]=nlohmann::json::array ();
789+ cell[" execution_count" ]={};
790+ }
791+ else if (cdb[" cell_type" ]==" latex" ) {
792+ cell[" cell_type" ]=" markdown" ;
793+ cell[" source" ]=nlohmann::json::array ();
794+ cell[" source" ].push_back (cdb[" source" ]);
795+ cell[" metadata" ]=nlohmann::json::object ();
796+ }
797+
798+ if (cell.is_null ()==false )
799+ cells.push_back (cell);
800+
799801 }
800802
801- json [" cells" ] = cells;
803+ ipynb [" cells" ] = cells;
802804
803- return json ;
805+ return ipynb ;
804806 }
0 commit comments