@@ -62,20 +62,20 @@ AnkiHighlight[]:=Module[{nb, t}, nb = InputNotebook[];
6262
6363(* ::Input::Initialization:: *)
6464(*Anki Connect: Pass Action and param*)
65- AnkiRequest [action_ ,params_ :< ||> ]:= Block [{req ,json },PrintToConsole [ params ] ; json = ImportString [ExportString [< |"action" -> action ,"version" -> 6 ,"params" -> params |> ,"JSON" ,"Compact" -> False ],"Text" ]; PrintToConsole [json ]; req = HTTPRequest [< |"Scheme" -> "http" ,"Domain" -> "localhost" ,"Port" -> 8765 ,Method -> "POST" ,"Body" -> json |> ]; URLRead [req ,"Body" ]
65+ AnkiRequest [action_ ,params_ :< ||> , debug_ : False ]:= Block [{req ,json },json = ImportString [ExportString [< |"action" -> action ,"version" -> 6 ,"params" -> params |> ,"JSON" ,"Compact" -> False ],"Text" ]; If [ debug , PrintToConsole [json ] ]; req = HTTPRequest [< |"Scheme" -> "http" ,"Domain" -> "localhost" ,"Port" -> 8765 ,Method -> "POST" ,"Body" -> json |> ]; URLRead [req ,"Body" ]
6666];
6767
6868(*Anki Connect: Pass deck name then for params and tags (opt)*)
6969PrepareAnkiNote [deckName_ ,cellID_ ,clozed_ ,title_ ,link_ ,tags_ :{}]:= {"deckName" -> deckName ,"modelName" -> "MathematicaCloze" ,"fields" -> {"CellID" -> ToString [cellID ],"Text" -> clozed ,"Extra" -> title ,"Link" -> link },"options" -> {"allowDuplicate" -> False },"tags" -> tags };
7070
7171(*Anki Connect: Pass deck name then all notes*)
72- AddOrUpdateNotes [deck_ ,rawNotes_ ]:= Block [{res ,resi ,toUpdate ,ids ,ankiIds },
72+ AddOrUpdateNotes [deck_ ,rawNotes_ , debug_ ]:= Block [{res ,resi ,toUpdate ,ids ,ankiIds },
7373res = (ImportString [AnkiRequest ["addNotes" ,{"notes" -> (PrepareAnkiNote [deck ,# [[1 ]],# [[2 ]],# [[3 ]],# [[4 ]],# [[5 ]]]& /@ rawNotes )}],"JSON" ]);
74- PrintToConsole [res ];
74+ If [ debug , PrintToConsole [res ] ];
7575res = ("result" /. res );
7676toUpdate = # [[2 ]]& /@ Select [Thread [{res ,rawNotes }],# [[1 ]]== Null & ];
77- PrintToConsole ["Need to update " <> ToString [Length [toUpdate ]]<> " notes" ];
78- If [Length [toUpdate ]> 0 ,res = {"actions" -> ({"action" -> "findNotes" ,"params" -> {"query" -> "CellID:" <> ToString [# [[1 ]]]}}& /@ toUpdate )}; ankiIds = # [[1 ]]& /@ ("result" /. ImportString [AnkiRequest ["multi" ,res ],"JSON" ]); toUpdate = Thread [{ankiIds ,toUpdate }]; PrintToConsole [ankiIds ]; PrintToConsole [toUpdate ]; res = {"actions" -> ({"action" -> "updateNoteFields" ,"params" -> {"note" -> {"id" -> ToString [# [[1 ]]],"deckName" -> deck ,"modelName" -> "MathematicaCloze" ,"fields" -> {"CellID" -> ToString [# [[2 ,1 ]]],"Text" -> # [[2 ,2 ]],"Extra" -> # [[2 ,3 ]],"Link" -> # [[2 ,4 ]]},"options" -> {"allowDuplicate" -> False }}}}& /@ toUpdate )}; ImportString [AnkiRequest ["multi" ,res ],"JSON" ]];
77+ If [ debug , PrintToConsole ["Need to update " <> ToString [Length [toUpdate ]]<> " notes" ] ];
78+ If [Length [toUpdate ]> 0 ,res = {"actions" -> ({"action" -> "findNotes" ,"params" -> {"query" -> "CellID:" <> ToString [# [[1 ]]]}}& /@ toUpdate )}; ankiIds = # [[1 ]]& /@ ("result" /. ImportString [AnkiRequest ["multi" ,res ],"JSON" ]); toUpdate = Thread [{ankiIds ,toUpdate }]; If [ debug , PrintToConsole [ankiIds ]] ; If [ debug , PrintToConsole [toUpdate ] ]; res = {"actions" -> ({"action" -> "updateNoteFields" ,"params" -> {"note" -> {"id" -> ToString [# [[1 ]]],"deckName" -> deck ,"modelName" -> "MathematicaCloze" ,"fields" -> {"CellID" -> ToString [# [[2 ,1 ]]],"Text" -> # [[2 ,2 ]],"Extra" -> # [[2 ,3 ]],"Link" -> # [[2 ,4 ]]},"options" -> {"allowDuplicate" -> False }}}}& /@ toUpdate )}; ImportString [AnkiRequest ["multi" ,res ],"JSON" ]];
7979(*PrintToConsole[ImportString[AnkiRequest["updateNoteFields",{"note"\[Rule]{"id"\[Rule]#[[1]],"fields"\[Rule]{"CellID"\[Rule]ToString[#[[2,1]]],"Text"\[Rule]#[[2,2]],"Extra"\[Rule]#[[2,3]],"Link"\[Rule]#[[2,4]]}}}],"JSON"]]&/@toUpdate;*)
8080(*PrintToConsole[ImportString[AnkiRequest["notesInfo",{"notes"\[Rule]toUpdate[[All,1]]}],"JSON"]];*)
8181];
@@ -103,12 +103,12 @@ s=CloudDeploy[EvaluationNotebook[],cn];
103103su = Quiet @ CloudDeploy [URLDispatcher [{"/" ~~ EndOfString :> Delayed @ CloudImport @ s ,"/" ~~ base :Repeated [DigitCharacter ,20 ]:> Delayed @ ExportForm [Notebook [Append [First @ CloudGet @ s ,cell @ EmbeddedHTML ["<script> function openCloseGroup() { wolfram.cloud.parentNotebook.evaluateExpression({ expression: 'FrontEndExecute[NotebookLocate[\" " <> base <> "\" ]]' }); } </script> <body onload='openCloseGroup()'>" ]],(Rest @ s )/. {Notebook -> Sequence }],"CloudCDF" ]}],StringDrop [cn ,- 3 ]];
104104StringDrop [StringSplit [CloudConnect [],"@" ][[1 ]]<> fn ,- 3 ]
105105];
106- ExportToAnki [sync_ :True ]:= Module [{separator ,styleTags ,cells ,sections ,subsections ,subsubsections ,subsubsubsections ,allinfo ,cellids ,celltags ,data ,ids ,cloze ,matchEq ,encoding ,eqCloze ,GetTOC ,exported ,filtered ,splited ,marked ,paths ,fixed ,final ,threaded ,deck ,title , base ,dat ,ndir ,tempPicPath , allspecial ,npath ,backupc ,cloudex },
106+ ExportToAnki [sync_ :True , debug_ : False ]:= Module [{separator ,styleTags ,cells ,sections ,subsections ,subsubsections ,subsubsubsections ,allinfo ,cellids ,celltags ,data ,ids ,cloze ,matchEq ,encoding ,eqCloze ,GetTOC ,exported ,filtered ,splited ,marked ,paths ,fixed ,final ,threaded ,deck ,title , base ,dat ,ndir ,tempPicPath , allspecial ,npath ,backupc ,cloudex },
107107If [NotebookDirectory []=== $Failed ,ShowStatus ["ExportAnki failed. Notebook must be saved first!" ]; Abort []];
108- cloudex = If [! $CloudConnected && $WolframID === None ,If [CloudConnect []=== $Failed ||CloudConnect []=== $Canceled ,False ,True ],True ];
108+ cloudex = sync && If [! $CloudConnected && $WolframID === None ,If [CloudConnect []=== $Failed ||CloudConnect []=== $Canceled ,False ,True ],True ];
109109separator = "#" ;
110110ShowStatus ["Export starts" ];
111- PrintToConsole ["Export starts" ];
111+ If [ debug , PrintToConsole ["Export starts" ] ];
112112ExportString ["Exp" ,"TeXFragment" ];
113113(*System`Convert`TeXFormDump`maketex["\[LeftSkeleton]"]="\\ll ";
114114System`Convert`TeXFormDump`maketex["\[RightSkeleton]"]="\\gg ";*)
@@ -128,6 +128,8 @@ System`Convert`TeXFormDump`maketex["\[Perpendicular]"]="\\perp ";
128128System ` Convert ` TeXFormDump ` maketex ["\[ TensorWedge]" ]= "\\ wedge " ; System ` Convert ` TeXFormDump ` maketex ["\[ Wedge]" ]= "\\ wedge " ;
129129System ` Convert ` TeXFormDump ` maketex ["\[ TensorProduct]" ]= "\\ otimes " ;
130130System ` Convert ` TeXFormDump ` maketex ["\[ GreaterTilde]" ]= "\\ gtrsim " ;
131+ System ` Convert ` TeXFormDump ` maketex ["<" ]= "\\ lt " ;
132+ System ` Convert ` TeXFormDump ` maketex [">" ]= "\\ gt " ;
131133System ` Convert ` TeXFormDump ` maketex ["\[ LineSeparator]" ]= "\n " ;
132134System ` Convert ` TeXFormDump ` maketex [":=" ]= ":=" ;
133135
@@ -341,12 +343,14 @@ FrontEndTokenExecute[EvaluationNotebook[],"SelectionOpenAllGroups"];
341343MapThread [SetOptions [#1 ,CellTags -> #2 ]& ,{Cells [EvaluationNotebook [],CellID -> ids ],backupc }];
342344,filtered = Select [Thread [{ids ,base ,paths ,npath ,celltags }],StringMatchQ [# [[2 ]],"*{{c@::*" ]& ];
343345];
346+ If [debug ,PrintToConsole [filtered ](*; Abort[]*) ];
347+
344348ShowStatus ["Exporting to Anki..." ];
345349deck = StringReplace [StringReplace [ndir ,e___ ~~ "/Knowledge/" ~~ f___ ~~ "/" :> f ],"/" :> "::" ];
346- PrintToConsole [deck ];
347- PrintToConsole [ AnkiRequest ["createDeck" ,< |"deck" -> deck |> ] ];
348- AddOrUpdateNotes [deck ,filtered ];
349- If [ sync , PrintToConsole [ AnkiRequest ["sync" ]] ];
350+ If [ debug , PrintToConsole [deck ]] ;
351+ AnkiRequest ["createDeck" ,< |"deck" -> deck |> , debug ];
352+ AddOrUpdateNotes [deck ,filtered , debug ];
353+ AnkiRequest ["sync" , < || > , debug ];
350354ShowStatus ["Notebook Exported" ];
351355];
352356End [];
0 commit comments