-
Notifications
You must be signed in to change notification settings - Fork 15
/
orglatex.py
1132 lines (1023 loc) · 42.1 KB
/
orglatex.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
import sublime
import sublime_plugin
import datetime
import re
import regex
from pathlib import Path
import os
import fnmatch
import OrgExtended.orgparse.node as node
from OrgExtended.orgparse.sublimenode import *
import OrgExtended.orgutil.util as util
import OrgExtended.orgutil.navigation as nav
import OrgExtended.orgutil.template as templateEngine
import logging
import sys
import traceback
import OrgExtended.orgfolding as folding
import OrgExtended.orgdb as db
import OrgExtended.asettings as sets
import OrgExtended.orgcapture as capture
import OrgExtended.orgproperties as props
import OrgExtended.orgutil.temp as tf
import OrgExtended.pymitter as evt
import OrgExtended.orgnotifications as notice
import OrgExtended.orgextension as ext
import OrgExtended.orgsourceblock as src
import OrgExtended.orgexporter as exp
import OrgExtended.orgplist as plist
import yaml
import sys
import subprocess
import html
log = logging.getLogger(__name__)
langMap = {
"cpp": "C++",
"python": "Python",
"C": "C",
"perl": "Perl",
"bash": "bash",
"sh": "sh",
"lua": "[5.0]Lua",
"java": "Java",
"php": "PHP",
"xml": "XML",
"lisp": "Lisp",
"sql": "SQL",
"r": "R",
"html": "HTML",
"go": "Go",
"make": "make",
"pascal": "Pascal",
"ruby": "Ruby",
"xsl": "XSLT",
"scala": "Scala",
"erlang": "erlang",
"gnuplot": "Gnuplot",
}
# overriding it by users settings
langMap.update(sets.Get("latexListingPackageLang",langMap))
def haveLang(lang):
return lang in langMap
def mapLanguage(lang):
if(lang in langMap):
return langMap[lang]
return lang
RE_ATTR = regex.compile(r"^\s*[#][+]ATTR_HTML[:](?P<params>\s+[:](?P<name>[a-zA-Z0-9._-]+)\s+(?P<value>([^:]|((?<! )[:]))+))+$")
RE_ATTR_ORG = regex.compile(r"^\s*[#][+]ATTR_ORG[:] ")
RE_LINK = re.compile(r"\[\[(?P<link>[^\]]+)\](\[(?P<desc>[^\]]+)\])?\]")
RE_UL = re.compile(r"^(?P<indent>\s*)(-|[+])\s+(?P<data>.+)")
RE_FN_MATCH = re.compile(r"\s*[:]([a-zA-Z0-9-_]+)\s+([^: ]+)?\s*")
RE_STARTSRC = re.compile(r"^\s*#\+(BEGIN_SRC|begin_src)\s+(?P<lang>[a-zA-Z0-9]+)")
RE_STARTDYN = re.compile(r"^\s*#\+(BEGIN:|begin:)\s+(?P<lang>[a-zA-Z0-9]+)")
RE_ENDSRC = re.compile(r"^\s*#\+(END_SRC|end_src)")
RE_ENDDYN = re.compile(r"^\s*#\+(end:|END:)")
RE_RESULTS = re.compile(r"^\s*#\+RESULTS.*")
RE_TABLE_SEPARATOR = re.compile(r"^\s*[|][-]")
RE_CHECKBOX = re.compile(r"^\[ \] ")
RE_CHECKED_CHECKBOX = re.compile(r"^\[[xX]\] ")
RE_PARTIAL_CHECKBOX = re.compile(r"^\[[-]\] ")
RE_EMPTY_LINE = re.compile(r"^\s*$")
# <!-- multiple_stores height="50%" width="50%" -->
RE_COMMENT_TAG = re.compile(r"^\s*[<][!][-][-]\s+(?P<name>[a-zA-Z0-9_-]+)\s+(?P<props>.*)\s+[-][-][>]")
#\documentclass{article}
# PREAMBLE
#\begin{document}
#Hello, \LaTeX\ world.
#\end{document}
sectionTypes = [
r"\chapter{{{heading}}}",
r"\section{{{heading}}}",
r"\subsection{{{heading}}}",
r"\subsubsection{{{heading}}}",
r"\paragraph{{{heading}}}",
r"\subparagraph{{{heading}}}"
]
class LatexSourceBlockState(exp.SourceBlockState):
def __init__(self,doc):
super(LatexSourceBlockState,self).__init__(doc)
self.skipSrc = False
def HandleOptions(self):
attr = self.e.GetAttrib("attr_latex")
optionsOp = ""
floatOp = None
if(attr):
p = plist.PList.createPList(attr)
else:
p = plist.PList.createPList("")
ops = p.Get("options",None)
if(ops):
optionsOp = ops
caption = self.e.GetAttrib("caption")
cc = p.Get("caption",None)
if(cc):
caption = cc
floatOp = GetOption(p,"float",None)
if(caption and not floatOp):
floatOp = "t"
if(floatOp and floatOp != "nil"):
if(floatOp == "multicolumn"):
figure = "figure*"
elif(floatOp == "sideways"):
figure = "sidewaysfigure"
elif(floatOp == "wrap"):
figure = "wrapfigure"
figureext = "{l}"
if(optionsOp.strip() != ""):
optionsOp = "[" + optionsOp + "]"
# There is a discrepancy between orgmode docs and
# actual emacs export.. I need to figure this out so
# nuke it for now till I understand it
optionsOp = ""
return (optionsOp,floatOp,caption)
def HandleEntering(self, m, l, orgnode):
self.skipSrc = False
language = m.group('lang')
paramstr = l[len(m.group(0)):]
p = type('', (), {})()
src.BuildFullParamList(p,language,paramstr,orgnode)
exp = p.params.Get("exports",None)
# Have to pass on parameter to the results block
self.e.sparams = p
if(isinstance(exp,list) and len(exp) > 0):
exp = exp[0]
if(exp == 'results' or exp == 'none'):
self.skipSrc = True
return
# Some languages we skip source by default
skipLangs = sets.Get("latexDefaultSkipSrc",[])
if(exp == None and language == skipLangs):
self.skipSrc = True
return
attribs = ""
self.options, self.float, self.caption = self.HandleOptions()
self.e.doc.append(r" \begin{center}")
self.e.doc.append(r" \centering")
if(haveLang(language)):
self.e.doc.append(r" \begin{options}{{lstlisting}}[language={{{lang}}}]".format(options=self.options,lang=mapLanguage(language)))
else:
self.e.doc.append(r" \begin{options}{{lstlisting}}".format(options=self.options))
def HandleExiting(self, m, l , orgnode):
if(not self.skipSrc):
self.e.doc.append(r" \end{lstlisting}")
self.e.doc.append(r" \end{center}")
skipSrc = False
def HandleIn(self,l, orgnode):
if(not self.skipSrc):
self.e.doc.append(l)
# Skips over contents not intended for a latex buffer
class LatexExportBlockState(exp.ExportBlockState):
def __init__(self,doc):
super(LatexExportBlockState,self).__init__(doc)
self.skipExport = False
def HandleEntering(self, m, l, orgnode):
self.skipExport = False
language = m.group('lang').strip().lower()
if(language != "latex"):
self.skipExport = True
return
# We will probably need this in the future.
#paramstr = l[len(m.group(0)):]
#p = type('', (), {})()
#src.BuildFullParamList(p,language,paramstr,orgnode)
def HandleExiting(self, m, l , orgnode):
self.skipExport = False
def HandleIn(self,l, orgnode):
if(not self.skipExport):
self.e.doc.append(l)
class LatexDynamicBlockState(exp.DynamicBlockState):
def __init__(self,doc):
super(LatexDynamicBlockState,self).__init__(doc)
self.skip = False
def HandleEntering(self,m,l,orgnode):
self.skip = False
language = m.group('lang')
paramstr = l[len(m.group(0)):]
p = type('', (), {})()
src.BuildFullParamList(p,language,paramstr,orgnode)
exp = p.params.Get("exports",None)
if(isinstance(exp,list) and len(exp) > 0):
exp = exp[0]
if(exp == 'results' or exp == 'none'):
self.skip = True
return
self.e.doc.append(r" \begin{verbatim}")
def HandleExiting(self, m, l , orgnode):
if(not self.skip):
self.e.doc.append(r" \end{verbatim}")
self.skip = False
def HandleIn(self,l, orgnode):
if(not self.skip):
self.e.doc.append(l)
class LatexQuoteBlockState(exp.QuoteBlockState):
def __init__(self,doc):
super(LatexQuoteBlockState,self).__init__(doc)
def HandleEntering(self,m,l,orgnode):
self.e.doc.append(r" \begin{displayquote}")
def HandleExiting(self, m, l , orgnode):
self.e.doc.append(r" \end{displayquote}")
def HandleIn(self,l, orgnode):
self.e.doc.append(l)
class LatexExampleBlockState(exp.ExampleBlockState):
def __init__(self,doc):
super(LatexExampleBlockState,self).__init__(doc)
def HandleEntering(self,m,l,orgnode):
self.e.doc.append(r" \begin{verbatim}")
def HandleExiting(self, m, l , orgnode):
self.e.doc.append(r" \end{verbatim}")
def HandleIn(self,l, orgnode):
self.e.doc.append(l)
class LatexGenericBlockState(exp.GenericBlockState):
def __init__(self,doc):
super(LatexGenericBlockState,self).__init__(doc)
def HandleEntering(self,m,l,orgnode):
self.data = m.group('data').strip().lower()
self.e.doc.append(r" \begin{{{data}}}".format(data=self.data))
def HandleExiting(self, m, l , orgnode):
self.e.doc.append(r" \end{{{data}}}".format(data=self.data))
def HandleIn(self,l, orgnode):
self.e.doc.append(l)
class LatexUnorderedListBlockState(exp.UnorderedListBlockState):
def __init__(self,doc):
super(LatexUnorderedListBlockState,self).__init__(doc)
def HandleEntering(self,m,l,orgnode):
self.e.doc.append(r" \begin{itemize}")
def HandleExiting(self, m, l , orgnode):
self.e.doc.append(r" \end{itemize}")
def StartHandleItem(self,m,l, orgnode):
#data = self.e.Escape(m.group('data'))
#self.e.doc.append(r" \item {content}".format(content=data))
definit = m.group('definition')
if(definit):
self.e.doc.append(r" \item``{definition}'' ".format(definition=definit))
else:
self.e.doc.append(r" \item ")
class LatexOrderedListBlockState(exp.OrderedListBlockState):
def __init__(self,doc):
super(LatexOrderedListBlockState,self).__init__(doc)
def HandleEntering(self,m,l,orgnode):
self.e.doc.append(r" \begin{enumerate}")
def HandleExiting(self, m, l , orgnode):
self.e.doc.append(r" \end{enumerate}")
def StartHandleItem(self,m,l, orgnode):
#data = self.e.Escape(m.group('data'))
#self.e.doc.append(r" \item {content}".format(content=data))
definit = m.group('definition')
if(definit):
self.e.doc.append(r" \item``{definition}'' ".format(definition=definit))
else:
self.e.doc.append(r" \item ")
class LatexCheckboxListBlockState(exp.CheckboxListBlockState):
def __init__(self,doc):
super(LatexCheckboxListBlockState,self).__init__(doc)
def HandleEntering(self,m,l,orgnode):
self.e.doc.append(r" \begin{todolist}")
def HandleExiting(self, m, l , orgnode):
self.e.doc.append(r" \end{todolist}")
def StartHandleItem(self,m,l, orgnode):
#data = self.e.Escape(m.group('data'))
state = m.group('state')
if(state == 'x'):
self.e.doc.append(r" \item[\wontfix] ")
elif(state == '-'):
self.e.doc.append(r" \item ")
#self.e.doc.append(r" \item[\inp] {content}".format(content=data))
else:
self.e.doc.append(r" \item ")
#if(state == 'x'):
# self.e.doc.append(r" \item[\wontfix] {content}".format(content=data))
#elif(state == '-'):
# self.e.doc.append(r" \item {content}".format(content=data))
# #self.e.doc.append(r" \item[\inp] {content}".format(content=data))
#else:
# self.e.doc.append(r" \item {content}".format(content=data))
class LatexTableBlockState(exp.TableBlockState):
def __init__(self,doc):
super(LatexTableBlockState,self).__init__(doc)
self.tablecnt = 1
def HandleEntering(self,m,l,orgnode):
attr = self.e.GetAttrib("attr_latex")
floatOp = None
align = "center"
self.modeDelimeterStart = ""
self.modeDelimeterEnd = ""
self.environment = "tabular"
self.figure = "center"
figureext = ""
if(attr):
p = plist.PList.createPList(attr)
else:
p = plist.PList.createPList("")
caption = self.e.GetAttrib("caption")
cc = p.Get("caption",None)
if(cc):
caption = cc
self.environment = GetOption(p,"environment",self.environment)
mode = GetOption(p,"mode",None)
if(mode == "math"):
self.modeDelimeterStart = r"\["
self.modeDelimeterEnd = r"\]"
if(mode == "inline-math"):
self.modeDelimeterStart = r"\("
self.modeDelimeterEnd = r"\)"
val = p.Get("center",None)
if(val and val == "nil"):
align = None
floatOp = GetOption(p,"float",None)
if(caption and not floatOp):
floatOp = "t"
if(floatOp and floatOp != "nil"):
if(floatOp):
self.figure = "table"
figureext = "[!htp]"
if(floatOp == "multicolumn"):
self.figure = "table*"
elif(floatOp == "sideways"):
self.figure = "sidewaysfigure"
elif(floatOp == "wrap"):
self.figure = "wrapfigure"
figureext = "{l}"
placement = p.Get("placement",None)
if(placement):
figureext = placement
tabledef = ""
tds = None
if(not RE_TABLE_SEPARATOR.search(l)):
tds = l.split('|')
if(len(tds) > 1):
if(mode == "math"):
tabledef = ""
else:
tabledef = "{" + ("|c" * (len(tds)-2)) + "|}"
self.e.doc.append(r" \begin{{{figure}}}{figureext}".format(figure=self.figure,figureext=figureext))
if(caption):
self.e.doc.append(r" \caption{{{caption}}}".format(caption=self.e.GetAttrib('caption')))
#self.fs.write(" <caption class=\"t-above\"><span class=\"table-number\">Table {index}:</span>{caption}</caption>".format(index=self.tableIndex,caption=self.caption))
#self.tableIndex += 1
if(align == "center" and self.environment == 'tabular'):
self.e.doc.append(r" \centering\renewcommand{\arraystretch}{1.2}")
self.e.doc.append(self.modeDelimeterStart)
self.e.doc.append(r" \begin{{{environment}}}{tabledef}".format(tabledef=tabledef,environment=self.environment))
self.e.ClearAttrib()
if(self.environment == 'tabular'):
self.e.doc.append(r" \hline")
if(tds):
self.HandleData(tds,True)
def HandleExiting(self, m, l , orgnode):
if(self.environment == 'tabular'):
self.e.doc.append(r" \hline")
self.e.doc.append(r" \end{{{environment}}}".format(environment=self.environment))
self.e.doc.append(self.modeDelimeterEnd)
self.e.doc.append(r" \label{{table:{cnt}}}".format(cnt=self.tablecnt))
self.e.doc.append(r" \end{{{figure}}}".format(figure=self.figure))
self.tablecnt += 1
def HandleData(self,tds,head=False):
if(len(tds) > 3):
# An actual table row, build a row
first = True
line = " "
for td in tds[1:-1]:
if(not first):
line += " & "
first = False
if(head and self.environment == 'tabular'):
line += r"\textbf{{{data}}}".format(data=self.e.Escape(td))
else:
line += self.e.Escape(td)
line += " \\\\"
self.e.doc.append(line)
haveTableHeader = True
def HandleIn(self,l, orgnode):
if(RE_TABLE_SEPARATOR.search(l)):
self.e.doc.append(r' \hline')
else:
tds = l.split('|')
self.HandleData(tds)
class LatexHrParser(exp.HrParser):
def __init__(self,doc):
super(LatexHrParser,self).__init__(doc)
def HandleLine(self,m,l,n):
self.e.doc.append(r"\newline\noindent\rule{\textwidth}{0.5pt}")
class LatexNameParser(exp.NameParser):
def __init__(self,doc):
super(LatexNameParser,self).__init__(doc)
def HandleLine(self,m,l,n):
self.e.doc.append(r"\label{{{data}}}".format(data=m.group('data')))
class LatexMathParser(exp.MathParser):
def __init__(self,doc):
super(LatexMathParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(r"\({data}\)".format(data=m.group('data')))
class LatexInlineMathParser(exp.InlineMathParser):
def __init__(self,doc):
super(LatexInlineMathParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(r"\({data}\)".format(data=m.group('data')))
class LatexEqMathParser(exp.EqMathParser):
def __init__(self,doc):
super(LatexEqMathParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(r"\[{data}\]".format(data=m.group('data')))
class LatexEmptyParser(exp.EmptyParser):
def __init__(self,doc):
super(LatexEmptyParser,self).__init__(doc)
def HandleLine(self,m,l,n):
self.e.doc.append(r"\leavevmode\newline")
class LatexActiveDateParser(exp.EmptyParser):
def __init__(self,doc):
super(LatexActiveDateParser,self).__init__(doc)
def HandleLine(self,m,l,n):
self.e.doc.append(r"\textit{{{date}}}".format(date=m.group()))
class LatexBoldParser(exp.BoldParser):
def __init__(self,doc):
super(LatexBoldParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(self.sre.sub(r"\\textbf{\g<data>}",m.group()))
class LatexItalicsParser(exp.ItalicsParser):
def __init__(self,doc):
super(LatexItalicsParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(self.sre.sub(r"\\textit{\g<data>}",m.group()))
class LatexUnderlineParser(exp.UnderlineParser):
def __init__(self,doc):
super(LatexUnderlineParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(self.sre.sub(r"\\underline{\g<data>}",m.group()))
class LatexStrikethroughParser(exp.StrikethroughParser):
def __init__(self,doc):
super(LatexStrikethroughParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(self.sre.sub(r"\\sout{\g<data>}",m.group()))
class LatexCodeParser(exp.CodeParser):
def __init__(self,doc):
super(LatexCodeParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(self.sre.sub(r"\\texttt{\g<data>}",m.group()))
class LatexVerbatimParser(exp.VerbatimParser):
def __init__(self,doc):
super(LatexVerbatimParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(self.sre.sub(r"\\texttt{\g<data>}",m.group()))
RE_LATEXKEYWORD = regex.compile(r"^\s*\\newline\s*$")
class LatexKeywordParser(exp.SubLineParser):
def __init__(self,doc):
super(LatexKeywordParser,self).__init__(RE_LATEXKEYWORD,doc)
def HandleSegment(self,m,l,orgnode):
self.e.doc.append(m.group().strip())
def FindImageFile(view, url):
# ABS
if(os.path.isabs(url)):
return url
# Relative
if(view != None):
curDir = os.path.dirname(view.file_name())
filename = os.path.join(curDir, url)
if(os.path.isfile(filename)):
return filename
# In search path
searchHere = sets.Get("imageSearchPath",[])
for direc in searchHere:
filename = os.path.join(direc, url)
if(os.path.isfile(filename)):
return filename
searchHere = sets.Get("orgDirs",[])
for direc in searchHere:
filename = os.path.join(direc, "images", url)
if(os.path.isfile(filename)):
return filename
def IsImageFile(fn):
# Todo make this configurable
if(fn.endswith(".gif") or fn.endswith(".png") or fn.endswith(".jpg") or fn.endswith(".svg")):
return True
return False
def AddOption(p,name,ops):
val = p.Get(name,None)
if(val):
if(ops != ""):
ops += ","
ops += name + "=" + val.strip()
return ops
def GetOption(p,name,ops):
val = p.Get(name,None)
if(val):
return val.strip()
return ops
# Simple links are easy. The hard part is images, includes and results
class LatexLinkParser(exp.LinkParser):
def __init__(self,doc):
super(LatexLinkParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
link = m.group('link').strip()
desc = m.group('desc')
if(desc):
desc = self.e.Escape(desc.strip())
if(link.startswith("file:")):
link = re.sub(r'^file:','',link)
view = sublime.active_window().active_view()
imgFile = FindImageFile(view,link)
if(imgFile and os.path.isfile(imgFile) and IsImageFile(imgFile)):
relPath = view.MakeRelativeToMe(imgFile)
imagePath = os.path.dirname(relPath)
imageToken = os.path.splitext(os.path.basename(relPath))[0]
# The figures let this float around to much. I can't control the positioning with
# that. Also the scale is crazy at 1.0. So I auto scale to .5? Probably not the best choice.
# Attributes will solve this at some point.
attr = self.e.GetAttrib("attr_latex")
optionsOp = ""
floatOp = None
figure = "figure"
align = "center"
figureext = ""
if(attr):
p = plist.PList.createPList(attr)
else:
p = plist.PList.createPList("")
ops = p.Get("options",None)
if(ops):
optionsOp = ops
caption = self.e.GetAttrib("caption")
cc = p.Get("caption",None)
if(cc):
caption = cc
optionsOp = AddOption(p,"width",optionsOp)
optionsOp = AddOption(p,"height",optionsOp)
optionsOp = AddOption(p,"scale",optionsOp)
val = p.Get("center",None)
if(val and val == "nil"):
align = None
if(optionsOp == ""):
optionsOp = r"width=.8\linewidth"
if(caption and not floatOp):
floatOp = "t"
if(floatOp and floatOp != "nil"):
if(floatOp == "multicolumn"):
figure = "figure*"
elif(floatOp == "sideways"):
figure = "sidewaysfigure"
elif(floatOp == "wrap"):
figure = "wrapfigure"
figureext = "{l}"
placement = p.Get("placement",None)
if(placement):
figureext = placement
self.e.doc.append(r"\begin{{{figure}}}{figureext}".format(figure=figure,figureext=figureext))
if(align == "center"):
self.e.doc.append(r"\centering")
self.e.doc.append(r"\includegraphics[{options}]{{{name}}}".format(name=imageToken,options=optionsOp))
if(caption):
self.e.doc.append(r"\caption{{{caption}}}".format(caption=caption))
if(floatOp and floatOp != "nil"):
self.e.doc.append(r"\end{{{figure}}}".format(figure=figure))
if(not imagePath in self.e.imagepaths):
self.e.imagepaths.append(imagePath)
else:
if(link.startswith("http")):
if(desc):
self.e.doc.append(r"\href{{{link}}}{{{desc}}}".format(link=link,desc=desc))
else:
self.e.doc.append(r"\url{{{link}}}".format(link=link))
elif("/" not in link and "\\" not in link and "." not in link):
if(desc):
self.e.doc.append(r"\hyperref[{link}]{{{desc}}}".format(link=link,desc=desc))
else:
self.e.doc.append(r"\hyperref[{link}]{{{desc}}}".format(link=link,desc=self.e.Escape(link)))
else:
link = re.sub(r"[:][:][^/].*","",link)
link = link.replace("\\","/")
text = m.group()
if(desc):
self.e.doc.append(r"\href{{{link}}}{{{desc}}}".format(link=link,desc=desc))
else:
self.e.doc.append(r"\url{{{link}}}".format(link=link))
self.e.ClearAttrib()
# <<TARGET>>
class LatexTargetParser(exp.TargetParser):
def __init__(self,doc):
super(LatexTargetParser,self).__init__(doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(r"\label{{{data}}}".format(data=m.group('data')))
class LatexLatexHeaderParser(exp.LatexHeaderParser):
def __init__(self,doc):
super(LatexLatexHeaderParser,self).__init__(doc)
def HandleLine(self,m,l,n):
self.e.pre.append(m.group('data').strip())
class LatexLatexClassOptionsParser(exp.LatexClassOptionsParser):
def __init__(self,doc):
super(LatexLatexClassOptionsParser,self).__init__(doc)
def HandleLine(self,m,l,n):
self.e.documentclass += m.group('data').strip()
# Outputs latex verbatim but in a line
RE_LATEX_SUBLATEX = regex.compile(r"[@][@](?P<data>.*)[@][@]")
class LatexLatexSubLatexParser(exp.SubLineParser):
def __init__(self,doc):
super(LatexLatexSubLatexParser,self).__init__(RE_LATEX_SUBLATEX, doc)
def HandleSegment(self,m,l,n):
self.e.doc.append(m.group('data').strip())
# Line of latex gets emitted
RE_LATEX_LATEX = regex.compile(r"^\s*[#][+]LATEX[:]\s*(?P<data>.*)")
class LatexLatexLatexParser(exp.LineParser):
def __init__(self,doc):
super(LatexLatexLatexParser,self).__init__(RE_LATEX_LATEX, doc)
def HandleLine(self,m,l,n):
self.e.doc.append(m.group('data').strip())
RE_ATTR_LATEX = regex.compile(r"^\s*[#][+]ATTR_LATEX[:]\s*(?P<data>.*)")
class LatexAttributeParser(exp.AttributeParser):
def __init__(self,doc):
super(LatexAttributeParser,self).__init__('attr_latex',RE_ATTR_LATEX,doc)
class LatexDoc(exp.OrgExporter):
def __init__(self,filename,file,**kwargs):
super(LatexDoc, self).__init__(filename, file, **kwargs)
self.file = file
self.sparams = None
self.documentclass = r'\documentclass{article}'
self.imagepaths = []
self.pre = []
self.doc = []
self.attribs = {}
self.amInBlock = False
# TODO: Make this configurable
self.pre.append(r"\usepackage[utf8]{inputenc}")
self.pre.append(r"\usepackage{listings}")
self.pre.append(r"\usepackage{hyperref}")
self.pre.append(r"\usepackage{csquotes}")
self.pre.append(r"\usepackage{makecell, caption}")
self.pre.append(r"\usepackage[T1]{fontenc}")
self.pre.append(r"\usepackage[greek,english]{babel}")
self.pre.append(r"\usepackage{CJKutf8}")
self.pre.append(r"\usepackage{graphicx}")
self.pre.append(r"\usepackage{grffile}")
self.pre.append(r"\usepackage{longtable}")
self.pre.append(r"\usepackage{wrapfig}")
self.pre.append(r"\usepackage{rotating}")
self.pre.append(r"\usepackage{textcomp}")
self.pre.append(r"\usepackage{capt-of}")
self.pre.append(r"\usepackage{amsmath}")
self.pre.append(r"\usepackage{amssymb}")
# Needed for strikethrough
self.pre.append(r"\usepackage[normalem]{ulem}")
# Checkbox Setup
self.pre.append(r"\usepackage{enumitem,amssymb}")
self.pre.append(r"\newlist{todolist}{itemize}{2}")
self.pre.append(r"\setlist[todolist]{label=$\square$}")
self.pre.append(r"\usepackage{pifont}")
self.pre.append(r"\newcommand{\cmark}{\ding{51}}%")
self.pre.append(r"\newcommand{\xmark}{\ding{55}}%")
self.pre.append(r"\newcommand{\tridot}{\ding{213}}%")
self.pre.append(r"\newcommand{\inp}{\rlap{$\square$}{\large\hspace{1pt}\tridot}}")
self.pre.append(r"\newcommand{\done}{\rlap{$\square$}{\raisebox{2pt}{\large\hspace{1pt}\cmark}}%")
self.pre.append(r"\hspace{-2.5pt}}")
self.pre.append(r"\newcommand{\wontfix}{\rlap{$\square$}{\large\hspace{1pt}\xmark}}")
#self.pre.append(r"\usepackage{flafter}")
self.nodeParsers = [
exp.SetupFileParser(self),
exp.CaptionAttributeParser(self),
LatexAttributeParser(self),
exp.ResultsParser(self),
LatexTableBlockState(self),
LatexSourceBlockState(self),
LatexDynamicBlockState(self),
LatexQuoteBlockState(self),
LatexExampleBlockState(self),
LatexCheckboxListBlockState(self),
LatexUnorderedListBlockState(self),
LatexOrderedListBlockState(self),
LatexExportBlockState(self),
LatexGenericBlockState(self),
exp.DrawerBlockState(self),
exp.SchedulingStripper(self),
exp.TblFmStripper(self),
exp.AttrHtmlStripper(self),
exp.AttrOrgStripper(self),
exp.KeywordStripper(self),
LatexEmptyParser(self),
LatexLinkParser(self),
LatexHrParser(self),
LatexNameParser(self),
LatexLatexHeaderParser(self),
LatexLatexClassOptionsParser(self),
LatexLatexLatexParser(self),
LatexActiveDateParser(self),
LatexMathParser(self),
LatexLatexSubLatexParser(self),
LatexInlineMathParser(self),
LatexEqMathParser(self),
LatexBoldParser(self),
LatexItalicsParser(self),
LatexUnderlineParser(self),
LatexStrikethroughParser(self),
LatexCodeParser(self),
LatexVerbatimParser(self),
LatexKeywordParser(self),
LatexTargetParser(self)
]
def SetAmInBlock(self,inBlock):
self.amInBlock = inBlock
def AmInBlock(self):
return self.amInBlock
def AddAttrib(self,name,val):
self.attribs[name] = val.strip()
def GetAttrib(self,name):
if(name in self.attribs):
return self.attribs[name]
return None
def ClearAttrib(self):
self.attribs.clear()
def setClass(self,className):
self.documentclass = r'\documentclass{{{docclass}}}'.format(docclass=className)
def BuildDoc(self):
imagepaths = ""
if(len(self.imagepaths) > 0):
imagepaths = r"\graphicspath{"
for i in self.imagepaths:
item = i.replace("\\","/").strip()
if(not item.endswith("/")):
item += "/"
if(not item.startswith(".")):
item = "./" + item
imagepaths += "{" + item + "}"
imagepaths += "}"
toc = [r"\maketitle",r"\tableofcontents"]
# Use our options to control the title and toc
ops = self.file.org.get_comment("OPTIONS",None)
if(ops):
ops = " ".join(ops)
ops = ops.strip().split(" ")
if("toc:nil" in ops):
toc.remove(r"\tableofcontents")
if("title:nil" in ops):
toc.remove(r"\maketitle")
if("author:nil" in ops):
for l in self.pre:
if(l.startswith("\\author")):
self.pre.remove(l)
break
if("date:nil" in ops):
for l in self.pre:
if(l.startswith("\\date")):
self.pre.remove(l)
break
self.pre.append("\\date{}")
for t in ops:
if(t.startswith("toc:")):
v = t.split(":")[1].strip()
try:
v = int(v)
if(v > 0):
toc.insert(0,"\\setcounter{{tocdepth}}{{{num}}}".format(num=v))
except:
pass
out = self.documentclass + '\n' + '\n'.join(self.pre) + '\n'+ imagepaths +"\n" + r'\begin{document}' + '\n' + "\n".join(toc) + '\n' + '\n'.join(self.doc) + '\n' + r'\end{document}' + '\n'
return out
# Document header metadata should go in here
def AddExportMetaCustom(self):
if(self.author):
self.pre.append(r"\author{{{data}}}".format(data=self.author))
if(self.title):
self.pre.append(r"\title{{{data}}}".format(data=self.title))
if(self.date):
self.pre.append(r"\date{{{data}}}".format(data=self.date))
pass
# Setup to start the export of a node
def StartNode(self, n):
pass
def Escape(self,str):
str,cnt = self.SingleLineReplacements(str)
if(0 == cnt):
return self.TexFullEscape(str)
elif(1 == cnt):
return self.TexCommandEscape(str)
else:
return str
def TexFullEscape(self,text):
conv = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'\\': r'\textbackslash{}',
'<': r'\textless{}',
'>': r'\textgreater{}',
}
cleanre = re.compile(r'([^\\])(\%|\&|\$|\#|\_|\{|\}|\~|\^|\\|\>|\<)')
#print("AAA: " + '|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key = lambda item: - len(item))))
#cleanre = re.compile('(.)(' + '|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key = lambda item: - len(item))) + ")")
result = cleanre.sub(lambda match: (match.group(1) if match.group(1) else "") + conv[match.group(2)] if (match.group(1) and match.group(1) != "\\") else match.group(), text)
return result
def TexCommandEscape(self,text):
conv = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'<': r'\textless{}',
'>': r'\textgreater{}',
}
cleanre = re.compile(r'([^\\])(\%|\&|\$|\#|\_|\~|\^|\>|\<)')
result = cleanre.sub(lambda match: (match.group(1) if match.group(1) else "") + conv[match.group(2)] if (match.group(1) and match.group(1) != "\\") else match.group(), text)
return result
#return cleanre.sub(lambda match: conv[match.group()], text)
def SingleLineReplace(self,reg,rep,text,ok):
nt = reg.sub(rep,text)
ok = ok or nt != text
return (nt,ok)
def SingleLineReplacements(self,text):
didRep = 0
text = exp.RE_TITLE.sub("",text)
text = exp.RE_AUTHOR.sub("",text)
text = exp.RE_LANGUAGE.sub("",text)
text = exp.RE_EMAIL.sub("",text)
text = exp.RE_DATE.sub("",text)
m = RE_LINK.search(text)
if(m):
link = m.group('link').strip()
desc = m.group('desc')
if(desc):
desc = self.TexFullEscape(desc.strip())
if(False and (link.endswith(".png") or link.endswith(".jpg") or link.endswith(".jpeg") or link.endswith(".gif"))):
if(link.startswith("file:")):
link = re.sub(r'^file:','',link)
extradata = ""
if(self.commentName and self.commentName in link):
extradata = " " + self.commentData
self.commentName = None
if(hasattr(self,'attrs')):
for key in self.attrs:
extradata += " " + str(key) + "=\"" + str(self.attrs[key]) + "\""
preamble = ""
postamble = ""
if(hasattr(self,'caption') and self.caption):
pass
#preamble = "<div class=\"figure\"><p>"
#postamble = "</p><p><span class=\"figure-number\">Figure {index}: </span>{caption}</p></div>".format(index=self.figureIndex,caption=self.caption)
self.figureIndex += 1
#text = RE_LINK.sub("{preamble}<img src=\"{link}\" alt=\"{desc}\"{extradata}>{postamble}".format(preamble=preamble,link=link,desc=desc,extradata=extradata,postamble=postamble),line)
didRep = True
#self.ClearAttributes()
return (text,2)
else:
if(link.startswith("file:")):
link = re.sub(r'^file:','',link)
link = re.sub(r"[:][:][^/].*","",link)
#link = self.TexFullEscape(link)
link = link.replace("\\","/")
if(desc):
traceback.print_stack()
text = RE_LINK.sub(r"\\ref{{{link}}}{{{desc}}}".format(link=link,desc=desc),text)
else:
text = RE_LINK.sub(r"\\ref{{{link}}}".format(link=link),text)
didRep = 2
#self.ClearAttributes()
return (text,2)
text,didRep = self.SingleLineReplace(exp.RE_NAME,r"\label{\g<data>}",text,didRep)
text,didRep = self.SingleLineReplace(exp.RE_BOLD,r"\\textbf{\g<data>}",text,didRep)
text,didRep = self.SingleLineReplace(exp.RE_ITALICS,r"\\textit{\g<data>}",text,didRep)
text,didRep = self.SingleLineReplace(exp.RE_UNDERLINE,r"\underline{\g<data>}",text,didRep)
text,didRep = self.SingleLineReplace(exp.RE_CODE,r"\\texttt{\g<data>}",text,didRep)
text,didRep = self.SingleLineReplace(exp.RE_VERBATIM,r"\\texttt{\g<data>}",text,didRep)
text,didRep = self.SingleLineReplace(exp.RE_HR,r"\hrulefill",text,didRep)
return (text,1 if didRep else 0)
# Export the heading of this node
def NodeHeading(self,n):
heading = self.Escape(n.heading)
level = n.level
if(level >= len(sectionTypes)):
level = len(sectionTypes)-1
self.doc.append(sectionTypes[level].format(heading=heading))
# We are about to start exporting the nodes body
def StartNodeBody(self,n):
pass
def AttributesGather(self, l):
return False
def NodeBody(self,n):
ilines = n._lines[1:]
for parser in self.nodeParsers:
ilines = parser.Handle(ilines,n)
for line in ilines:
self.doc.append(self.TexFullEscape(line))
# We are done exporting the nodes body so finish it off
def EndNodeBody(self,n):
pass
# We are now done the node itself so finish that off
def EndNode(self,n):
pass
# def about to start exporting nodes
def StartNodes(self):