//Copyright Chris Xiong 2018
//License: Expat (MIT)
/* Returns all scanned posts in an array in the following format:
* {
* file: String = file name of post (excluding the .txt extension)
* title: String = title extracted from the post
* date: String = post date extracted from the post (expected to be an ISO8601 full-date string, with optional post-status suffix)
* tags: String = comma-separated list of tags
* mdate: Number = source last modification time in msec since epoch
* needsupdate: boolean = true if post needs re-rendering
* }
*/
module.exports={
scan:function(s,d){return _scan(s,d);},
build_list_index:function(){return _build_list_index();}
};
const fs=require('fs');
const path=require('path');
const util=require('./util');
const list=[];
const tags=[];
function _scan(s,dst)
{
list.splice(0);
d=fs.readdirSync(s).reverse();
pdst=path.join(dst,'post');
let poste=true;
try{
st=fs.statSync(pdst);
if(!st.isDirectory)throw 'shit';
}catch(e){poste=false;}
for(let i of d)
if(i.endsWith('.txt'))
{
const cont=fs.readFileSync(path.join(s,i),'utf8');
contsplit=cont.split('\n');
if(contsplit.length<4)continue;
if(contsplit[1].indexOf('WIP')!=-1)continue;
list.push({
file:i.substring(0,i.length-4),
title:contsplit[0].trim(),
date:contsplit[1].trim(),
tags:contsplit[2].trim(),
mdate:fs.statSync(path.join(s,i)).mtimeMs,
needsupdate:!util.mtime_cmp(path.join(s,i),path.join(pdst,i.substring(0,i.length-4)+'.html'))
});
}
return list;
}
function _build_list_index()
{
for(let i of list)
{
if (i.date.indexOf('UNLISTED') != -1) continue;
for(let tg of i.tags.split(','))
{
if(!tags[tg])tags[tg]={posts:[],needsupdate:false};
tags[tg].posts.push(i);
tags[tg].needsupdate|=i.needsupdate;
}
}
return tags;
}