module.exports={
render:function(inf,outf,np,pp){return _render(inf,outf,np,pp);},
set_template:function(tmplf){return _set_template(tmplf);}
};
const fs=require('fs');
const jsdom=require('jsdom');
const path=require('path');
const aes=require('aes-js');
const scrypt=require('scrypt-js');
const btoa=require('btoa');
const sha256=require('./sha256').sha256;
const spawn=require('child_process').spawn;
let template_file='';
let tmplcont='';
let tocid;
let headerlist;
function _dfs(doc,el,le,p)
{
var e=doc.createElement('li');
e.innerHTML=`${el.innerHTML}`;
le.appendChild(e);
el.id='tocanch'+(tocid++);
el.classList.add('tvis');
var che=null,i;
for(i=p+1;i["+(starting+i+1)+"]: "+s+"
";
doc.getElementById("notediv").appendChild(span);
}
starting+=footnotes.length;
while(footnotes.length)footnotes[0].remove();
}
async function encrypt(doc)
{
let enid=0;
for(let i of doc.querySelectorAll('encrypted'))
{
const key=i.getAttribute('key');
i.removeAttribute('key');
const contu8=aes.utils.utf8.toBytes(i.innerHTML);
const conths=sha256.hash(aes.utils.hex.fromBytes(contu8),{msgFormat:'hex-bytes'});
const keyu8=aes.utils.utf8.toBytes(key);
const saltu8=aes.utils.utf8.toBytes('hellwhymustiaddsalttothiscrap');
const enckey=await new Promise(
(resolv,rej)=>
{scrypt(keyu8,saltu8,1024,16,2,32,(e,p,k)=>{e?rej(e):k?resolv(k):undefined;});}
);
const ctr=new aes.ModeOfOperation.ctr(enckey);
const enccont=ctr.encrypt(contu8);
let encconts='';for(let i=0;ihere
to decrypt. `;
++enid;
}
}
function _set_template(tmplf)
{
template_file=tmplf;
tmplcont=fs.readFileSync(template_file,'utf8');
}
async function _render(inf,outf,np,pp)
{
const postcont=fs.readFileSync(inf,'utf8');
const tr=new jsdom.JSDOM(tmplcont);
const trd=tr.window.document;
const cont=fs.readFileSync(inf,'utf8');
const contsplit=cont.split('\n');
const meta=contsplit.splice(0,4);
const preproc=meta[3].trim();
trd.getElementById('title').innerHTML=
trd.getElementById('titleh').innerHTML=meta[0].trim();
trd.getElementById('datetags').innerHTML=`${meta[1].trim()}
#${meta[2].split(',').join(' #')}`;
if(preproc.length)
{
const ppargs=preproc.split(' ');
const ppcmd=ppargs.splice(0,1);
const pp=spawn(ppcmd[0],ppargs,{maxBuffer:1048576});
let out='';
pp.stdout.setEncoding('utf8');
pp.stdout.on('data',(d)=>{out+=d;});
pp.stdin.write(contsplit.join('\n'),'utf8');
const ppp=new Promise(
(resolv,rej)=>
{
pp.on('close',(r)=>{if(r)rej(r);resolv(out);});
}
);
pp.stdin.end();
trd.getElementById('article').innerHTML=await ppp;
}
else
trd.getElementById('article').innerHTML=contsplit.join('\n');
//Encryption
await encrypt(trd);
//TOC
const l=trd.getElementById('article').querySelectorAll('h2,h3,h4,h5,h6');
const tocroot=trd.getElementById('tocroot');
tocid=0;
headerlist=[];
for(let i of l)
if(!i.classList.contains('notoc'))headerlist.push(i);
for(let i=0;i${tgs[i]}`;
trd.getElementById('tagslist').appendChild(l);
}
//neighboring posts
if(pp)trd.getElementById('prevp').href=`${pp}.html`;
if(np)trd.getElementById('nextp').href=`${np}.html`;
fs.writeFileSync(outf,trd.documentElement.outerHTML,'utf8');
return outf;
}