daniel-img-fix / app.py
DanielCL's picture
add hide button to the `fix_imgs` function into a new function `fix_imgs_with_hide`
5376226
raw history blame
No virus
1.15 kB
from fastcore.utils import *
import gradio as gr
def fix_imgs(src, dst):
found = {n:(s,f) for n,s,f in
re.findall(r'!\[(\S+)\|(\S+)\]\((\S+)\)', src)}
def repl_img(x):
res = found.get(x.group(1))
if res:
sz,nm = res
return f'![{x.group(1)}|{sz}]({nm})'
else: return f'MISSING IMAGE: {x.group(1)}'
return re.sub(r'!\[\[([^.]+)\.\w+(\|\d+)?\]\]', repl_img, dst)
def fix_imgs_with_hide(src, dst):
result_no_hide = fix_imgs(src, dst)
def add_hide_top_func(x):
add_hide_top = """\n\n[details='Images']"""
return f'{x.group(1)}{add_hide_top}{x.group(2)}'
result_top_hide = re.sub(r'(\w|\?|\.|\`)([\n]+!\[[^|]+\|\S+\]\(\S+\))', add_hide_top_func, result_no_hide)
def add_hide_bottom_func(x):
add_hide_bottom = """[/details]\n\n"""
return f'{x.group(1)}{add_hide_bottom}{x.group(2)}'
return re.sub(r'(!\[[^|]+\|\S+\]\(\S+\)[\n]+)(\w|#)', add_hide_bottom_func, result_top_hide)
gr.Interface(fn=fix_imgs_with_hide, inputs=[gr.Textbox(lines=15),gr.Textbox(lines=15)], outputs=gr.Textbox(lines=15)).launch()