BibTeX转化为默认格式

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <title>BibTeX 到自定义格式转换器</title>
</head>

<body class="bg-gray-100 font-sans">
    <div class="container mx-auto p-8">
        <div class="bg-white p-6 rounded-md shadow-md">
            <h2 class="text-xl font-bold mb-4">输入 BibTeX</h2>
            <textarea id="bibtex-input" class="w-full h-48 border border-gray-300 p-2 rounded-md resize-none">@inproceedings{
hendrycks2021measuring,
title={Measuring Massive Multitask Language Understanding},
author={Dan Hendrycks and Collin Burns and Steven Basart and Andy Zou and Mantas Mazeika and Dawn Song and Jacob Steinhardt},
booktitle={International Conference on Learning Representations},
year={2021},
url={https://openreview.net/forum?id=d7KBjmI3GmQ}
}</textarea>
            <button id="convert-button"
                class="mt-4 bg-blue-500 text-white py-2 px-4 rounded-md hover:bg-blue-600 transition duration-300">转换</button>
        </div>
        <div class="mt-8 bg-white p-6 rounded-md shadow-md">
            <h2 class="text-xl font-bold mb-4">输出自定义格式</h2>
            <textarea id="output" class="w-full h-48 border border-gray-300 p-2 rounded-md resize-none" readonly></textarea>
        </div>
    </div>

    <script>
        document.getElementById('convert-button').addEventListener('click', () => {
            const bibtex = document.getElementById('bibtex-input').value;
            const lines = bibtex.split('\n');
            let currentEntry = {};
            let inEntry = false;

            lines.forEach(line => {
                if (line.trim().startsWith('@')) {
                    inEntry = true;
                    currentEntry = {};
                } else if (line.trim() === '}') {
                    inEntry = false;
                } else if (inEntry) {
                    const parts = line.split('=');
                    if (parts.length === 2) {
                        const key = parts[0].trim();
                        let value = parts[1].trim().replace(/[{},]/g, '').replace(/"/g, '');
                        currentEntry[key] = value;
                    }
                }
            });

            const authorList = currentEntry.author ? currentEntry.author.split(' and ') : [];
            let authorStr = '';
            if (authorList.length > 3) {
                authorStr = authorList.slice(0, 3).map(author => {
                    const parts = author.split(' ');
                    const lastName = parts.pop();
                    const initials = parts.map(name => name[0]).join('');
                    return `${lastName} ${initials}`;
                }).join(', ') + ', et al.';
            } else {
                authorStr = authorList.map(author => {
                    const parts = author.split(' ');
                    const lastName = parts.pop();
                    const initials = parts.map(name => name[0]).join('');
                    return `${lastName} ${initials}`;
                }).join(', ');
            }

            const title = currentEntry.title ? currentEntry.title.replace(/[{}]/g, '') : 'unknown';
            const booktitle = currentEntry.booktitle || 'unknown';
            const year = currentEntry.year || 'unknown';

            const output = `${authorStr}. ${title}[C]//${booktitle}. ${year}.`;
            document.getElementById('output').value = output;
        });
    </script>
</body>

</html>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CSPhD-winston-杨帆

给我饭钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值