0.9.1添加数学函数

This commit is contained in:
跨越晨昏 2024-09-22 19:44:35 +08:00
parent 6de7e32ca3
commit b31bb0acaa
5 changed files with 29 additions and 4 deletions

View File

@ -4,7 +4,7 @@ import re
import markdown import markdown
try: # 检测当前平台是否支持扩展语法 try: # 检测当前平台是否支持扩展语法
import Extra import CrossMore
EXTRA_ABLE = True EXTRA_ABLE = True
except ModuleNotFoundError: except ModuleNotFoundError:
EXTRA_ABLE = False EXTRA_ABLE = False
@ -239,7 +239,20 @@ class CodeBlock:
elif re.match(r'\$[^$]*\$', code): # 是LaTex代码(单行) elif re.match(r'\$[^$]*\$', code): # 是LaTex代码(单行)
self.codes[index] = re.sub(fr'\$([^$]*)\$', r'<p>\(\1\)</p>', code) self.codes[index] = re.sub(fr'\$([^$]*)\$', r'<p>\(\1\)</p>', code)
elif re.match(r'¥[^$]*¥', code): # 是数学函数(单行) elif re.match(r'¥[^$]*¥', code): # 是数学函数(单行)
self.codes[index] = re.sub(fr'¥([^$]*)¥', r'<p>\(\1\)</p>', code) if EXTRA_ABLE:
expression, range_ = re.findall(r'¥([^$]*)¥(€[^$]*€)?', code)[0] # 分离表达式与范围(如果有)
x_r = (-10, 10)
y_r = (-20, 20)
if range_ != '': # 定义了范围
ranges = range_[1:-1].split('|')
if len(ranges) in (1, 2): # 定义的范围正确
x_r = tuple(int(i) for i in ranges[0].split(','))
if len(ranges) == 2: # 定义了y范围
y_r = tuple(int(i) for i in ranges[1].split(','))
self.codes[index] = CrossMore.function_drawing(function=lambda x: eval(expression.split('=')[1]), x_range=x_r, y_range=y_r)
else:
self.codes[index] = '<mark>该平台不支持扩展语法</mark>'
else: # 是突出块 else: # 是突出块
self.codes[index] = f'<span class="block">{code}</span>' self.codes[index] = f'<span class="block">{code}</span>'

View File

@ -4,7 +4,7 @@ import base64
from io import BytesIO from io import BytesIO
def plot_function_to_base64(function, x_range=(-10, 10), y_range=(-20, 20), dpi=100): def function_drawing(function, x_range=(-10, 10), y_range=(-20, 20), dpi=100):
# 创建一个图像和坐标轴对象 # 创建一个图像和坐标轴对象
fig, ax = plt.subplots() fig, ax = plt.subplots()

File diff suppressed because one or more lines are too long

View File

@ -45,6 +45,10 @@ ___
3.1.1 LaTex 3.1.1 LaTex
`$CO_2$` `$CO_2$`
`$H_2O$` `$H_2O$`
3.1.2 函数
`¥y=x*2+1¥` // 不定义范围
`¥y=x**2¥€-50,50€` // 定义了x范围
`¥y=x**3¥€-50,50|-100,100€` // 定义了y范围
3.2 多行 3.2 多行
3.2.1 YAML 3.2.1 YAML
` `

View File

@ -12,7 +12,10 @@ setuptools.setup(
long_description=long_description, long_description=long_description,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",
url="https://github.com/CrossDark/CrossDown", url="https://github.com/CrossDark/CrossDown",
packages=setuptools.find_packages(), py_modules=[
'CrossDown',
'CrossMore',
],
install_requires=[ install_requires=[
'markdown', 'markdown',
], ],