File size: 1,224 Bytes
a013f20
 
 
 
 
 
 
 
 
 
 
 
 
 
8789604
fd07604
 
c41e3c2
 
 
 
fd07604
 
 
 
 
bd669c9
fd07604
 
 
 
 
 
a013f20
fd07604
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
33
34
35
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()