Update 'scripts/compile-lexico.html'

This commit is contained in:
方而静 2022-10-27 09:38:54 +00:00
parent f3f07d97c6
commit d2f0b36e07

View File

@ -1,230 +1,264 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>C'pesolino Lexici Compiler</title> <title>C'pesolino Lexici Compiler</title>
</head> </head>
<body> <body>
<label for=input>lexici texto:</label><br/><textarea id=input></textarea><br/> <label for=input>lexici texto:</label><br/><textarea id=input></textarea><br/>
<button onclick="parse()">alqa</button> <label for=szinput>papro caito:</label><br/><input id=szinput placeholder="a4/16k" value="a4"/><br/>
<p id=err></p> <button onclick="parse()">alqa</button>
<p id=err></p>
<label for=output>markdown frutco:</label><br/>
<textarea id=output readonly></textarea> <label for=output>markdown frutco:</label><br/>
<textarea id=output readonly></textarea>
<label for=output2>LaTeX frutco:</label><br/>
<textarea id=output2 readonly></textarea> <label for=output2>LaTeX frutco:</label><br/>
<textarea id=output2 readonly></textarea>
</body>
</body>
<style type="text/css">
#err { <style type="text/css">
color: red; #err {
} color: red;
}
textarea {
width: 100%; textarea {
height: 300px; width: 100%;
} height: 300px;
</style> }
</style>
<script type="text/javascript">
<script type="text/javascript">
const auxi = ['b','c','d','f','g','h','i','l','m','n','p','q','r','s','t','v','w','z','zh','dr','ts'];
const basi = ['a','e','i','o','u','ü','ar','er','ir','or','ur','ür','ai','ei','oi','al','el','ul','ül','ao','eo','io','üo','eu','ua','au','an','en','in','on','un','ing','am','em','im','um','oen']; const auxi = ['b','c','d','f','g','h','i','l','m','n','p','q','r','s','t','v','w','z','zh','dr','ts'];
const amark = {"a": "\\\"a","b": "p","e": "e","c": "k\\super{h}","d": "t", "dd": "t", "o": "o","f": "f","u": "u","g": "g","ü": "y","h": "h","ar": "A\\textrhookschwa","i": "j","er": "7\\textrhookschwa","l": "l","ir": "i\\textrhookschwa","m": "m","or": "o\\textrhookschwa","n": "n","ur": "u\\textrhookschwa","p": "p\\super{h}","ür": "y\\textrhookschwa","q": "\\:t\\:s\\super{h}","ai": "aj","r": "\\:R\\super{w}","ei": "ej","s": "s","oi": "oj","t": "t\\super{h}","al": "6\\textltilde","v": "v","el": "eo\\textltilde","w": "w","ul": "u\\textltilde","z(1th)": "ts","ül": "4u\\textltilde","z(exa)": "z","ao": "Aw","zh": "\\:z","eo": "eo","dr": "\\t{\\:t\\:R}\\super{w}","io": "jo","ts": "ts\\super{h}","üo": "4o","eu": "ju","ua": "w\\\"a","au": "6","an": "an","en": "@n","in": "in","on": "on","un": "2n","am": "\\\"am","em": "@m","im": "im","um": "2m","ing": "iN","oen": "ow@n"}; const basi = ['a','e','i','o','u','ü','ar','er','ir','or','ur','ür','ai','ei','oi','al','el','ul','ül','ao','eo','üo','eu','ua','au','an','en','in','on','un','ing','am','em','im','um','oen'];
const bmark = {"a": "\\\"a","b": "p","e": "e","c": "k\\super{h}","i": "i","d": "t", "dd": "t", "o": "o","f": "f","u": "u","g": "g","ü": "y","h": "h","ar": "A\\textrhookschwa","er": "7\\textrhookschwa","l": "l","ir": "i\\textrhookschwa","m": "m","or": "o\\textrhookschwa","n": "n","ur": "u\\textrhookschwa","p": "p\\super{h}","ür": "y\\textrhookschwa","q": "\\:t\\:s\\super{h}","ai": "aj","r": "\\:R\\super{w}","ei": "ej","s": "s","oi": "oj","t": "t\\super{h}","al": "6\\textltilde","v": "v","el": "eo\\textltilde","w": "w","ul": "u\\textltilde","z(1th)": "ts","ül": "4u\\textltilde","z(exa)": "z","ao": "Aw","zh": "\\:z","eo": "eo","dr": "\\t{\\:t\\:R}\\super{w}","io": "jo","ts": "ts\\super{h}","üo": "4o","eu": "ju","ua": "w\\\"a","au": "6","an": "an","en": "@n","in": "in","on": "on","un": "2n","am": "\\\"am","em": "@m","im": "im","um": "2m","ing": "iN","oen": "ow@n"}; const amark = {"a": "\\\"a","b": "p","e": "e","c": "k\\super{h}","d": "t", "dd": "t", "o": "o","f": "f","u": "u","g": "k","ü": "y","h": "h","ar": "A\\textrhookschwa","i": "j","er": "7\\textrhookschwa","l": "l","ir": "i\\textrhookschwa","m": "m","or": "o\\textrhookschwa","n": "n","ur": "u\\textrhookschwa","p": "p\\super{h}","ür": "y\\textrhookschwa","q": "\\:t\\:s\\super{h}","ai": "aj","r": "\\:R\\super{w}","ei": "ej","s": "s","oi": "oj","t": "t\\super{h}","al": "6\\textltilde","v": "v","el": "eo\\textltilde","w": "w","ul": "u\\textltilde","z(1th)": "ts","ül": "4u\\textltilde","z(exa)": "z","ao": "Aw","zh": "\\:z","eo": "eo","dr": "\\t{\\:t\\:R}\\super{w}","ts": "ts\\super{h}","üo": "4o","eu": "ju","ua": "w\\\"a","au": "6","an": "an","en": "@n","in": "in","on": "on","un": "2n","am": "\\\"am","em": "@m","im": "im","um": "2m","ing": "iN","oen": "ow@n"};
const bmark = {"a": "\\\"a","b": "p","e": "e","c": "k\\super{h}","i": "i","d": "t", "dd": "t", "o": "o","f": "f","u": "u","g": "k","ü": "y","h": "h","ar": "A\\textrhookschwa","er": "7\\textrhookschwa","l": "l","ir": "i\\textrhookschwa","m": "m","or": "o\\textrhookschwa","n": "n","ur": "u\\textrhookschwa","p": "p\\super{h}","ür": "y\\textrhookschwa","q": "\\:t\\:s\\super{h}","ai": "aj","r": "\\:R\\super{w}","ei": "ej","s": "s","oi": "oj","t": "t\\super{h}","al": "6\\textltilde","v": "v","el": "eo\\textltilde","w": "w","ul": "u\\textltilde","z(1th)": "ts","ül": "4u\\textltilde","z(exa)": "z","ao": "Aw","zh": "\\:z","eo": "eo","dr": "\\t{\\:t\\:R}\\super{w}","ts": "ts\\super{h}","üo": "4o","eu": "ju","ua": "w\\\"a","au": "6","an": "an","en": "@n","in": "in","on": "on","un": "2n","am": "\\\"am","em": "@m","im": "im","um": "2m","ing": "iN","oen": "ow@n"};
class Lexico {
constructor(val, trans, pt, source) { class Lexico {
this.v = val.trim().replaceAll('ii', 'ü'); constructor(val, trans, pt, source) {
this.trans = trans.trim().split('/'); this.v = val.trim().replaceAll('ii', 'ü');
this.pt = pt.trim().split('').map(x => parseInt(x)); this.trans = trans.trim().split('/');
this.source = parse_source(source.trim()); this.pt = pt.trim().split('').map(x => parseInt(x));
} this.source = parse_source(source.trim());
}
get_pt() {
if (this.pt.length === 0) { get_pt() {
return '/'; if (this.pt.length === 0) {
} return '/';
let pv = this.v.replaceAll('x', 'cs').replaceAll('dd', 'd').replaceAll('\'', ''); }
if (this.pt.reduce((x, y) => x + y, 0) != pv.length) { let pv = this.v.toLowerCase().replaceAll('x', 'cs').replaceAll('dd', 'd').replaceAll('\'', '');
report_error('ling divo mifuto'); if (this.pt.reduce((x, y) => x + y, 0) != pv.length) {
return null; report_error('ling divo mifuto');
} return null;
let res = '', c = 0, pti = 0; }
for (let i = 0; i < pv.length; i += 1) { let res = '', c = 0, pti = 0;
res += pv[i]; for (let i = 0; i < pv.length; i += 1) {
c += 1; res += pv[i];
if (c == this.pt[pti] && i + 1 < pv.length) { c += 1;
c = 0; if (c == this.pt[pti] && i + 1 < pv.length) {
res += '/'; c = 0;
pti += 1; res += '/';
} pti += 1;
} }
return res; }
} return res;
}
get_pt2() {
if (this.pt.length === 0) { get_pt2() {
return []; if (this.pt.length === 0) {
} return [];
let pv = this.v.replaceAll('x', 'cs').replaceAll('dd', 'd').replaceAll('\'', ''); }
if (this.pt.reduce((x, y) => x + y, 0) != pv.length) { let pv = this.v.toLowerCase().replaceAll('x', 'cs').replaceAll('dd', 'd').replaceAll('\'', '');
report_error('ling divo mifuto'); if (this.pt.reduce((x, y) => x + y, 0) != pv.length) {
return null; report_error('ling divo mifuto');
} return null;
let res = [], c = 0, pti = 0, cv = ''; }
for (let i = 0; i < pv.length; i += 1) { let res = [], c = 0, pti = 0, cv = '';
cv += pv[i]; for (let i = 0; i < pv.length; i += 1) {
c += 1; cv += pv[i];
if (c == this.pt[pti]) { c += 1;
c = 0; if (c == this.pt[pti]) {
res.push(cv); c = 0;
cv = ''; res.push(cv);
pti += 1; cv = '';
} pti += 1;
} }
return res; }
} return res;
}
get_trans() {
return this.trans.reduce((x, y) => x + y + '', ''); get_trans() {
} return this.trans.reduce((x, y) => x + y + '', '');
} }
}
let LINE = 0;
const source_types = []; let LINE = 0;
const source_types = [];
function parse_source(s) {
// TODO: impl this function parse_source(s) {
return s; // TODO: impl this
} return s;
}
function report_error(x) {
document.getElementById('err').innerHTML = `focopliqo sa rovo ${LINE}: ${x}`; function report_error(x) {
} document.getElementById('err').innerHTML = `focopliqo sa rovo ${LINE}: ${x}`;
}
function parse_pt_to_marco(pt) {
if (pt.length == 0) { return '/'; } function parse_pt_to_marco(pt) {
if (pt.length == 0) { return '/'; }
let res = '[';
for (let i = 0; i < pt.length; i += 1) { let res = '[';
let pti = pt[i]; for (let i = 0; i < pt.length; i += 1) {
let first_auxalino = '', rest = pti; let pti = pt[i];
for (const a of auxi) { let first_auxalino = '', rest = pti;
if (pti.startsWith(a)) { for (const a of auxi) {
first_auxalino = a; if (pti.startsWith(a)) {
rest = pti.substring(a.length); first_auxalino = a;
// dont break here for parsing ts, dr and zh! rest = pti.substring(a.length);
} // dont break here for parsing ts, dr and zh!
} }
}
if (rest.length == 0) {
if (first_auxalino == 'i') { if (rest.length == 0) {
res += bmark['i']; if (first_auxalino == 'i') {
} else if (first_auxalino == 'z') { res += bmark['i'];
res += amark[i == 0 ? 'z(1th)' : 'z(exa)']; } else if (first_auxalino == 'z') {
} else { res += amark[i == 0 ? 'z(1th)' : 'z(exa)'];
res += amark[first_auxalino]; } else {
} res += amark[first_auxalino];
continue; }
} continue;
}
let basolino = '', rauxa = '';
for (const b of basi) { if (first_auxalino == 'i') {
if (rest.startsWith(b)) { let first_basolino = '';
basolino = b; for (const b of basi) {
rauxa = rest.substring(b.length); if (pti.startsWith(b)) {
// dont break here! first_basolino = b;
} // dont break here!
} }
}
if (first_auxalino == 'i' && basolino.length == 0) {
basolino = 'i'; if (first_basolino != 'i') {
first_auxalino = ''; first_auxalino = '';
} rest = pti;
}
if ((first_auxalino.length > 0 && basolino.length == 0) || (rauxa.length > 0 && !auxi.includes(rauxa))) { }
report_error(`ling divo mifuto2: ${pti}`);
return null; let basolino = '', rauxa = rest;
} for (const b of basi) {
if (rest.startsWith(b)) {
if (first_auxalino.length > 0) { basolino = b;
if (first_auxalino == 'z') { rauxa = rest.substring(b.length);
res += amark[i == 0 ? 'z(1th)' : 'z(exa)']; // dont break here!
} else { }
res += amark[first_auxalino]; }
}
} if (first_auxalino == 'i' && basolino.length == 0) {
res += '{}'; basolino = 'i';
if (basolino.length > 0) { first_auxalino = '';
res += bmark[basolino]; }
}
res += '{}'; if (basolino.length == 0 || (rauxa.length > 0 && !auxi.includes(rauxa))) {
if (rauxa.length > 0) { report_error(`ling divo mifuto2: ${pti}`);
if (rauxa == 'z') { return null;
res += amark[rauxa + '(exa)']; }
} else {
res += amark[rauxa]; if (first_auxalino.length > 0) {
} if (first_auxalino == 'z') {
} res += amark[i == 0 ? 'z(1th)' : 'z(exa)'];
res += '{}'; } else {
} res += amark[first_auxalino];
return res + ']'; }
} }
res += '{}';
function escape_tex(s) { if (basolino.length > 0) {
return s res += bmark[basolino];
.replaceAll('\\', '\\textbackslash{}') }
.replaceAll('+', ' + ') res += '{}';
.replaceAll('(', ' (') if (rauxa.length > 0) {
.replaceAll(')', ') ') if (rauxa == 'z') {
.replaceAll('<', '\\textless{}') res += amark[rauxa + '(exa)'];
.replaceAll('>', '\\textgreater{}') } else {
.replaceAll('#', '\\#') res += amark[rauxa];
.replaceAll('&', '\\&') }
.replaceAll('~', '$\\sim$'); }
} res += '{}';
}
function parse() { return res + ']';
document.getElementById('err').innerHTML = ''; }
document.getElementById('output').value = '';
let t = document.getElementById('input').value.split('\n'); function escape_tex(s) {
let markres = '| 单词 | 释义 | 音节划分 | 词源 |\n| :---: | :---: | :---: | :---: |\n'; return s
let texres = '\\documentclass[10pt]{article}\\usepackage{tabularx}\\usepackage{ctex}\\usepackage{tipa}\\usepackage{geometry}\\geometry{a4paper,left=1cm,right=1cm,top=1.2cm,bottom=1.5cm}\\usepackage{xltabular}\\begin{document}\\newcolumntype{Y}{>{\\raggedright\\arraybackslash}X}\\centering\\begin{xltabular}{\\textwidth}{|Y|Y|l|Y|}\\hline \\textbf{Lexici} & \\textbf{Texo} & \\textbf{Lingmarco} & \\textbf{Matro} \\\\ \\hline\n'; .replaceAll('\\', '\\textbackslash{}')
for (let i in t) { .replaceAll('+', ' + ')
LINE = parseInt(i) + 1; .replaceAll('(', ' (')
let l = parse_item(t[i]); .replaceAll(')', ') ')
if (l == null) { .replaceAll('<', '\\textless{}')
return null; .replaceAll('>', '\\textgreater{}')
} .replaceAll('#', '\\#')
let pt = l.get_pt(); .replaceAll('&', '\\&')
if (pt == null) { .replaceAll('~', '$\\sim$');
console.log(t[i]); }
return null;
} const tex_sizing = {
markres += `| ${l.v} | ${l.get_trans()} | ${pt} | ${l.source} |\n`; 'a4': '\\geometry{a4paper,left=1cm,right=1cm,top=1.5cm,bottom=1.5cm,footskip=5mm}',
texres += `\t${escape_tex(l.v)} & ${escape_tex(l.get_trans())} & \\textipa{${parse_pt_to_marco(l.get_pt2())}} & ${escape_tex(l.source)} \\\\\n\t\\hline\n`; '16k': '\\geometry{paperwidth=185mm,paperheight=260mm,left=1cm,right=1cm,top=1.5cm,bottom=1.5cm,footskip=5mm}',
} }
texres += `\\end{xltabular}\\end{document}`
document.getElementById('output').value = markres; function parse() {
document.getElementById('output2').value = texres; document.getElementById('err').innerHTML = '';
return true; document.getElementById('output').value = '';
} document.getElementById('output2').value = '';
let papro_caito = document.getElementById('szinput').value;
function parse_item(val) { let t = document.getElementById('input').value.split('\n');
let p = val.match(/^([^\:]+):[ \t]*([^\[]+)[ \t]*\[(\d*)\;[ \t]*([^\]]*)\][ \r\n\t]*$/); let markres = '| 单词 | 释义 | 音节划分 | 词源 |\n| :---: | :---: | :---: | :---: |\n';
if (p == null) { let texres = `\\documentclass[10pt]{article}\\usepackage{tabularx}\\usepackage{ctex}\\usepackage{tipa}\\usepackage{geometry}${tex_sizing[papro_caito]}\\usepackage{xltabular}\\begin{document}\\newcolumntype{Y}{>{\\raggedright\\arraybackslash}X}\\centering\\footnotesize\\begin{xltabular}{\\textwidth}{|Y|Y|l|Y|}\\hline\\textbf{Lexici} & \\textbf{Texo} & \\textbf{Lingmarco} & \\textbf{Matro} \\\\ \\hline\n`;
console.error(val); for (let i in t) {
report_error('lexo mifuto.'); if (t.length == 0) {
return null; continue;
} }
return new Lexico(p[1], p[2], p[3], p[4]); LINE = parseInt(i) + 1;
} let l = parse_item(t[i]);
if (l == null) {
</script> return null;
}
let pt = l.get_pt();
if (pt == null) {
console.log(t[i]);
return null;
}
markres += `| ${l.v} | ${l.get_trans()} | ${pt} | ${l.source} |\n`;
let lingmarco = parse_pt_to_marco(l.get_pt2());
if (lingmarco == null) return null;
texres += `\t${escape_tex(l.v)} & ${escape_tex(l.get_trans())} & \\textipa{${lingmarco}} & ${escape_tex(l.source)} \\\\\n\t\\hline\n`;
}
texres += `\\end{xltabular}\\end{document}`
document.getElementById('output').value = markres;
document.getElementById('output2').value = texres;
return true;
}
function parse_item(val) {
let p = val.match(/^([^\:]+):[ \t]*([^\[]+)[ \t]*\[(\d*)\;[ \t]*([^\]]*)\][ \r\n\t]*$/);
if (p == null) {
console.error(val);
report_error('lexo mifuto.');
return null;
}
return new Lexico(p[1], p[2], p[3], p[4]);
}
onload = function() {
document.getElementById('output').value = '';
document.getElementById('output2').value = '';
}
</script>
</html> </html>