File size: 1,149 Bytes
a013f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5376226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a013f20
5376226
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
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()