-
Notifications
You must be signed in to change notification settings - Fork 1
/
md2tex.py
executable file
·229 lines (192 loc) · 7.38 KB
/
md2tex.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
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# This file is part of md2tex.
#
# Md2tex is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Md2tex is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with md2tex. If not, see <http://www.gnu.org/licenses/>.
import shutil
import sys
import getopt
import os
import re
import subprocess
def usage():
print("launch md2tex -h for the help");
try:
opts, args = getopt.getopt(sys.argv[1:], "po:ct:a:d:hv",["pdf","output=","complete","title=","author=","date=","help","version","header=","footer=","configure-header","configure-footer"])
except getopt.GetoptError as err:
# Affiche l'aide et quitte le programme
print(err) # Va afficher l'erreur en anglais
usage() # Fonction a ecrire rappelant la syntaxe de la commande. Function to display the command syntax.
sys.exit(2)
pdf = False;
output = False;
outputFilenameBase = "";
#pdfOutputFilename = "";
outputFilename = "";
#texOutputFilename = "";
complete = False;
pathDir = os.environ['HOME']+'/.md2tex/';
headerFilename = pathDir+"header.tex";
footerFilename = pathDir+"footer.tex";
author = "Unknown author"
title = "Unknown title"
date=r"\\today"
def writeTexFile(texFilename,source):
try:
headerFile = open(headerFilename,'r');
headerContent = headerFile.read();
headerContent = re.sub(r'@@@Title@@@',title,headerContent);
headerContent = re.sub(r'@@@Author@@@',author,headerContent);
headerContent = re.sub(r'@@@Date@@@',date,headerContent);
footerFile = open(footerFilename,'r');
footerContent = footerFile.read();
outputFile = open(texFilename,'w');
outputFile.write(headerContent);
outputFile.write(source);
outputFile.write(footerContent);
outputFile.close();
headerFile.close();
footerFile.close();
return True;
except IOError as err:
print(err);
return False;
if len(args) > 0:
inputFilename = args[0]
outputFilenameBase = re.sub(r'(.*).(txt|md|markdown)',r'\1',inputFilename);
for o, a in opts:
if o in ("-p", "--pdf"):
pdf = True;
complete = True;
elif o in ("-o","--output"):
output = True;
outputFilename = a;
elif o in ("-a","--author"):
author = a;
elif o in ("-t","--title"):
title = a;
elif o in ("-d","--date"):
date = a;
elif o in ("-c","--complete"):
complete = True;
elif o in ("-h","--help"):
subprocess.call(["less",pathDir+"README.md"])
sys.exit(1);
elif o in ("--configure-header"):
if len(args) > 0:
if not os.path.isfile(args[0]):
print("L'argument doit être un chemin vers un fichier valide");
sys.exit(2);
shutil.copyfile(args[0],pathDir+"header.tex");
sys.exit(0);
else :
sys.usage();
elif o in ("--configure-footer"):
if len(args) > 0:
if not os.path.isfile(args[0]):
print("L'argument doit être un chemin vers un fichier valide");
sys.exit(2);
shutil.copyfile(args[0],pathDir+"footer.tex");
sys.exit(0);
else :
sys.usage();
elif o in ("--header"):
if os.path.isfile(a):
headerFilename = a;
else:
print("The argument must be a path to a valid file. L'argument doit être un chemin vers un fichier valide");
sys.exit(2);
elif o in ("--footer"):
if os.path.isfile(a):
footerFilename = a;
else:
print("The argument must be a path to a valid file. L'argument doit être un chemin vers un fichier valide.");
sys.exit(2);
elif o in ("-v","--version"):
print("md2tex version 1.0 under licence GPL v3");
sys.exit(0);
else :
usage();
sys.exit(2);
#if output:
#if pdf:
#pdfOutputFilename = outputFilename;
#else
#texOutputFilename = outputFilename;
if len(args) == 0:
usage();
sys.exit(2);
try:
fileSource=open(inputFilename,'r')
# Read the entire source file into a string called source.
source = fileSource.read()
fileSource.close();
source = re.sub(r'!\((.*) "(.*)"\)',r'\\begin{figure}\n\t\\includegraphics{\1}\n\t\\caption{\2}\n\\end{figure}',source);
# itemize
source = re.sub(r'\n\n(?=\* .*)',r'\n\n\\begin{itemize}\n',source);
source = re.sub(r'\n\* (.*)\n\n',r'\item \1\n\\end{itemize}\n\n',source);
source = re.sub(r'\n\* (.*)',r'\n\item \1',source);
source = re.sub(r'\n\t\* (.*)',r'\n\t\subitem \1',source);
# enumerate
source = re.sub(r'\n\n(?=[0-9]+\. .*)',r'\n\n\\begin{enumerate}\n',source);
source = re.sub(r'[0-9]+\. (.*)\n\n',r'\item \1\n\\end{enumerate}\n\n',source);
source = re.sub(r'\n[0-9]+\. (.*)',r'\n\item \1',source);
source = re.sub(r'\n\t[0-9]+\. (.*)',r'\n\t\subitem \1',source);
# retour à la ligne / Return a newline
source = re.sub(r' \n',r'\n\\newline\n',source,re.MULTILINE);
# gras **
source = re.sub(r'(?<!\\)\*\*(([^*{}]|\\.)+?)(?<!\\)\*\*',r'\\textbf{\1}',source)
# gras __
source = re.sub(r'(?<!\\)__(([^_{}]|\\_)+?)(?<!\\)(?<!\\)__',r'\\textbf{\1}',source)
# italique *
source = re.sub(r'(?<!\\)\*(([^*{}]|\\.)+?)(?<!\\)\*',r'\\textit{\1}',source)
# italique _
source = re.sub(r'(?<!\\)_(([^_{}]|\\.)+?)(?<!\\)(?<!\\)_',r'\\textit{\1}',source)
# subsubsection
source = re.sub(r'### (.*?)\n',r'\\subsubsection{\1}\n',source)
# subsection
source = re.sub(r'## (.*?)\n',r'\\subsection{\1}\n',source)
source = re.sub(r'(.*?)\n-+\n',r'\\subsection{\1}\n',source)
# section
source = re.sub(r'# (.*?)\n',r'\\section{\1}\n',source)
source = re.sub(r'(.*?)\n=+\n',r'\\section{\1}\n',source)
if pdf:
if output :
outputFilenameBase = re.sub(r'(.*).pdf',r'\1', outputFilename);
texFilename = outputFilenameBase+".tex";
if writeTexFile(texFilename,source):
subprocess.call(["pdflatex",texFilename])
else :
if output:
writeTexFile(outputFilename,source);
else:
try:
if complete :
headerFile = open(headerFilename,'r');
headerContent = headerFile.read();
headerContent = re.sub(r'@@@Title@@@',title,headerContent);
headerContent = re.sub(r'@@@Author@@@',author,headerContent);
headerContent = re.sub(r'@@@Date@@@',date,headerContent);
footerFile = open(footerFilename,'r');
footerContent = footerFile.read();
headerFile.close();
footerFile.close();
print(headerContent);
print(source);
print(footerContent);
else :
print(source);
except IOError as err:
print(err);
except IOError as err:
print(err);