aboutsummaryrefslogblamecommitdiff
path: root/generator/listrenderer.js
blob: e1fd7971de3c55262db44be1e4d84f3e6f354075 (plain) (tree)
1
2
3
4
5
6
7
8
9

                            





                                                                               
                           












                                                       
 










































                                                                                                                                       

                                                                                      

                    
//Copyright Chris Xiong 2018
//License: Expat (MIT)
module.exports={
	render:function(outf,pginfo,posts){return _render(outf,pginfo,posts);},
	set_template:function(tmplf){return _set_template(tmplf);}
};
const fs=require('fs');
const jsdom=require('jsdom');
const path=require('path');
let template_file='';
let tmplcont='';

function _set_template(tmplf)
{
	template_file=tmplf;
	tmplcont=fs.readFileSync(template_file,'utf8');
}

async function _render(outf,pginfo,posts)
{
	const tr=new jsdom.JSDOM(tmplcont);
	const trd=tr.window.document;

	const tags_list=trd.getElementById('tagslist');
	const posts_list=trd.getElementById('postslist');
	const atag_template=trd.getElementById('active_taglist_item_template');
	const tag_template=trd.getElementById('taglist_item_template');
	const pitm_template=trd.getElementById('post_item_template');

	for(let i of pginfo.tags)
	{
		const newnode=(i==pginfo.atag?atag_template:tag_template).cloneNode(true);
		newnode.id='';
		const a=newnode.querySelector('a');
		a.innerHTML=i;
		a.href=`/blog/list/${i}`;
		tags_list.appendChild(newnode);
	}

	for(let i of posts)
	{
		let newnode=pitm_template.cloneNode(true);
		newnode.id='';
		const mdate=new Date(i.mdate);
		const mdstr=`${mdate.getFullYear()}-${(mdate.getMonth()+1+'').padStart(2,'0')}-${(mdate.getDate()+'').padStart(2,'0')}`
		newnode.querySelector('#title').innerHTML=i.title;
		newnode.querySelector('#title').href=`/blog/post/${i.file}.html`;
		newnode.querySelector('#tags').innerHTML='#'+i.tags.split(',').join(' #');
		newnode.querySelector('#time').innerHTML=`${i.date}/${mdstr}`;
		newnode.querySelector('#title').id=
		newnode.querySelector('#tags').id=
		newnode.querySelector('#time').id='';
		posts_list.appendChild(newnode);
	}

	trd.getElementById('page').innerHTML=`${pginfo.cp+1}/${pginfo.pc}`;
	trd.getElementById('prepage').href=pginfo.cp>0?
		`/blog/list/${pginfo.atag?pginfo.atag+'/':''}${pginfo.cp-1}.html`
		:'#';
	trd.getElementById('nexpage').href=pginfo.cp<pginfo.pc-1?
		`/blog/list/${pginfo.atag?pginfo.atag+'/':''}${pginfo.cp+1}.html`
		:'#';

	atag_template.parentNode.removeChild(atag_template);
	tag_template.parentNode.removeChild(tag_template);
	pitm_template.parentNode.removeChild(pitm_template);

	fs.writeFileSync(outf,"<!DOCTYPE html>"+trd.documentElement.outerHTML,'utf8');
	return outf;
}