Skip to content

Commit afaed0f

Browse files
committed
Added options to “Anki” button
- Fixed a bug with “<“ sign, now it is exported as \lt
1 parent f37b0c6 commit afaed0f

6 files changed

Lines changed: 1175 additions & 966 deletions

File tree

Mathematica/ScienceNotebooks/AnkiExporter/AnkiExporter.wl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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)*)
6969
PrepareAnkiNote[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},
7373
res=(ImportString[AnkiRequest["addNotes",{"notes"->(PrepareAnkiNote[deck,#[[1]],#[[2]],#[[3]],#[[4]],#[[5]]]&/@rawNotes)}],"JSON"]);
74-
PrintToConsole[res];
74+
If[debug,PrintToConsole[res]];
7575
res=("result"/.res);
7676
toUpdate=#[[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];
103103
su=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]];
104104
StringDrop[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},
107107
If[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];
109109
separator="#";
110110
ShowStatus["Export starts"];
111-
PrintToConsole["Export starts"];
111+
If[debug,PrintToConsole["Export starts"]];
112112
ExportString["Exp","TeXFragment"];
113113
(*System`Convert`TeXFormDump`maketex["\[LeftSkeleton]"]="\\ll ";
114114
System`Convert`TeXFormDump`maketex["\[RightSkeleton]"]="\\gg ";*)
@@ -128,6 +128,8 @@ System`Convert`TeXFormDump`maketex["\[Perpendicular]"]="\\perp ";
128128
System`Convert`TeXFormDump`maketex["\[TensorWedge]"]="\\wedge ";System`Convert`TeXFormDump`maketex["\[Wedge]"]="\\wedge ";
129129
System`Convert`TeXFormDump`maketex["\[TensorProduct]"]="\\otimes ";
130130
System`Convert`TeXFormDump`maketex["\[GreaterTilde]"]="\\gtrsim ";
131+
System`Convert`TeXFormDump`maketex["<"]="\\lt ";
132+
System`Convert`TeXFormDump`maketex[">"]="\\gt ";
131133
System`Convert`TeXFormDump`maketex["\[LineSeparator]"]="\n";
132134
System`Convert`TeXFormDump`maketex[":="]=":=";
133135

@@ -341,12 +343,14 @@ FrontEndTokenExecute[EvaluationNotebook[],"SelectionOpenAllGroups"];
341343
MapThread[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+
344348
ShowStatus["Exporting to Anki..."];
345349
deck=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];
350354
ShowStatus["Notebook Exported"];
351355
];
352356
End[];

0 commit comments

Comments
 (0)