From 32d1d6642ffd38fa69ae9009da0e4d86819284d5 Mon Sep 17 00:00:00 2001 From: v Date: Thu, 15 Nov 2018 17:39:35 +0300 Subject: [PATCH] process inline styles --- wikizimmer.js | 214 +++++++++++++++++++++++++++++--------------------- 1 file changed, 126 insertions(+), 88 deletions(-) diff --git a/wikizimmer.js b/wikizimmer.js index d42ae28..8e1e0ec 100755 --- a/wikizimmer.js +++ b/wikizimmer.js @@ -334,12 +334,11 @@ class WikiItem { Object.assign( this, { zimNameSpace, url, title }) } - data () { - return ( this.data_ !== undefined ? Promise.resolve( this.data_ ) : ( this.data_ = this.load( ))) - .then( data => ! Buffer.isBuffer( data ) || this.encoding == null - ? data - : iconv.decode( data, this.encoding ) - ) + async getData () { + let data = await ( this.data !== undefined ? Promise.resolve( this.data ) : ( this.data = this.load( ))) + if ( Buffer.isBuffer( data ) && this.encoding != null ) + data = iconv.decode( data, this.encoding ) + return data } urlReplacements () { @@ -412,6 +411,10 @@ class WikiItem { return '/' + this.zimNameSpace + '/' + this.baseName() } + rootPath () { + return '../'.repeat( this.baseName().split( '/' ).length - 1 ) + } + urlKey () { return this.zimNameSpace + this.baseName() } @@ -463,7 +466,7 @@ class WikiItem { process () { return Promise.resolve() - .then( () => this.data()) + .then( () => this.getData()) .then( data => this.store( data )) .then( () => this.storeMetadata() ) .then( () => this.localPath() ) @@ -513,7 +516,6 @@ class ArticleStub extends WikiItem { class Article extends ArticleStub { constructor ( pageInfo ) { super( pageInfo ) - this.basePath = '../'.repeat( this.baseName().split( '/' ).length - 1 ) } load () { @@ -521,7 +523,7 @@ class Article extends ArticleStub { .then( body => this.preProcess( body )) } - preProcess( data, reply ) { + async preProcess( data, reply ) { let src try { src = cheerio.load( data ) @@ -529,56 +531,61 @@ class Article extends ArticleStub { log( 'cheerio.load error', e, data, reply ) return data } - let content = src( '#bodyContent' ) - if ( content.length == 0 ) { - content = src( 'article' ) - } - if ( content.length == 0 ) { - fatal( "Article.preProcess -- fatal error: Can't find article's content:", this.title ) - } - - const dom = cheerio.load( wiki.pageTemplate ) - dom( 'title' ).text( this.title ) - - dom( '#bodyContent' ).replaceWith( content ) - - // remove comments - dom( '*' ).contents().each( (i, elem) => { - //~ log( 'comment', elem.type ) - if ( elem.type === 'comment' ) { - dom( elem ).remove() + try { + let content = src( '#bodyContent' ) + if ( content.length == 0 ) { + content = src( 'article' ) + } + if ( content.length == 0 ) { + fatal( "Article.preProcess -- fatal error: Can't find article's content:", this.title ) } - }) - // display content inside