aboutsummaryrefslogtreecommitdiff
path: root/generator/postrenderer.js
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2019-02-12 23:51:08 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2019-02-12 23:51:08 +0800
commit42b38863761f8258781406469505f0ac1c27ac54 (patch)
treea493976206cd043041e89688d5bbd31ced1ad565 /generator/postrenderer.js
parentedd8aa6e058d12dd0f9b0b886e507bd73dce72cb (diff)
downloadsbs-42b38863761f8258781406469505f0ac1c27ac54.tar.xz
New feature: thumbnail generation!
Diffstat (limited to 'generator/postrenderer.js')
-rw-r--r--generator/postrenderer.js76
1 files changed, 57 insertions, 19 deletions
diff --git a/generator/postrenderer.js b/generator/postrenderer.js
index e387691..f3cf46b 100644
--- a/generator/postrenderer.js
+++ b/generator/postrenderer.js
@@ -12,6 +12,11 @@ const scrypt=require('scrypt-js');
const btoa=require('btoa');
const sha256=require('./sha256').sha256;
const spawn=require('child_process').spawn;
+const util=require('./util');
+
+const THUMB_IMAGE_URL='//filestorage.chrisoft.org/blog/img/';
+const THUMB_LOCAL_DIR='../content/img/';
+
let template_file='';
let tmplcont='';
let tocid;
@@ -43,26 +48,58 @@ function _dfs(doc,el,le,p)
}
function footnoter(doc)
{
- var footnotes=doc.body.getElementsByTagName("footnote");
- let starting=doc.getElementById("notediv").children.length;
+ var footnotes=doc.body.getElementsByTagName('footnote');
+ let starting=doc.getElementById('notediv').children.length;
for(var i=0;i<footnotes.length;++i)
{
var s=footnotes[i].innerHTML;
- footnotes[i].innerHTML="";
- var a=doc.createElement("a");
- a.setAttribute("id","n"+(starting+i+1));
- a.setAttribute("href","#note"+(starting+i+1));
- a.setAttribute("class","note");
- a.innerHTML="["+(starting+i+1)+"]";
+ footnotes[i].innerHTML='';
+ var a=doc.createElement('a');
+ a.setAttribute('id',`n${(starting+i+1)}`);
+ a.setAttribute('href',`#note${(starting+i+1)}`);
+ a.setAttribute('class','note');
+ a.innerHTML=`[${(starting+i+1)}]`;
footnotes[i].parentNode.insertBefore(a,footnotes[i]);
- var span=doc.createElement("span");
- span.setAttribute("class","TText");
- span.innerHTML="<a id=\"note"+(starting+i+1)+"\" href=\"#n"+(starting+i+1)+"\">["+(starting+i+1)+"]</a>: "+s+"<br>";
- doc.getElementById("notediv").appendChild(span);
+ var span=doc.createElement('span');
+ span.setAttribute('class','TText');
+ span.innerHTML=`<a id="note${(starting+i+1)}" href="#n${(starting+i+1)}">[${(starting+i+1)}]</a>: ${s}<br>`;
+ doc.getElementById('notediv').appendChild(span);
}
starting+=footnotes.length;
while(footnotes.length)footnotes[0].remove();
}
+function gen_thumb(doc,el)
+{
+ const a=doc.createElement('a');
+ const im=doc.createElement('img');
+ const srcfn=el.innerHTML;
+ let w=el.hasAttribute('width')?Number(el.getAttribute('width')):-1;
+ let h=el.hasAttribute('height')?Number(el.getAttribute('height')):-1;
+ let p=el.hasAttribute('scale')?Number(el.getAttribute('scale')):-1;
+ if(el.hasAttribute('iw'))
+ im.style.width=el.getAttribute('iw');
+ if(el.hasAttribute('ih'))
+ im.style.height=el.getAttribute('ih');
+ if(p>0)w=h=-1;
+ if(w>0||h>0)
+ {
+ if(w<=0)w=h;
+ else if(h<=0)h=w;
+ }
+ const dstfn=`ssbsthumb_${p>0?'s'+p.toFixed(2):w+'x'+h}_${srcfn}`
+ const srcf=path.join(THUMB_LOCAL_DIR,srcfn);
+ const dstf=path.join(THUMB_LOCAL_DIR,dstfn);
+ if(!util.mtime_cmp(srcf,dstf))
+ {
+ const m=spawn('magick',[srcf,'-resize',p>0?(p*100).toFixed()+'%':w+'x'+h,dstf]);
+ m.stdout.on('data',(d)=>{console.log(`IMout:${d}`);});
+ m.stderr.on('data',(d)=>{console.log(`IMerr:${d}`);});
+ }
+ a.setAttribute('href',THUMB_IMAGE_URL+srcfn);
+ im.src=THUMB_IMAGE_URL+dstfn;
+ a.appendChild(im);
+ el.parentNode.replaceChild(a,el);
+}
async function encrypt(doc)
{
let enid=0;
@@ -128,6 +165,14 @@ async function _render(inf,outf,np,pp)
}
else
trd.getElementById('article').innerHTML=contsplit.join('\n');
+
+ //async images
+ trd.getElementById('article').querySelectorAll('img').forEach(
+ (i)=>{if(!i.getAttribute('decoding'))i.setAttribute('decoding','async');}
+ );
+
+ //thumbnails generation
+ trd.getElementById('article').querySelectorAll('thumb').forEach((el)=>{gen_thumb(trd,el);});
//Encryption
await encrypt(trd);
@@ -144,13 +189,6 @@ async function _render(inf,outf,np,pp)
//Footnotes
footnoter(trd);
-
- //async images
- //TODO: Actually compress the images using imagemagick
- //(by introducing a new tag e.g. <thumb> ?)
- trd.getElementById('article').querySelectorAll('img').forEach(
- (i)=>{if(!i.getAttribute('decoding'))i.setAttribute('decoding','async');}
- );
//Tag list
const tgs=meta[2].split(',');