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)}' result = re.sub(r'!\[\[(\S+)\.(png|jpeg)(\|\d+)?\]\]', repl_img, dst) def fix_imgs_with_hide(src, dst): if dst != "": result_no_hide = fix_imgs(src, dst) else: result_no_hide = src 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|\`][\s]*)([\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()