aboutsummaryrefslogblamecommitdiff
path: root/generator/main.js
blob: 87c58850ba1ab28ac2be999ed57c6f2a7528b0a9 (plain) (tree)
1
2
3
               

                            














                                                         




                                   
                                                          




                                                
                                                                          














                                                      
                                                 
                     
              


                               
                                      
                                                              




                                                            
                                                  


                                             
                                                                          
 
                                                              



                                                       

                                                 



































                                                                                             
#!/usr/bin/node
//Copyright Chris Xiong 2018
//License: Expat (MIT)
const config = {
	content_dir: '../content',
	template_dir: '../templates',
	dest_dir: '../generated',
	posts_per_listpage: 5,
	// These are only used by the Atom feed generator
	atom_feed_nposts: 10,
	version_string: '2.6',
	atom_url: 'https://chrisoft.org/blog/atom.xml',
	published_url: 'https://chrisoft.org/blog/',
	atom_title: 'Specluncam Ursae',
	atom_subtitle: 'Chris Xiong\'s blog posts',
	atom_author: 'Chris Xiong',
	atom_icon: 'https://chrisoft.org/cx.png'
};

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

const scanner=require('./scanner');
let list=scanner.scan(config.content_dir,config.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(config.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(config.dest_dir,'post');
ensure_dir(post_dir);
const prps=[];
for(let j=0;j<list.length;++j){
const i=list[j];
if(i.needsupdate||force)
	prps.push(postrenderer.render(
		path.join(config.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}`);})
		.catch((e)=>{console.error(e);}));
}

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

list = list.filter((t) => (t.date.indexOf('UNLISTED') == -1));
Promise.all(prps).then((_) => {
	const gen_atom = require('./atomgen').gen_atom;
	gen_atom(list, config);
});
const list_dir=path.join(config.dest_dir,'list');
const ppp=config.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'));
}