Browse Source

added hd images feature

Bachir Soussi Chiadmi 4 years ago
parent
commit
647981c4c7
3 changed files with 67 additions and 3 deletions
  1. 7 0
      assets/css/html2print.scss
  2. 51 0
      assets/js/html2print.js
  3. 9 3
      bin/build.py

+ 7 - 0
assets/css/html2print.scss

@@ -202,3 +202,10 @@ $paper-height: $page-height + ( $edge * 2 );
      */
     -webkit-region-break-before: always;
 }
+
+
+
+img.missing-hd{
+  opacity: 0.5;
+  border: 1mm red solid;
+}

+ 51 - 0
assets/js/html2print.js

@@ -8,3 +8,54 @@ $(function() {
     $("#master-page").attr("data-width", $(".paper:first-child").width()).hide();
 
 });
+
+
+//     __  ______
+//    / / / / __ \
+//   / /_/ / / / /
+//  / __  / /_/ /
+// /_/ /_/_____/
+var len;
+var switchedLen = 0;
+
+function imageExists($elmt, src, callback) {
+  var img = new Image();
+  img.onload = function() { callback($elmt, src, true); };
+  img.onerror = function() { callback($elmt, src, false); };
+  img.src = src;
+}
+
+function switchHD($img, src, exists) {
+  // console.log('src=' + src + ', exists=' + exists);
+  if(exists){
+    $img.attr('src', src);
+  }else{
+    console.error('src '+src+' does not exists');
+    $img.addClass('missing-hd');
+  }
+  switchedLen++;
+  // console.log('len : '+len+" | switchedLen : "+switchedLen);
+
+  if(len == switchedLen)
+    console.log('HD assets enabled');
+}
+
+
+function enableHD(){
+  console.log('enabling HD assets');
+
+  len = $('img').length;
+
+  $('img').each(function(index, el) {
+    // console.log('img', el);
+    var $img = $(el);
+    var src = $img.attr('src');
+    // console.log('src BEFORE',src);
+    src = src.replace('assets', 'assets-hd');
+    src = src.replace('-preview', '');
+    // console.log('src AFTER', src);
+    // $img.attr('src', src);
+    imageExists($img, src, switchHD);
+    // console.log('--');
+  });
+}

+ 9 - 3
bin/build.py

@@ -35,9 +35,15 @@ _ERROR_PREF = '\033[1m[!!]\033[0m '
 print("Building book")
 def main():
    # clean build directory
-   if os.path.isdir(_BUILD_d):
-      shutil.rmtree(_BUILD_d, ignore_errors=True)
-   os.mkdir(_BUILD_d)
+   if not os.path.isdir(_BUILD_d):
+      # shutil.rmtree(_BUILD_d, ignore_errors=True)
+      os.mkdir(_BUILD_d)
+
+   if os.path.isdir(os.path.join(_BUILD_d, 'assets')):
+      shutil.rmtree(os.path.join(_BUILD_d, 'assets'), ignore_errors=True)
+
+   if os.path.isfile(os.path.join(_BUILD_d, 'stories.html')):
+      shutil.rmtree(os.path.join(_BUILD_d, 'stories.html'), ignore_errors=True)
 
    print('Hyphen has fr language')
    print('fr' in pyphen.LANGUAGES)