-
Notifications
You must be signed in to change notification settings - Fork 4
/
manage.py
executable file
·1160 lines (842 loc) · 39.9 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/python3
import sys
import shutil
from LatexZettel import files, database
import re
import os
import peewee as pw
import datetime
import platform
OPEN_COMMAND = 'xdg-open'
if platform.system() == 'Darwin':
OPEN_COMMAND = 'open'
elif platform.system() == 'Windows':
OPEN_COMMAND = 'start'
class Helper:
"""
Collection of functions for managing a LaTeX Zettelkasten. Each of these functions can be run from the command line by running
`python manage.py function_name arg1 arg2 ...`
For example, running
`python manage.py newnote note_name`
executes `Hepler.newnote(note_name)`.
These can be executed by hand (for now). The plan for the future is to get other applications (eg a text editor) to run these functions.
"""
renderers = {'pdf': ['pdflatex', ['--interaction=scrollmode']], 'html': ['make4ht', ['-um', 'draft', '-c', os.path.join('..', 'config', 'make4ht.cfg'), '-']]} # {'format': ['command_line_command', ['list', 'of', 'commandline', 'options']]}
def help():
print("""
Manage the LaTeX slip box. The file manage.py is documented with docstrings, see /docs for detailed information
""")
def addtodocuments(filename, reference=""):
"""
Adds line \externaldocument[reference-]{filename} to documents.tex
If reference is not supplied then it defaults to, for example, NoteName if filename=note_name
"""
with open('notes/documents.tex', 'a') as f:
f.write(f'\externaldocument[{reference}-]{{{filename}}}\n')
def newnote_md(note_name, reference_name=""):
"""
Creates a new markdown note with the given title. Otherwise, same functionality as Helper.newnote()
"""
Helper.newnote(note_name, reference_name, extension='md')
def newnote(note_name, reference_name="", **kwargs):
"""
Makes a new note with name note_name [Optional ReferenceName]
Creates note with name note_name.tex, Second argument is optional and is the name in the reference, defaults to NoteName
to do: check whether a file already exists etc
"""
if reference_name == "":
reference_name = ''.join([w.capitalize() for w in note_name.split('_')])
#see if the note already exists
try:
note = database.Note.get(filename=note_name)
raise ValueError(f'A note with file name {note_name} already exists in the database. If this is not the case then run manage.py synchronize to update the database, and then try again')
return
except pw.OperationalError:
database.create_all_tables()
except database.Note.DoesNotExist:
try:
note = database.Note.get(reference=reference_name)
raise ValueError(f'A note with reference {reference} already exists in the database. If this is not the case then run manage.py synchronize to update the database, and then try again. If the problem persists check the documents.tex file is correctly setup')
return
except database.Note.DoesNotExist:
pass
try:
ext = kwargs['extension']
except KeyError:
ext = 'tex'
Helper.__createnotefile(note_name, ext)
Helper.addtodocuments(note_name, reference_name)
#once created, add note to database
note = database.Note(filename=note_name, reference=reference_name, created = datetime.datetime.now(), last_edit_date = datetime.datetime.now())
note.save()
def newproject(dir_name, filename=None):
"""
Creates a project folder (for exporting notes) and copies the project.tex file into the folder
"""
try:
os.mkdir('projects')
except FileExistsError:
pass
try:
dirpath = os.path.join('projects', dir_name)
os.mkdir(dirpath)
except FileExistsError:
raise Exception('Error: project directory already exists')
if filename is None:
filename = f'{dir_name}.tex'
template = os.path.join('template', 'project.tex')
tex_file = os.path.join(dirpath, filename)
shutil.copyfile(template, tex_file)
def list_recent_files(n = 10):
"""
List the most recently edited files.
"""
recent_files = Helper.__get_recent_files(int(n))
for i, f in enumerate(recent_files):
print(f'{i + 1}:\t', os.path.split(f)[-1][:-4])
def rename_recent(n = 1):
"""
Rename the most recent nth file.
"""
Helper.synchronize()
n = int(n)
file = os.path.split(Helper.__get_recent_files(int(n))[n-1])[-1][:-4]
db_file = database.Note.get(filename=file)
new_name = input(f"Change file name to [{file}]: ") or db_file.filename
new_reference = input(f"Change reference to [{db_file.reference}]: ") or db_file.reference
if new_name != db_file.filename:
Helper.rename_file(db_file.filename, new_name)
if new_reference != db_file.reference:
Helper.rename_reference(db_file.reference, new_reference)
def rename_file(old_filename, new_filename):
"""
Rename the .tex file, fairly straightforward since only need to change documents.tex
"""
try:
with open(f'notes/slipbox/{new_filename}.tex', 'r'):
pass
raise ValueError(f'File, {new_filename}.tex already exists')
except FileNotFoundError:
pass
#copy file
shutil.copy(f'notes/slipbox/{old_filename}.tex', f'notes/slipbox/{new_filename}.tex')
os.remove(f'notes/slipbox/{old_filename}.tex')
#change documents.tex
note = database.Note.get(filename=old_filename)
note.filename = new_filename
note.save()
lines = bytearray()
with open('notes/documents.tex', 'r') as f:
for line in f:
lines.extend(re.sub('\\\\externaldocument\[' + note.reference + '\-\]\{' + old_filename + '\}', '\\\\externaldocument[' + note.reference + '-]{' + new_filename + '}', line).encode())
with open('notes/documents.tex', 'wb') as f:
f.write(lines)
def rename_reference(old_reference, new_reference):
"""
Rename the reference used throughout the whole Zettelkasten. This function changes documents.tex and also any documents that reference this note.
"""
#function for regex replacement
Helper.synchronize()
def replace_text(m):
if m.group(1) is None and m.group(3) is None:
return f'\\excref{new_reference}'
elif m.group(1) is None and m.group(3) is not None:
return f'\\excref[{m.group(4)}]{{{new_reference}}}'
elif m.group(1) is not None and m.group(3) is None:
return f'\\exhyperref{new_reference}'
elif m.group(1) is not None:
return f'\\exhyperref[{m.group(4)}]{{{new_reference}}}'
# Update documents.tex
note = database.Note.get(reference=old_reference)
lines = bytearray()
with open('notes/documents.tex', 'r') as f:
for line in f:
lines.extend(re.sub('\\\\externaldocument\[' + note.reference + '\-\]\{' + note.filename + '\}', '\\\\externaldocument[' + new_reference + '-]{' + note.filename + '}', line).encode())
with open('notes/documents.tex', 'wb') as f:
f.write(lines)
for label in note.labels:
backrefs = set()
for backref in label.referenced_by:
backrefs.add(backref)
for backref in backrefs:
lines = bytearray()
with open(f'notes/slipbox/{backref.source.filename}.tex', 'r') as f:
for line in f:
lines.extend(re.sub('\\\\ex(hyper)?(c)?ref(\[([^]]+)\])?\{' + old_reference + '\}', lambda m: replace_text(m), line).encode())
with open(f'notes/slipbox/{backref.source.filename}.tex', 'wb') as f:
f.write(lines)
#update note db
note.reference = new_reference
note.save()
def remove_note(filename):
"""
Delete a note with given filename
"""
try:
note = database.Note.get(filename=filename)
print('Delete database entry? (y/n)')
if Helper.__getyesno():
note.delete_instance()
except database.Note.DoesNotExist:
note = None
print(f'No note with filename {filename} exists in db')
with open('notes/documents.tex', 'r') as f:
lines = f.readlines()
to_delete= []
for i, line in enumerate(lines):
m = re.search(f'(\\\\externaldocument\[)(.+?)(\-\]\{{){filename}(\}})', line)
if m:
to_delete.append(i)
for i in reversed(to_delete):
print(f'delete line {lines[i].strip()} from notes/documents.tex? (y/n)')
if Helper.__getyesno():
lines.pop(i)
with open('notes/documents.tex', 'w') as f:
for line in lines:
f.write(line)
print(f'Delete notes/slipbox/{filename}.tex? (y/n)')
if Helper.__getyesno():
try:
os.remove(f'notes/slipbox/{filename}.tex')
except FileNotFoundError:
print('Error, no such file exists')
def sync_md():
#check that the database is setup correctly, and that the slipbox folder exists
database.create_all_tables()
try:
os.mkdir(os.path.join('notes', 'slipbox'))
except FileExistsError:
pass
markdown_files = files.get_files(os.path.join('notes', 'md'), 'md')
sb_file_names = {os.path.basename(f)[:-3]: "_".join([s for s in os.path.basename(f).split(" ")])[:-3] for f in markdown_files}
print('Warning: this may overwrite any files in notes/slipbox that share their filename with a file in notes/md. Do you wish to continue?')
if not Helper.__getyesno():
return
tracked_note_files = [note.filename for note in database.Note]
for file in markdown_files:
filename = os.path.basename(file)[:-3]
if sb_file_names[filename] not in tracked_note_files:
reference_name = ''.join([w.capitalize() for w in sb_file_names[filename].split('_')])
note = database.Note(filename=sb_file_names[filename], reference=reference_name, created = datetime.datetime.now(), last_edit_date = datetime.datetime.now())
note.save()
Helper.addtodocuments(sb_file_names[filename], reference_name)
for file in markdown_files:
filename = os.path.basename(file)[:-3]
sb_file = sb_file_names[filename]
print(sb_file)
try:
edit_delta = os.path.getmtime(file) - os.path.getmtime(os.path.join('notes', 'slipbox', f'{sb_file}.tex'))
except FileNotFoundError:
edit_delta = 1
#if edit_delta > 0:
if edit_delta > 0:
#change all the links in the document
def md_name_to_reference(m):
filename = m.group(1)
sb_name = '_'.join(filename.split(' '))
return database.Note.get(filename=sb_name).reference
with open(file, 'r') as f:
file_contents = f.read()
regex = "\[\[([^{\#\]\|}]+)\]\]"
text = re.sub(regex, lambda m: f"\\excref{{{md_name_to_reference(m)}}}", file_contents)
regex = "\[\[([^{\#\]\|}]+)\#\^?([^]]+)\]\]"
text = re.sub(regex, lambda m: f"\\excref[{m.group(2)}]{{{md_name_to_reference(m)}}}", text)
regex = "\[\[([^{\#\]\|}]+)\|([^]]+)\]\]"
text = re.sub(regex, lambda m: f"\\exhyperref{{{md_name_to_reference(m)}}}{{{m.group(2)}}}", text)
regex = "\[\[([^{\#\]\|}]+)\#\^?([^]]+)\|([^]]+)\]\]"
text = re.sub(regex, lambda m: f"\\exhyperref[{m.group(2)}]{{{md_name_to_reference(m)}}}{{{m.group(3)}}}", text)
import subprocess
command = 'pandoc'
options = ['-o', os.path.join('notes', 'slipbox', f'{sb_file}.tex')]
options += ['-s', '-t', 'latex', '--lua-filter=pandoc/filter.lua', '--template=pandoc/template.tex', '--metadata-file=pandoc/defaults.yaml', "--biblatex", "-M", f"title={filename}"]
process = subprocess.run([command, *options], input=text.encode(), capture_output=True)
if process.returncode != 0:
print('pandoc error:', process.stderr)
print(process.stdout)
def render(filename, format='pdf', biber=False):
"""
Render the LaTeX file. By default renders as a pdf and output is stored in the folder /pdf. The other format option is html, although support is currently experimental.
"""
if biber:
Helper.render(filename, format, False)
Helper.biber(filename)
import subprocess
command, options = Helper.renderers[format]
options = list(options)
try:
os.mkdir(format)
except FileExistsError:
pass
note = database.Note.get(filename=filename)
linked_files = set()
external_documents = ""
references = set()
for label in note.labels:
for link in label.referenced_by:
source = link.source
references.add(source)
linked_files.add(link.source.reference)
for link in note.references:
target_note = link.target.note
if link.target.note.last_build_date_html is not None:
references.add(target_note)
#inject external documents
if format == 'html':
for reference in references:
if reference.last_build_date_html is not None:
external_documents += f"\\externaldocument[{reference.reference}-]{{{reference.filename}}}\n"
referenced_by_section = "\\section*{Referenced In}\n\\begin{itemize}\n"
for reference in linked_files:
referenced_by_section += f"\\item \\excref{{{reference}}}"
referenced_by_section += "\\end{itemize}"
os.chdir(format)
path_to_file = os.path.join('..', 'notes', 'slipbox', f'{filename}.tex')
try:
with open(path_to_file, 'r'):
pass
except FileNotFoundError:
print('No such file!')
return '', f'Can\'t find {path_to_file}'
with open(path_to_file, 'r') as f:
contents = f.read()
if format == 'pdf':
options.insert(0, f"--jobname={filename}")
elif format == 'html':
options = ['-j', filename] + options + ['"svg-"']
#options = ['-j', filename] + options
document = contents.split('\\end{document}')[0]
if len(linked_files) > 0:
document += referenced_by_section
if format == 'html':
#replace include with preamble_html and inject external documents
document = document.replace("\\subimport{../template}{preamble.tex}", "\\subimport{../template}{preamble_html.tex}\n" + external_documents)
document = document.replace("\\documentclass{../template/texnote}", "\\documentclass{../template/texnote}\n" + external_documents)
document += "\\end{document}"
process = subprocess.run([command, *options], input=document.encode(), capture_output=True)
os.chdir('..')
if process.returncode != 0:
print('Failed to compile', process.stdout.decode()) #TODO: Exceptions
return process
if format == 'html':
note.last_build_date_html = datetime.datetime.now()
elif format == 'pdf':
note.last_build_date_pdf = datetime.datetime.now()
note.save()
return process
def render_all(format='pdf'):
"""
Function to replace renderallhtml and renderallpdf, rendering notes in a sensible order with regards to dependencies for PDFLaTeX links. Not currently implemented.
"""
pass
def biber(filename, folder='pdf'):
"""
Run biber on the render of the note. Folder can be either html or pdf, depending on the format.
"""
import subprocess
os.chdir(folder)
process = subprocess.run(['biber', filename], capture_output=True)
output, error = process.stdout, process.stderr
os.chdir('../')
return output, error
def render_all_html():
"""
Renderes all the notes using make4ht. Saves output in /html
"""
import subprocess
notes = Helper.__getnotefiles()
print('render pass 1')
for note in notes:
filename = os.path.split(note)[-1][:-4]
print(f'rendering {filename}...', end='')
process = Helper.render(filename, 'html')
print('done')
print('running biber...', end='')
output, error = Helper.biber(filename, 'html')
print('done')
print('render pass 2')
for note in notes:
filename = os.path.split(note)[-1][:-4]
print(f'rendering {filename}...', end='')
process = Helper.render(filename, 'html')
if process.returncode == 0:
print('done')
else:
print('\n',process.stderr)
def render_all_pdf():
"""
Renderes all the notes using pdflatex. Saves output in /pdf
"""
import subprocess
notes = Helper.__getnotefiles()
print('render pass 1')
for note in notes:
filename = os.path.split(note)[-1][:-4]
print(f'rendering {filename}...', end='')
output, error = Helper.render(filename)
if error == b'':
print('done')
print('running biber...', end='')
output, error = Helper.biber(filename)
print('done')
else:
print('error!')
print(error)
print('render pass 2')
for note in notes:
filename = os.path.split(note)[-1][:-4]
print(f'rendering {filename}...', end='')
output, error = Helper.render(filename)
if error == '':
print('done')
else:
print('\n',error)
def render_updates(format='pdf'):
updated, new_links, run_biber = Helper.synchronize()
for note in database.Note:
if note in updated:
continue
if format == 'pdf':
if note.last_build_date_pdf is None or note.last_edit_date > note.last_build_date_pdf:
updated.append(note)
run_biber[note] = True
#fix reference in
new_links.extend([r for r in note.references])
elif format == 'html':
if note.last_build_date_html is None or note.last_edit_date > note.last_build_date_html:
updated.append(note)
run_biber[note] = True
#fix referenced in
new_links.extend([r for r in note.references])
#render the updated files
for note in updated:
print(f'Rendering {note.filename}')
Helper.render(note.filename, format, run_biber[note])
rerendered = []
for link in new_links:
if link.target.note in rerendered:
continue
print(f'Rendering {link.target.note.filename}')
Helper.render(link.target.note.filename, format)
rerendered.append(link.target.note)
#rerender the originals if some other rendering has occured
rerendered = []
for link in new_links:
if link.source in rerendered:
continue
print(f'rendering {link.source.filename}')
Helper.render(link.source.filename, format)
rerendered.append(link.source)
def synchronize():
"""
Reads all the files that have been changed since the last call of this function and updates the database
"""
#loop through all the tracked notes and check for required updates
to_read = []
for note in database.Note:
#try and get the edit date from the file system.
try:
file = os.path.join('notes', 'slipbox', f'{note.filename}.tex')
modified = os.path.getmtime(file)
if datetime.datetime.fromtimestamp(modified) > note.last_edit_date:
to_read.append(note)
note.last_edit_date = datetime.datetime.fromtimestamp(modified)
note.save()
except FileNotFoundError:
#Todo: file has been deleted or renamed without the database being updated really need to run force_synchronize.
print(f'file not found for note with reference {note.reference}')
pass
#update labels
run_biber = {}
for note in to_read:
Helper.__update_labels(note)
run_biber[note] = Helper.__update_citations(note)
new_links = []
for note in to_read:
new_links.extend(Helper.__update_links(note))
return to_read, new_links, run_biber
def force_synchronize():
"""
Reads the file documents.tex and adds these files to the database (/slipbox.db) and checks for files in /notes that aren't in the documents. Then fixes and confilcts with these before reading the notes and creating database objects for labels, links and citations.
"""
database.create_all_tables()
notes = Helper.__getnotefiles()
notes = [os.path.split(note)[-1][:-4] for note in notes]
#get all the tracked notes (the ones in documents.tex)
tracked_notes = {}
with open('notes/documents.tex', 'r') as f:
for line in f:
m = re.search('(\\\\externaldocument\[)(.+?)(\-\]\{)(.+?)(\})', line)
if m:
reference_name = m.group(2)
filename = m.group(4)
if filename not in notes:
print(f'File {filename} with reference {reference_name} missing from notes. Make new note now? (y/n)')
if Helper.__getyesno():
Helper.__createnotefile(filename)
tracked_notes[filename] = reference_name
else:
tracked_notes[filename] = reference_name
for filename, reference_name in tracked_notes.items():
filepath = os.path.join('notes', 'slipbox', f'{filename}.tex')
modified = datetime.datetime.fromtimestamp(os.path.getmtime(filepath))
try:
note = database.Note.get(filename=filename)
note.last_edit_date = modified
if note.created is None:
note.created = note.last_edit_date
try:
html_render = datetime.datetime.fromtimestamp(os.path.getmtime(f'html/{filename}.html'))
note.last_build_date_html = html_render
except FileNotFoundError as e:
#print(f'{filename} is yet to be rendered as html')
pass
try:
pdf_render = datetime.datetime.fromtimestamp(os.path.getmtime(f'pdf/{filename}.pdf'))
note.last_build_date_pdf = pdf_render
except FileNotFoundError as e:
print(f'{filename} is yet to be rendered as pdf')
if note.reference == reference_name:
pass #nothing to do
else:
#update note reference to the one in documents.tex, might want to check that this is the right thing to do
note.reference = reference_name
note.save()
except database.Note.DoesNotExist:
try:
note = database.Note.get(reference=reference_name)
#update the filename in database, might want to check that this is the right thing to do
note.filename = filename
note.save()
except database.Note.DoesNotExist:
#create the note if there are no close matches
note = database.Note(filename=filename, reference=reference_name, created_at=modified, last_edit_date=modified)
note.save()
for note in notes:
#add any notes to documents.tex
if note not in tracked_notes:
print(f'File {note} not tracked by the file documents.tex. Add to the file now? (y/n)')
if Helper.__getyesno():
reference = ''.join([w.capitalize() for w in note.split('_')])
print(f'Reference (defaults to {reference}):', end='')
new_reference = input()
if new_reference != "":
reference = new_reference
try:
database.Note.create(filename=note, reference=reference)
Helper.addtodocuments(note, reference)
except pw.IntegrityError:
print('Error adding note, note already exists in database')
#add labels
for note in database.Note:
Helper.__update_labels(note)
Helper.__update_citations(note)
#add connections
for note in database.Note:
Helper.__update_links(note)
def list_unreferenced():
from LatexZettel import analysis
"""
Prints a list of notes that are not referenced in any other note. These might want to be added to the index, for example.
"""
import numpy as np
notes, adj_matrix = analysis.calculate_adjacency_matrix()
referenced_by = np.sum(adj_matrix, axis=0)
number = 1
for note, links_from in zip(notes, referenced_by):
if links_from == 0:
print(f'{number}: {note.filename}')
number += 1
def edit(filename=None):
"""
Open the note default text editor in the directory /notes/slipbox. If no file is passed then this opens
"""
import subprocess
if filename is None:
most_recent = Helper.__get_recent_files(1)[0]
os.chdir(os.path.join('notes','slipbox'))
subprocess.call([OPEN_COMMAND, os.path.split(most_recent)[1]])
else:
os.chdir(os.path.join('notes','slipbox'))
subprocess.call([OPEN_COMMAND, f'{filename}.tex'])
def to_md(note_name):
"""
Work in progress. Export a LaTeX document note to markdown, and convert references to [[WikiLink]] style references.
"""
def replace_string(m, md_links):
replacement = '[['
label = ''
if m.group(1) is None and m.group(2) == 'c':
replacement += md_links[(m.group(5), m.group(4))]
elif m.group(1) == 'hyper':
replacement += md_links[(m.group(5), m.group(4))] + '|' + m.group(7)
print(replacement)
return replacement + ']]'
# load the file and convert exhyperref and excref to Wiki links
file = []
file_references = []
with open(f'notes/slipbox/{note_name}.tex', 'r') as f:
for line in f:
file.append(line)
links = re.finditer('\\\\ex?(hyper)?(c)?ref(\[([^]+)\])?\{(.*?)\}', line)
for link in links:
if link.group(4) is None:
label = 'note'
else:
label = link.group(4)
file_references.append((link.group(5), label))
md_links = {}
for ref, tex_label in file_references:
try:
note = database.Note.get(reference=ref)
except database.Note.DoesNotExist:
note = None
if tex_label == 'note':
md_links[(ref, tex_label)] = note.filename
else:
md_links[(ref, tex_label)] = f'{note.filename}#{tex_label}'
new_file = bytearray()
for line in file:
new_file.extend((re.sub('\\\\ex(hyper)?(c)?ref(\[([^]]+)\])?\{(.*?)\}(\{(.*?)\})?', lambda m : replace_string(m, md_links), line)).encode())
import subprocess
print(new_file)
p = subprocess.run(["pandoc", "-t", "markdown", "-f", "latex", "-o", f"markdown/{note_name}.md" ], input=new_file, capture_output=True)
print(p.stdout, p.stderr)
def export_project(project_folder, texfile=None):
"""
Replaces \\transclude calls in a project file with the contents of the notes. Output is saved in project_dir/standalone
"""
if texfile is None:
texfile = f'{project_folder}.tex'
output_dir = os.path.join('projects', project_folder, 'standalone')
try:
os.mkdir(output_dir)
except FileExistsError:
print('Export already exists, continue? (Warning: This will overwrite any changes you have made to the file {texfile} in {output_dir})')
if not Helper.__getyesno():
return
output = bytearray()
input_file = os.path.join('projects', project_folder, texfile)
with open(input_file, 'r') as f:
for line in f:
output.extend((re.sub('\\\\transclude(\[[^]]+\]+)?\{([^}]+)\}', '', line).strip() + '\n').encode())
external_documents = re.finditer('\\\\transclude(\[([^]]+)\])?\{([^}]+)\}', line)
for document in external_documents:
tag = document.group(2)
document = document.group(3)
if tag is None:
tag = 'note'
note_file = os.path.join('notes', 'slipbox', f'{document}.tex')
with open(note_file, 'r') as in_file:
full_document = in_file.read()
import_text = re.search(f'%<\*{tag}>((.|\n)*?)%</{tag}>', full_document)
output.extend(import_text.group(1).strip().encode())
out_file = os.path.join(output_dir, texfile)
with open(out_file, 'wb') as f:
f.write(output)
def export_draft(input_file, output_file=None):
"""
Exports the export input_file (a file like /export/example.tex containing \ExecuteMetaData calls) and creates a .tex file (by default in /draft/filename.tex) where the \ExecuteMetadata commands are replaced with the body of the notes that they reference.
"""
if output_file is None:
try:
os.mkdir('draft')
except FileExistsError:
pass
filename = input_file.split('/')[-1]
output_file = f'draft/{filename}'
output = bytearray()
with open(input_file, 'r') as f:
for line in f:
output.extend((re.sub('\\\\ExecuteMetaData\[\.\./([^]]+)\]\{([^}]+)\}', '', line).strip() + '\n').encode())
external_documents = re.finditer('\\\\ExecuteMetaData\[\.\./([^]]+)\]\{([^}]+)\}', line)
for document in external_documents:
import_file = document.group(1)
tag = document.group(2)
with open(import_file, 'r') as in_file:
import_text_file = in_file.read()
import_text = re.search(f'%<\*{tag}>((.|\n)*?)%</{tag}>', import_text_file)
output.extend(import_text.group(1).strip().encode())
with open(output_file, 'wb') as f:
f.write(output)
def remove_duplicate_citations():
for note in database.Note:
tracked = [c for c in note.citations]
tracked_keys = [c.citationkey for c in tracked]
tracked_set = set(tracked_keys)
if len(tracked_set) < len(tracked_keys):
print(f'deleting duplicate keys in {note.filename}')
for key in tracked_set:
citation_instances = [c for c in database.Citation.select().where(database.Citation.note == note, database.Citation.citationkey==key)]
for i, c in enumerate(citation_instances):
if i == 0:
continue
c.delete_instance()
def __update_citations(note):
keys = Helper.__getcitations(note)
tracked = [c for c in note.citations]
tracked_keys = [c.citationkey for c in tracked]
updates_to_citations = False
for key in keys:
if key in tracked_keys:
continue
database.Citation.create(note=note, citationkey=key)
updates_to_citations = True
for citation in tracked:
if citation.citationkey not in keys:
citation.delete_instance()
updates_to_citations = True
return updates_to_citations
def __update_labels(note):
labels = Helper.__getlabels(note)
tracked_labels = [label.label for label in note.labels]
for label in labels:
if label not in tracked_labels:
database.Label.create(label=label, note=note)
#remove extra labels
for label in note.labels:
if label.label not in labels:
label.delete_instance()
#add connections
def __update_links(note):
links = Helper.__getlinks(note)
modified = []
tracked = [(link.target.note.reference, link.target.label) for link in note.references]
#add in untracked
for link in links:
if link not in tracked:
try:
label = database.Label.get(note__reference=link[0], label=link[1])
link = database.Link.create(target=label, source=note)
modified.append(link)
except database.Label.DoesNotExist:
print(f'label in {note.filename} with details {link[0]}, {link[1]} does not exist')
#remove any that no longer exist
for link in note.references:
if (link.target.note.reference, link.target.label) not in links:
print(f'link {(link.target.note.reference, link.target.label)} no longer exists, deleting')
link.delete_instance()
modified.append(link)
return modified
def __getnotefiles(directory='notes/slipbox'):
notes = [str(f) for f in files.get_files(directory, '.tex')]
return notes
def __createnotefile(filename, extension = 'tex'):
try:
os.mkdir('notes')
except FileExistsError:
pass
if extension == 'tex':
folder = os.path.join('notes', 'slipbox')
else: