fix code highlight problem
This commit is contained in:
parent
c0a697f6c8
commit
a2099f1622
@ -10,6 +10,26 @@ from shared_utils.config_loader import get_conf as get_conf
|
||||
pj = os.path.join
|
||||
default_user_name = 'default_user'
|
||||
|
||||
markdown_extension_configs = {
|
||||
'mdx_math': {
|
||||
'enable_dollar_delimiter': True,
|
||||
'use_gitlab_delimiters': False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
code_highlight_configs = {
|
||||
"pymdownx.superfences": {
|
||||
'css_class': 'codehilite',
|
||||
},
|
||||
"pymdownx.highlight": {
|
||||
'css_class': 'codehilite',
|
||||
'guess_lang': True,
|
||||
# 'auto_title': True,
|
||||
# 'linenums': True
|
||||
}
|
||||
}
|
||||
|
||||
def text_divide_paragraph(text):
|
||||
"""
|
||||
将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。
|
||||
@ -33,6 +53,7 @@ def text_divide_paragraph(text):
|
||||
text = "</br>".join(lines)
|
||||
return pre + text + suf
|
||||
|
||||
|
||||
def tex2mathml_catch_exception(content, *args, **kwargs):
|
||||
try:
|
||||
content = tex2mathml(content, *args, **kwargs)
|
||||
@ -40,6 +61,7 @@ def tex2mathml_catch_exception(content, *args, **kwargs):
|
||||
content = content
|
||||
return content
|
||||
|
||||
|
||||
def replace_math_no_render(match):
|
||||
content = match.group(1)
|
||||
if 'mode=display' in match.group(0):
|
||||
@ -48,6 +70,7 @@ def replace_math_no_render(match):
|
||||
else:
|
||||
return f"<font color=\"#00FF00\">$</font><font color=\"#FF00FF\">{content}</font><font color=\"#00FF00\">$</font>"
|
||||
|
||||
|
||||
def replace_math_render(match):
|
||||
content = match.group(1)
|
||||
if 'mode=display' in match.group(0):
|
||||
@ -60,6 +83,7 @@ def replace_math_render(match):
|
||||
else:
|
||||
return tex2mathml_catch_exception(content)
|
||||
|
||||
|
||||
def markdown_bug_hunt(content):
|
||||
"""
|
||||
解决一个mdx_math的bug(单$包裹begin命令时多余<script>)
|
||||
@ -69,6 +93,7 @@ def markdown_bug_hunt(content):
|
||||
content = content.replace('</script>\n</script>', '</script>')
|
||||
return content
|
||||
|
||||
|
||||
def is_equation(txt):
|
||||
"""
|
||||
判定是否为公式 | 测试1 写出洛伦兹定律,使用tex格式公式 测试2 给出柯西不等式,使用latex格式 测试3 写出麦克斯韦方程组
|
||||
@ -99,6 +124,7 @@ def is_equation(txt):
|
||||
contain_any_eq = True
|
||||
return contain_any_eq
|
||||
|
||||
|
||||
def fix_markdown_indent(txt):
|
||||
# fix markdown indent
|
||||
if (' - ' not in txt) or ('. ' not in txt):
|
||||
@ -119,6 +145,7 @@ def fix_markdown_indent(txt):
|
||||
lines[i] = ' ' * num_spaces_should_be + stripped_string
|
||||
return '\n'.join(lines)
|
||||
|
||||
|
||||
FENCED_BLOCK_RE = re.compile(
|
||||
dedent(r'''
|
||||
(?P<fence>^[ \t]*(?:~{3,}|`{3,}))[ ]* # opening fence
|
||||
@ -132,6 +159,7 @@ FENCED_BLOCK_RE = re.compile(
|
||||
re.MULTILINE | re.DOTALL | re.VERBOSE
|
||||
)
|
||||
|
||||
|
||||
def get_line_range(re_match_obj, txt):
|
||||
start_pos, end_pos = re_match_obj.regs[0]
|
||||
num_newlines_before = txt[:start_pos+1].count('\n')
|
||||
@ -139,6 +167,7 @@ def get_line_range(re_match_obj, txt):
|
||||
line_end = num_newlines_before + txt[start_pos:end_pos].count('\n')+1
|
||||
return line_start, line_end
|
||||
|
||||
|
||||
def fix_code_segment_indent(txt):
|
||||
lines = []
|
||||
change_any = False
|
||||
@ -175,6 +204,7 @@ def fix_code_segment_indent(txt):
|
||||
else:
|
||||
return txt
|
||||
|
||||
|
||||
@lru_cache(maxsize=128) # 使用 lru缓存 加快转换速度
|
||||
def markdown_convertion(txt):
|
||||
"""
|
||||
@ -186,12 +216,6 @@ def markdown_convertion(txt):
|
||||
# print('警告,输入了已经经过转化的字符串,二次转化可能出问题')
|
||||
return txt # 已经被转化过,不需要再次转化
|
||||
|
||||
markdown_extension_configs = {
|
||||
'mdx_math': {
|
||||
'enable_dollar_delimiter': True,
|
||||
'use_gitlab_delimiters': False,
|
||||
},
|
||||
}
|
||||
find_equation_pattern = r'<script type="math/tex(?:.*?)>(.*?)</script>'
|
||||
|
||||
txt = fix_markdown_indent(txt)
|
||||
@ -199,8 +223,8 @@ def markdown_convertion(txt):
|
||||
if is_equation(txt): # 有$标识的公式符号,且没有代码段```的标识
|
||||
# convert everything to html format
|
||||
split = markdown.markdown(text='---')
|
||||
convert_stage_1 = markdown.markdown(text=txt, extensions=['sane_lists', 'tables', 'mdx_math', 'pymdownx.superfences'],
|
||||
extension_configs=markdown_extension_configs)
|
||||
convert_stage_1 = markdown.markdown(text=txt, extensions=['sane_lists', 'tables', 'mdx_math', 'pymdownx.superfences', 'pymdownx.highlight'],
|
||||
extension_configs={**markdown_extension_configs, **code_highlight_configs})
|
||||
convert_stage_1 = markdown_bug_hunt(convert_stage_1)
|
||||
# 1. convert to easy-to-copy tex (do not render math)
|
||||
convert_stage_2_1, n = re.subn(find_equation_pattern, replace_math_no_render, convert_stage_1, flags=re.DOTALL)
|
||||
@ -209,7 +233,7 @@ def markdown_convertion(txt):
|
||||
# cat them together
|
||||
return pre + convert_stage_2_1 + f'{split}' + convert_stage_2_2 + suf
|
||||
else:
|
||||
return pre + markdown.markdown(txt, extensions=['sane_lists', 'tables', 'pymdownx.superfences', 'codehilite']) + suf
|
||||
return pre + markdown.markdown(txt, extensions=['sane_lists', 'tables', 'pymdownx.superfences', 'pymdownx.highlight'], extension_configs=code_highlight_configs) + suf
|
||||
|
||||
|
||||
def close_up_code_segment_during_stream(gpt_reply):
|
||||
@ -250,7 +274,7 @@ def format_io(self, y):
|
||||
if gpt_reply is not None: gpt_reply = close_up_code_segment_during_stream(gpt_reply)
|
||||
# process
|
||||
y[-1] = (
|
||||
None if i_ask is None else markdown.markdown(i_ask, extensions=['pymdownx.superfences', 'tables']),
|
||||
None if i_ask is None else markdown.markdown(i_ask, extensions=['pymdownx.superfences', 'tables', 'pymdownx.highlight'], extension_configs=code_highlight_configs),
|
||||
None if gpt_reply is None else markdown_convertion(gpt_reply)
|
||||
)
|
||||
return y
|
||||
|
@ -1,118 +1,19 @@
|
||||
md = """
|
||||
|
||||
要计算文件的哈希值,可以使用哈希算法(如MD5、SHA-1或SHA-256)对文件的内容进行计算。
|
||||
|
||||
以下是一个使用sha256算法计算文件哈希值的示例代码:
|
||||
You can use the following Python script to rename files matching the pattern '* - 副本.tex' to '* - wushiguang.tex' in a directory:
|
||||
|
||||
```python
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
def calculate_hash(file_path):
|
||||
sha256_hash = hashlib.sha256()
|
||||
with open(file_path, 'rb') as file:
|
||||
for chunk in iter(lambda: file.read(4096), b''):
|
||||
sha256_hash.update(chunk)
|
||||
return sha256_hash.hexdigest()
|
||||
# Directory containing the files
|
||||
directory = 'Tex/'
|
||||
|
||||
# 使用示例
|
||||
file_path = 'path/to/file.txt'
|
||||
hash_value = calculate_hash(file_path)
|
||||
print('File hash:', hash_value)
|
||||
for filename in os.listdir(directory):
|
||||
if filename.endswith(' - 副本.tex'):
|
||||
new_filename = filename.replace(' - 副本.tex', ' - wushiguang.tex')
|
||||
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
|
||||
```
|
||||
|
||||
在上面的示例中,`calculate_hash`函数接受一个文件路径作为参数,并打开文件以二进制读取模式读取文件内容。然后,使用哈希对象sha256初始化,并对文件内容进行分块读取并更新哈希值。最后,通过`hexdigest`方法获取哈希值的十六进制表示。
|
||||
|
||||
可以根据需要更改哈希算法(如使用`hashlib.md5()`来使用MD5算法)和块大小(这里使用4096字节)。
|
||||
|
||||
"""
|
||||
|
||||
md = """
|
||||
要在Ubuntu中将NTFS格式转换为ext4格式,您需要进行以下步骤:
|
||||
|
||||
1. 首先,确保您已经安装了gparted软件。如果没有安装,请使用以下命令进行安装:
|
||||
|
||||
```
|
||||
sudo apt update
|
||||
sudo apt install gparted
|
||||
```
|
||||
|
||||
2. 然后,打开GParted软件。您可以在"应用程序"菜单中搜索并启动它。
|
||||
|
||||
3. 在GParted界面中,选择您想要转换格式的NTFS分区。请小心选择,确保选择正确的分区。
|
||||
|
||||
4. 确保分区未挂载。如果分区当前正在使用,您需要首先卸载它。在命令行中,您可以使用以下命令卸载该分区:
|
||||
|
||||
```
|
||||
sudo umount /dev/sdc1
|
||||
```
|
||||
|
||||
注意:请将"/dev/sdc1"替换为您要卸载的分区的正确路径。
|
||||
|
||||
5. 在GParted界面中,单击菜单中的"设备"选项,然后选择"创建"。
|
||||
|
||||
6. 在弹出的对话框中,选择要转换为的文件系统类型。在这种情况下,选择"ext4"。然后单击"添加"按钮。
|
||||
|
||||
7. 在"操作"菜单中,选择"应用所有操作"。这将开始分区格式转换的过程。
|
||||
|
||||
8. 等待GParted完成转换操作。这可能需要一些时间,具体取决于分区的大小和系统性能。
|
||||
|
||||
9. 转换完成后,您将看到分区的文件系统已更改为ext4。
|
||||
|
||||
10. 最后,请确保挂载分区以便访问它。您可以使用以下命令挂载该分区:
|
||||
|
||||
```
|
||||
sudo mount /dev/sdc1 /media/fuqingxu/eb63a8fa-cee9-48a5-9f05-b1388c3fda9e
|
||||
```
|
||||
|
||||
注意:请将"/dev/sdc1"替换为已转换分区的正确路径,并将"/media/fuqingxu/eb63a8fa-cee9-48a5-9f05-b1388c3fda9e"替换为您要挂载的目标路径。
|
||||
|
||||
请注意,在执行任何分区操作之前,务必备份重要的数据。操作不当可能导致数据丢失。
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
md = """
|
||||
要将NTFS格式转换为ext4格式,需要执行以下步骤:
|
||||
|
||||
1. 首先,确保你的NTFS分区已经挂载。可以使用`df -lh`命令来检查。
|
||||
|
||||
2. 创建一个ext4文件系统。打开终端并执行以下命令:
|
||||
```
|
||||
sudo mkfs.ext4 /dev/sdc1
|
||||
```
|
||||
注意将`/dev/sdc1`替换为你要转换的NTFS分区的设备名称。
|
||||
|
||||
3. 等待文件系统创建完成。这可能需要一些时间。
|
||||
|
||||
4. 将新的ext4文件系统挂载到指定的挂载点。执行以下命令:
|
||||
```
|
||||
sudo mkdir /mnt/mydrive
|
||||
sudo mount -t ext4 /dev/sdc1 /mnt/mydrive
|
||||
```
|
||||
注意将`/mnt/mydrive`替换为你要挂载的目录。
|
||||
|
||||
5. 现在,可以将数据从原始NTFS分区复制到新的ext4分区。执行以下命令:
|
||||
```
|
||||
sudo rsync -avxP /media/fuqingxu/eb63a8fa-cee9-48a5-9f05-b1388c3fda9e /mnt/mydrive
|
||||
```
|
||||
注意将`/media/fuqingxu/eb63a8fa-cee9-48a5-9f05-b1388c3fda9e`替换为你的原始NTFS分区的挂载点。
|
||||
|
||||
6. 等待数据复制完成。这可能需要一些时间,具体取决于数据量。
|
||||
|
||||
7. 确认数据已经成功复制到新的ext4分区后,可以卸载原始NTFS分区。执行以下命令:
|
||||
```
|
||||
sudo umount /media/fuqingxu/eb63a8fa-cee9-48a5-9f05-b1388c3fda9e
|
||||
```
|
||||
|
||||
8. 现在,你可以将新的ext4分区重新挂载到原来的位置。执行以下命令:
|
||||
```
|
||||
sudo umount /mnt/mydrive
|
||||
sudo mount -t ext4 /dev/sdc1 /media/fuqingxu/eb63a8fa-cee9-48a5-9f05-b1388c3fda9e
|
||||
```
|
||||
|
||||
完成上述步骤后,NTFS分区将被转换为ext4格式,并且数据将被复制到新的分区中。请确保在执行任何操作之前备份重要数据。
|
||||
Replace 'Tex/' with the actual directory path where your files are located before running the script.
|
||||
|
||||
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user