From 42b38863761f8258781406469505f0ac1c27ac54 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Tue, 12 Feb 2019 23:51:08 +0800 Subject: New feature: thumbnail generation! --- generator/postrenderer.js | 76 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 19 deletions(-) (limited to 'generator/postrenderer.js') 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["+(starting+i+1)+"]: "+s+"
"; - doc.getElementById("notediv").appendChild(span); + var span=doc.createElement('span'); + span.setAttribute('class','TText'); + span.innerHTML=`[${(starting+i+1)}]: ${s}
`; + 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. ?) - trd.getElementById('article').querySelectorAll('img').forEach( - (i)=>{if(!i.getAttribute('decoding'))i.setAttribute('decoding','async');} - ); //Tag list const tgs=meta[2].split(','); -- cgit v1.2.3