aboutsummaryrefslogtreecommitdiff
path: root/generator/main.js
blob: 0d86ca5d5604c931eee049e4e2f7398938e7d099 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const content_dir='../content';
const template_dir='../templates';
const dest_dir='../generated';
const posts_per_listpage=5;

const fs=require('fs');
const path=require('path');

const scanner=require('./scanner');
const list=scanner.scan(content_dir,dest_dir);
const tags=scanner.build_list_index();
const taglist=Object.keys(tags).sort();
const force=process.argv.indexOf('--force')!=-1;

const postrenderer=require('./postrenderer');
postrenderer.set_template(path.join(template_dir,'post_template'));

function ensure_dir(p)
{
	let needcreation=false;
	try{
		if(!fs.statSync(p).isDirectory())
		{
			needcreation=true;
			fs.unlinkSync(p);
		}
	}catch(e){needcreation=true;}
	if(needcreation)fs.mkdirSync(p);
	if(!fs.statSync(p).isDirectory())throw 'shit';
}

const post_dir=path.join(dest_dir,'post');
ensure_dir(post_dir);
for(let j=0;j<list.length;++j){
const i=list[j];
if(i.needsupdate||force)
	postrenderer.render(
		path.join(content_dir,`${i.file}.txt`),
		path.join(post_dir,`${i.file}.html`),
		j?list[j-1].file:undefined,
		j<list.length-1?list[j+1].file:undefined
	)
		.then((r)=>{console.log(`rendered: ${r}`);})
}

const listrenderer=require('./listrenderer');
listrenderer.set_template(path.join(template_dir,'list_template'));

const list_dir=path.join(dest_dir,'list');
const ppp=posts_per_listpage;
ensure_dir(list_dir);
let pc=Math.floor(list.length/ppp)+(list.length%ppp!=0);
for(let i=0;i<pc;++i)
{
	const cl=list.slice(i*ppp,Math.min((i+1)*ppp,list.length));
	listrenderer.render(
		path.join(list_dir,`${i}.html`),
		{atag:'',tags:taglist,cp:i,pc:pc},
		cl
	);
}
try{
fs.unlinkSync(path.join(list_dir,'index.html'));
}catch(e){};
fs.symlinkSync('0.html',path.join(list_dir,'index.html'));

for(let i of taglist)
if(tags[i].needsupdate||force)
{
	const tl_dir=path.join(list_dir,i);
	ensure_dir(tl_dir);
	let pc=Math.floor(tags[i].posts.length/ppp)+(tags[i].posts.length%ppp!=0);
	for(let j=0;j<pc;++j)
	{
		const cl=tags[i].posts.slice(j*ppp,Math.min((j+1)*ppp,tags[i].posts.length));
		listrenderer.render(
			path.join(tl_dir,`${j}.html`),
			{atag:i,tags:taglist,cp:j,pc:pc},
			cl
		);
	}
	try{
	fs.unlinkSync(path.join(tl_dir,'index.html'));
	}catch(e){};
	fs.symlinkSync('0.html',path.join(tl_dir,'index.html'));
}