Skip to content

Commit

Permalink
添加以markdown格式输出的选项
Browse files Browse the repository at this point in the history
  • Loading branch information
dyxcloud committed Aug 30, 2019
1 parent b6f92f0 commit 0ad457e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@


## TODO
0. 添加"带有md格式的复制"选项: `[![imgName](base64)](imgurl)`
1. 本地中文文件名压缩转换
2. 转换参数可配置
18 changes: 14 additions & 4 deletions guistarter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def createWidgets(self):
self.Combo1List = ['不压缩','压缩至webp','压缩至png']
self.Combo1 = ttk.Combobox(self.top,state="readonly", values=self.Combo1List, font=('微软雅黑',9))
self.Combo1.current(0)
self.Combo1.place(relx=0.040, rely=0.300, relwidth=0.400, relheight=0.150)
self.Combo1.place(relx=0.040, rely=0.300, relwidth=0.350, relheight=0.150)

# 选择框
self.auto_compress = StringVar(value='1')
self.style.configure('Check1.TCheckbutton',font=('微软雅黑',9))
self.Check1 = ttk.Checkbutton(self.top, text='小于60k不压缩', variable=self.auto_compress, style='Check1.TCheckbutton')
self.Check1.place(relx=0.480, rely=0.300, relwidth=0.400, relheight=0.150)
self.Check1.place(relx=0.420, rely=0.300, relwidth=0.300, relheight=0.150)

self.with_md = StringVar(value='1')
self.style.configure('Check1.TCheckbutton',font=('微软雅黑',9))
self.Check1 = ttk.Checkbutton(self.top, text='md格式', variable=self.with_md, style='Check1.TCheckbutton')
self.Check1.place(relx=0.760, rely=0.300, relwidth=0.200, relheight=0.150)

self.copytext = StringVar(value='点击复制')
self.style.configure('Command2.TButton',font=('微软雅黑',9))
Expand All @@ -69,8 +74,9 @@ def doupload(self, event=None):
checkdata = self.Combo1.get()
checkindex = self.Combo1List.index(checkdata)
ifauto = int(self.auto_compress.get())
with_md = int(self.with_md.get())

self.result,showlen = mytool.work_file(local_file_path,checkindex,ifauto)
self.result,showlen = mytool.work_file(local_file_path,checkindex,ifauto,with_md)
if len(self.result)>50:
addToClipBoard(self.result)
self.copytext.set("点击复制, "+showlen)
Expand All @@ -86,8 +92,12 @@ def dotrans(self, event=None):
checkdata = self.Combo1.get()
checkindex = self.Combo1List.index(checkdata)
ifauto = int(self.auto_compress.get())
with_md = int(self.with_md.get())

if len(dataurl)==0:
return

self.result,showlen = mytool.work_url(dataurl,checkindex,ifauto)
self.result,showlen = mytool.work_url(dataurl,checkindex,ifauto,with_md)
if len(self.result)>50:
addToClipBoard(self.result)
self.copytext.set("点击复制, "+showlen)
Expand Down
20 changes: 16 additions & 4 deletions mytool.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ def _base64_getheader(filename):
ex = os.path.splitext(filename)[1]
return imagemapping.base64headers[ex]

def _with_md(result_line,imgname,url=None):
'''为base64结果添加md格式'''
if url is None:
pass
return "![{}]({})".format(imgname,result_line)
else:
return "[![{}]({})]({})".format(imgname,result_line,url)


def work_url(url,index,ifauto):
def work_url(url,index=0,ifauto=1,with_md=0):
'''0不压缩,1webp,2png'''
response,imgname = downloader.get_response_imgname(url)
bytes = response.read()
Expand All @@ -45,7 +53,7 @@ def work_url(url,index,ifauto):
index = 0
if index==0:
result_line = dobase64_with_bytes(bytes,imgname)
showlen = "nochange {}k".format(s,s)
showlen = "nochange {}k".format(s)
else:
source_path = psworkspace+imgname
downloader.download_by_bytes(bytes,source_path)
Expand All @@ -60,17 +68,19 @@ def work_url(url,index,ifauto):
showlen = "img_size {}k to {}k".format(s,r)
os.remove(source_path)
os.remove(result_path)
if with_md:
result_line = _with_md(result_line,imgname,url)
return result_line,showlen

def work_file(source_path,index,ifauto):
def work_file(source_path,index=0,ifauto=1,with_md=0):
'''0不压缩,1webp,2png'''
imgname = os.path.basename(source_path)
s = "{:.2f}".format(os.path.getsize(source_path)/1024.0)
if ifauto and os.path.getsize(source_path) < 60*1024:
index = 0
if index==0:
result_line = dobase64(source_path)
showlen = "nochange {}k".format(s,s)
showlen = "nochange {}k".format(s)
else:
is_to_png = index==2
if is_to_png:
Expand All @@ -82,6 +92,8 @@ def work_file(source_path,index,ifauto):
r = "{:.2f}".format(os.path.getsize(result_path)/1024.0)
showlen = "img_size {}k to {}k".format(s,r)
os.remove(result_path)
if with_md:
result_line = _with_md(result_line,imgname)
return result_line,showlen


Expand Down

0 comments on commit 0ad457e

Please sign in to comment.