aboutsummaryrefslogtreecommitdiff
path: root/generator/listrenderer.js
blob: fd58b3ac89b6d14ece8fb6cab09da94f19213862 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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,trd.documentElement.outerHTML,'utf8');
	return outf;
}