if(!text.trim()) return '';
const lowerCaseWords = {
'a':1,'an':1,'the':1,
'and':1,'but':1,'or':1,'nor':1,'yet':1,'so':1,
'for':1,'of':1,'in':1,'on':1,'at':1,'to':1,'by':1,'with':1,'from':1,'as':1,'up':1,'per':1,'via':1,'into':1,'onto':1,'than':1,'till':1,'upon':1
};
const alwaysCapitalize = {'i':1,'ii':1,'iii':1,'iv':1,'v':1,'vi':1,'vii':1,'viii':1,'ix':1,'x':1};
let words = text.split(/\s+/);
return words.map((word, idx) => {
const w = word.toLowerCase();
const isFirstOrLast = idx === 0 || idx === words.length - 1;
if(alwaysCapitalize[w] && (w === 'i' || /^[ivxlcdm]+$/.test(w))) return w.toUpperCase();
if(style === 'ap'){
if(isFirstOrLast) return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
if(word.length <= 3 && lowerCaseWords[w] && w !== 'is' && w !== 'are' && w !== 'be' && w !== 'am') return w;
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
if(style === 'apa'){
if(isFirstOrLast) return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
if(lowerCaseWords[w]) return w;
if(/^[a-z]+$/.test(word)) return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
return word;
}
if(style === 'chicago' || style === 'mla'){
if(isFirstOrLast) return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
if(lowerCaseWords[w] && w.length <= 3) return w;
if(w === 'to' && style === 'mla') return w;
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}).join(' ');
}
function convertTitle(){
const text = document.getElementById('input-text').value;
const container = document.getElementById('results');
if(!text.trim()){
container.innerHTML = '';
return;
}
const styles = [
{label:'AP Style (美联社)', key:'ap'},
{label:'APA Style (学术)', key:'apa'},
{label:'Chicago Style', key:'chicago'},
{label:'MLA Style', key:'mla'},
{label:'简单标题 (Simple)', key:'simple'}
];
container.innerHTML = styles.map(s => {
const result = s.key === 'simple'
? text.split(/\s+/).map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join(' ')
: toTitleCase(text, s.key);
return `
\x3Cdiv class="result-card">
\x3Cdiv class="card-title">
\x3Cspan>${s.label}\x3C/span>
\x3Cbutton class="copy-btn" onclick="copyText(this)" data-text="${encodeURIComponent(result)}">📋 复制\x3C/button>
\x3C/div>
\x3Cdiv class="card-content">${result || '(空)'}\x3C/div>
\x3C/div>`;
}).join('');
}
function copyText(btn){
const text = decodeURIComponent(btn.dataset.text);
navigator.clipboard.writeText(text).then(()=>showToast('已复制')).catch(()=>showToast('复制失败'));
}