1 Star 0 Fork 0

bit212/arxiv2kindle

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tex2md.py 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
xtoff 提交于 2016-10-26 03:44 +08:00 . init
import sys
import os.path
import glob
def getText(l):
l=l.replace('\\citep','')
l=l.replace('\\cite','')
return l
def checkFig(l):
i=l.find('\\includegraph')
if i>=0:
j=l.find('{',i)
if j>=0:
k=l.find('}',j)
if k>=0:
ff=l[j+1:k]
if ff.lower().endswith(".pdf"):
os.system('convert '+ff+" "+ff[0:-4]+".png")
ff=ff[0:-4]+".png"
if os.path.exists(ff): print("\n![img]("+ff+")\n")
else:
ffs=glob.glob(ff+'*')
if len(ffs)>1: print("WARNING ambiguous pic",ffs)
if len(ffs)>=1: print("\n![img]("+ffs[0]+")\n")
def texfile(tfich):
with open(tfich,'rb') as f : ls = f.readlines()
empty=False
eq=False
for l in ls:
l=l.strip()
if len(l)==0:
if not empty:
empty=True
print("")
continue
if l[0]=='\\':
if eq:
if l.startswith('\\end{eq'): eq=False
else: print(l)
if l.startswith('\\section'):
print('\n# '+getText(l[9:-1])+'\n')
elif l.startswith('\\subsection'):
print('\n## '+getText(l[12:-1])+'\n')
elif l.startswith('\\subsubsection'):
print('\n### '+getText(l[15:-1])+'\n')
elif l.startswith('\\begin{eq'): eq=True
elif l.startswith('\\input{'):
i=l.find('{')
if i>=0:
j=l.find('}',i)
if j>=0:
texfile(l[i+1:j]+'.tex')
continue
# supprime toutes les lignes qui commencent par \
checkFig(l)
if not empty:
empty=True
print("")
elif l[0]=='%':
# supprime toutes les lignes qui commencent par %
if not empty:
empty=True
print(getText(l))
else:
print(l)
empty=False
texfile(sys.argv[1])
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bit212/arxiv2kindle.git
git@gitee.com:bit212/arxiv2kindle.git
bit212
arxiv2kindle
arxiv2kindle
master

搜索帮助