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 +++++++++++++++++++++++++++++++++++------------ generator/scanner.js | 10 ++----- generator/util.js | 19 ++++++++++++ 3 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 generator/util.js (limited to 'generator') 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(','); diff --git a/generator/scanner.js b/generator/scanner.js index 87b3f2f..52744a4 100644 --- a/generator/scanner.js +++ b/generator/scanner.js @@ -6,6 +6,7 @@ module.exports={ }; const fs=require('fs'); const path=require('path'); +const util=require('./util'); const list=[]; const tags=[]; function _scan(s,dst) @@ -22,11 +23,6 @@ function _scan(s,dst) if(i.endsWith('.txt')) { const cont=fs.readFileSync(path.join(s,i),'utf8'); - const smodt=fs.statSync(path.join(s,i)).mtimeMs; - let dmodt=0; - try{ - dmodt=fs.statSync(path.join(pdst,i.substring(0,i.length-4)+'.html')).mtimeMs; - }catch(e){}; contsplit=cont.split('\n'); if(contsplit.length<4)continue; if(contsplit[1].indexOf('WIP')!=-1)continue; @@ -35,8 +31,8 @@ function _scan(s,dst) title:contsplit[0].trim(), date:contsplit[1].trim(), tags:contsplit[2].trim(), - mdate:smodt, - needsupdate:dmodt=mtimea; +} -- cgit v1.2.3