add python

This commit is contained in:
2026-06-16 13:18:49 +08:00
parent ea1ba5c933
commit 4a37c27938
2 changed files with 54 additions and 5 deletions
+25 -4
View File
@@ -85,7 +85,12 @@
>C</button>
<button
class="lang-tab"
:class="{ active: language === 'python' }"
:class="{
active: language === 'python',
disabled: !hasPythonVersion
}"
:disabled="!hasPythonVersion"
:title="hasPythonVersion ? '切换到 Python 版本' : '该文件暂无 Python 版本'"
@click="switchLanguage('python')"
>Python</button>
</div>
@@ -129,11 +134,16 @@ const chapterId = computed(() => props.id || route.params.id)
const currentFile = ref(null)
const language = ref('c')
// 判断当前文件是否有 Python 版本
const hasPythonVersion = computed(() => {
return currentFile.value && !!currentFile.value.pyPath
})
// 根据语言生成文件路径
const currentFilePath = computed(() => {
if (!currentFile.value) return null
if (language.value === 'python') {
return currentFile.value.pyPath || currentFile.value.path.replace(/^c\//, 'py/').replace(/\.c$/, '.py')
return currentFile.value.pyPath
}
return currentFile.value.path
})
@@ -143,11 +153,17 @@ const currentFileName = computed(() => {
if (language.value === 'python') {
return currentFile.value.pyPath
? currentFile.value.pyPath.split('/').pop()
: currentFile.value.name.replace(/\.c$/, '.py')
: currentFile.value.name
}
return currentFile.value.name
})
// 切换语言时,如果目标语言没有对应文件则保持当前语言
function switchLanguage(lang) {
if (lang === 'python' && !hasPythonVersion.value) return
language.value = lang
}
function switchLanguage(lang) {
language.value = lang
}
@@ -458,7 +474,7 @@ function getFolderDescription(folderName) {
font-family: var(--mono);
}
.lang-tab:hover {
.lang-tab:hover:not(:disabled) {
color: var(--text-primary);
}
@@ -468,6 +484,11 @@ function getFolderDescription(folderName) {
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.lang-tab.disabled {
opacity: 0.4;
cursor: not-allowed;
}
.drawer-close-btn {
background: none;
border: none;
+29 -1
View File
@@ -1,9 +1,37 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// https://vite.dev/config/
export default defineConfig({
plugins: [vue()],
plugins: [
vue(),
// 让 Vite 开发服务器正确提供 .py 文件的静态服务
{
name: 'serve-py-files',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = req.url
// 只处理 .py 文件请求,且排除 API 路径
if (url.endsWith('.py') && !url.startsWith('/api/')) {
const filePath = path.join(__dirname, 'public', url.replace(/^\//, ''))
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf-8')
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.statusCode = 200
res.end(content)
return
}
}
next()
})
}
}
],
server: {
port: 1025,
proxy: {