File size: 1,613 Bytes
529e208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
854aad5
529e208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import re

import streamlit as st

from ui.Component import side_bar_links

st.set_page_config(
    page_title='工具箱',
    page_icon='🔨',
    layout='wide',
)

with st.sidebar:
    side_bar_links()

st.title("格式化工具")


def re_format(origin_str: str) -> str:
    new_str = origin_str.replace('\r', '').replace('\n', '').replace('', '①').replace('', '②').replace('', '③')

    matches = re.findall(r'\[\s*\d+(?:,\s*\d+)*]', new_str)

    for match in matches:
        match_str: str = match
        new_ref = ''.join([
            f"[^{ind.replace(' ', '')}]"
            for ind in match_str.replace('[', '').replace(']', '').split(',')
        ])
        new_str = new_str.replace(match, new_ref)

    matches = re.findall(r'\[\s*\d+(?:-\s*\d+)*]', new_str)

    for match in matches:
        match_str: str = match
        match_str = match_str.replace('[', '').replace(']', '')
        a = int(match_str.split('-')[0].strip())
        b = int(match_str.split('-')[-1].strip())

        new_ref = ''.join([
            f'[^{i}]'
            for i in range(a, b + 1)
        ])
        new_str = new_str.replace(match, new_ref)

    return new_str


col1, col2 = st.columns([1, 1], gap="medium")

if 'markdown_text' not in st.session_state:
    st.session_state.markdown_text = ''

with col1.container(height=520, border=True):
    st.markdown(st.session_state.markdown_text)

with col2:
    st.code(st.session_state.markdown_text, language='markdown')

if prompt := st.chat_input():
    response = re_format(prompt)

    st.session_state.markdown_text = response

    st.rerun()