]> _ Git - cubeextranet.git/commitdiff
wip #4916 @2
authorvincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Wed, 1 Dec 2021 11:49:43 +0000 (11:49 +0000)
committervincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Wed, 1 Dec 2021 11:49:43 +0000 (11:49 +0000)
inc/ws/Util/html5/master/class.ws.html5.compiler.php
inc/ws/Util/html5/master/class.ws.html5.links.php

index 1e3d97865d9e9b1f3f436021122c9f3c47064a73..d4b4776dfa261466cb4bfb530211d508bd49167b 100644 (file)
@@ -224,6 +224,7 @@ class wsHTML5Compiler
     protected $_indexVars = null;
     public $accessibleTexts = [];
     protected $_svgSymbols = [];
+    protected $_addedPDFJS = false;
 
     protected $_docDimensions = [];
 
@@ -940,10 +941,10 @@ class wsHTML5Compiler
         $this->writeImages();
         $this->log('Images written');
         $this->writeCartConfig();
-        $this->writeArticles();
-        $this->log('Articles written');
         $linksCSS = $this->writeLinks();
         $this->log('Links written');
+        $this->writeArticles();
+        $this->log('Articles written');
         $this->writeStats();
         $this->log('Stats written');
         $this->writeLangs();
@@ -2246,6 +2247,37 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         $this->config->content_lock = $this->content_lock;
     }
 
+    public function addPDFJS($force = false)
+    {
+        if ($this->_addedPDFJS) {
+            return;
+        }
+
+        if (stripos($this->book->parametres->PDFRenderer, 'pdfjs') !== false) {
+            $renderer = $this->book->parametres->PDFRenderer;
+        } else if ($force) {
+            $renderer = 'pdfjs-legacy';
+        } else {
+            return;
+        }
+
+        $this->_addedPDFJS = true;
+
+
+        if ($renderer === 'pdfjs') {
+            $this->vdir->copyDirectory(WS_COMPILE_ASSETS . '/pdfjs', 'pdfjs');
+        } else if ($renderer === 'pdfjs-legacy') {
+            $this->vdir->copyDirectory(WS_COMPILE_ASSETS . '/pdfjs-legacy', 'pdfjs');
+        }
+
+        $css = '.article #sidebarContainer, .article .toolbar {display:none !important;}';
+        $css .= '.article .pdfViewer{padding:0 !important;}';
+        $css .= '.article #viewerContainer{top:0 !important;overflow:visible !important;}';
+        $css .= '.article{--page-border:0;--page-margin:0;--body-bg-color:transparent;}';
+        $css .= '.openFile,.rotateCw,.rotateCcw,.rotateCcw + .horizontalToolbarSeparator{display:none !important;}' . $this->book->parametres->PDFJSCSS;
+        $this->vdir->file_put_contents('pdfjs/web/viewer.css', file_get_contents(WS_COMPILE_ASSETS . '/' . $renderer . '/web/viewer.css') . $css);
+    }
+
     protected function writeJs()
     {
         $this->beforeWriteConfig();
@@ -2256,16 +2288,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
 
         $finals = $this->jsLibs;
 
-        if ($this->book->parametres->PDFRenderer === 'pdfjs') {
-            $this->vdir->copyDirectory(WS_COMPILE_ASSETS . '/pdfjs', 'pdfjs');
-        } else if ($this->book->parametres->PDFRenderer === 'pdfjs-legacy') {
-            $this->vdir->copyDirectory(WS_COMPILE_ASSETS . '/pdfjs-legacy', 'pdfjs');
-        }
-
-        if (stripos($this->book->parametres->PDFRenderer, 'pdfjs') !== false) {
-            $css = '.openFile,.rotateCw,.rotateCcw,.rotateCcw + .horizontalToolbarSeparator{display:none !important;}' . $this->book->parametres->PDFJSCSS;
-            $this->vdir->file_put_contents('pdfjs/web/viewer.css', file_get_contents(WS_COMPILE_ASSETS . '/' . $this->book->parametres->PDFRenderer . '/web/viewer.css') . $css);
-        }
+        $this->addPDFJS();
 
         if ($this->book->parametres->scorm_enable) {
             $finals['scorm'] = array();
@@ -3231,131 +3254,125 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         $this->lessVariables['articles-font'] = 'OpenSans';
 
         $list = [];
-
-        $f = $this->book->parametres->articlesFile;
-        if ($f === '') {
-            $this->config->articlesList = $list;
-            return;
-        }
-        $f = $this->wdir . '/' . $f;
-        if (!file_exists($f)) {
-            $this->config->articlesList = $list;
-            return;
+        if (isset($this->config->articlesList)) {
+            $list = $this->config->articlesList;
         }
 
-        $this->addLess('articles');
-        if ($this->book->parametres->articlesStyle !== 'default') {
-            $this->lessVariables['articles-styles'] = $this->book->parametres->articlesStyle;
-        }
+        $f = $this->book->parametres->articlesFile;
+        if ($f !== '' && file_exists($f = $this->wdir . '/' . $f)) {
+            $this->addLess('articles');
+            if ($this->book->parametres->articlesStyle !== 'default') {
+                $this->lessVariables['articles-styles'] = $this->book->parametres->articlesStyle;
+            }
 
-        $this->lessVariables['articles-title-color'] = '#565657';
-        $this->lessVariables['articles-font'] = $mapFonts[$this->book->parametres->articlesFont] ?? $this->book->parametres->articlesFont;
-        $fontPath = $this->addFontKit($this->book->parametres->articlesFont);
+            $this->lessVariables['articles-title-color'] = '#565657';
+            $this->lessVariables['articles-font'] = $mapFonts[$this->book->parametres->articlesFont] ?? $this->book->parametres->articlesFont;
+            $fontPath = $this->addFontKit($this->book->parametres->articlesFont);
 
-        $svg = '<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><symbol id="nav-print" viewBox="0 0 512 512">
+            $svg = '<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><symbol id="nav-print" viewBox="0 0 512 512">
         <path d="m424 186l-39 0 0-114c0-9-6-15-14-15l-230 0c-8 0-14 6-14 15l0 114-39 0c-22 0-41 19-41 41l0 121c0 23 19 41 41 41l39 0 0 49c0 8 6 15 14 15l230 0c8 0 14-7 14-15l0-49 39 0c22 0 41-18 41-41l0-121c0-22-19-41-41-41z m-268-100l200 0 0 100-200 0z m200 340l-200 0 0-88 200 0z m80-76c0 6-6 12-12 12l-39 0 0-38c0-9-6-15-14-15l-230 0c-8 0-14 6-14 15l0 38-39 0c-6 0-12-6-12-12l0-121c0-6 6-12 12-12l336 0c6 0 12 6 12 12z m-278-96l-33 0c-8 0-14 6-14 14 0 8 6 15 14 15l35 0c8 0 14-7 14-15 0-8-8-14-16-14z m32 139l132 0c8 0 14-6 14-14 0-8-6-14-14-14l-132 0c-8 0-14 6-14 14 0 8 6 14 14 14z"/>
     </symbol></svg>';
 
 
-        $x = simplexml_load_string(file_get_contents($f));
-        $prevurl = '';
-        foreach ($x->xpath('/articles/article') as $k => $a) {
-            $dir = isset($a['dir']) ? (string)$a['dir'] : null;
-            $url = (string)$a['url'];
-            $id = (string)$a['id'];
-            $color = (string)$a['color'];
-            if (!$color) {
-                $color = '#000';
-            }
+            $x = simplexml_load_string(file_get_contents($f));
+            foreach ($x->xpath('/articles/article') as $k => $a) {
+                $dir = isset($a['dir']) ? (string)$a['dir'] : null;
+                $url = (string)$a['url'];
+                $id = (string)$a['id'];
+                $color = (string)$a['color'];
+                if (!$color) {
+                    $color = '#000';
+                }
 
-            $specificStyles = '## h3, ## figure figcaption{background-color:' . $color . '}';
-            $specificStyles .= '## .chapo, ## blockquote, ## a{color:' . $color . ';}';
+                $specificStyles = '## h3, ## figure figcaption{background-color:' . $color . '}';
+                $specificStyles .= '## .chapo, ## blockquote, ## a{color:' . $color . ';}';
 
-            $inner = '<article data-id="$id" class="menu-article" id="article_$id"';
-            if (null !== $dir) {
-                $inner .= ' dir="' . $dir . '"';
-            }
-            $inner .= '>';
-            $inner .= '<style type="text/css">' . str_replace('##', '#article_$id', $specificStyles) . '</style>';
-            $inner .= '<div class="actions">';
-            if ($this->book->parametres->articlesShare && $this->book->parametres->share) {
-                $inner .= '<a data-id="$id" data-url="$url" href="#" class="articlesShare"><svg viewBox="0 0 512 512" class="nav-share nav-icon svg-icon"><use xlink:href="#nav-share"></use></svg></a>';
-            }
-            $inner .= '<a href="#" class="articlesPrint"><svg viewBox="0 0 512 512" class="nav-print nav-icon svg-icon"><use xlink:href="#nav-print"></use></svg></a>';
-            $inner .= '</div>';
+                $inner = '<article data-id="$id" class="menu-article" id="article_$id"';
+                if (null !== $dir) {
+                    $inner .= ' dir="' . $dir . '"';
+                }
+                $inner .= '>';
+                $inner .= '<style type="text/css">' . str_replace('##', '#article_$id', $specificStyles) . '</style>';
+                $inner .= '<div class="actions">';
+                if ($this->book->parametres->articlesShare && $this->book->parametres->share) {
+                    $inner .= '<a data-id="$id" data-url="$url" href="#" class="articlesShare"><svg viewBox="0 0 512 512" class="nav-share nav-icon svg-icon"><use xlink:href="#nav-share"></use></svg></a>';
+                }
+                $inner .= '<a href="#" class="articlesPrint"><svg viewBox="0 0 512 512" class="nav-print nav-icon svg-icon"><use xlink:href="#nav-print"></use></svg></a>';
+                $inner .= '</div>';
 
-            $inner .= '<div class="articleBody">';
+                $inner .= '<div class="articleBody">';
 
-            $title = '';
-            $lead = '';
-            $image = '';
+                $title = '';
+                $lead = '';
+                $image = '';
 
-            $first = true;
+                $first = true;
 
-            foreach ($a->children() as $child) {
-                if ($first) {
-                    $first = false;
-                    if ($child->getName() !== 'category') {
-                        $inner .= '<h3>&nbsp;</h3>';
+                foreach ($a->children() as $child) {
+                    if ($first) {
+                        $first = false;
+                        if ($child->getName() !== 'category') {
+                            $inner .= '<h3>&nbsp;</h3>';
+                        }
                     }
+                    $inner .= $this->_articleToHTML($child, $title, $lead, $image, $dir);
                 }
-                $inner .= $this->_articleToHTML($child, $title, $lead, $image, $dir);
-            }
-            $inner .= '</div></article>';
+                $inner .= '</div></article>';
 
-            if (!$title) {
-                $title = 'Article sans titre ' . $k;
-            }
+                if (!$title) {
+                    $title = 'Article sans titre ' . $k;
+                }
 
-            if (!$id) {
-                $id = CubeIT_Text::str2URL($title);
-            }
+                if (!$id) {
+                    $id = CubeIT_Text::str2URL($title);
+                }
 
-            if (!$url) {
-                $url = $id . '.html';
-            }
+                if (!$url) {
+                    $url = $id . '.html';
+                }
 
-            $inner = str_replace(array('$id', '$url'), array($id, $url), $inner);
+                $inner = str_replace(array('$id', '$url'), array($id, $url), $inner);
 
-            $article = ['id' => $id,
-                'url' => $url,
-                'color' => $color,
-                'contents' => '',
-                'prev' => $prevurl,
-                'next' => ''];
+                $article = ['id' => $id,
+                    'url' => $url,
+                    'color' => $color,
+                    'contents' => '',
+                    'type' => 'xml'];
 
-            if ($prevurl !== '') {
-                $list[count($list) - 1]['next'] = $url;
-            } else {
-                $firsturl = $url;
-            }
+                $article['contents'] = $inner;
+                $content = '<html><head>';
+                $content .= '<link rel="stylesheet" type="text/css" href="' . $fontPath . '">';
+                $content .= '<link rel="stylesheet" type="text/css" href="style/articles.css">';
+                $content .= '<style type="text/css">';
+                $content .= str_replace('## ', '', $specificStyles);
+                $content .= '</style>';
+                $content .= '<style type="text/css" media="screen">*{visibility:hidden}</style>';
+                $content .= '</head><body>';
+                $content .= $svg;
+                $content .= $inner;
+                $content .= '</body></html>';
+                $article['print'] = $content;
+                $list[] = $article;
 
-            $prevurl = $url;
+                $this->addSEOArticle('#/article/' . $article['url'], $title, $lead, $image, $article['id'], $article['url'], $inner);
+            }
+        }
 
-            $article['contents'] = $inner;
-            $content = '<html><head>';
-            $content .= '<link rel="stylesheet" type="text/css" href="' . $fontPath . '">';
-            $content .= '<link rel="stylesheet" type="text/css" href="style/articles.css">';
-            $content .= '<style type="text/css">';
-            $content .= str_replace('## ', '', $specificStyles);
-            $content .= '</style>';
-            $content .= '<style type="text/css" media="screen">*{visibility:hidden}</style>';
-            $content .= '</head><body>';
-            $content .= $svg;
-            $content .= $inner;
-            $content .= '</body></html>';
-            $article['print'] = $content;
-            $list[] = $article;
 
-            $this->addSEOArticle('#/article/' . $article['url'], $title, $lead, $image, $article['id'], $article['url'], $inner);
+        $nb = count($list);
+        foreach ($list as $k => $item) {
+            $nextIndex = ($k + 1) % $nb;
+            $prevIndex = ($k - 1 + $nb) % $nb;
+            $list[$k]['prev'] = $list[$prevIndex]['url'];
+            $list[$k]['next'] = $list[$nextIndex]['url'];
         }
-        $list[0]['prev'] = $prevurl;
-        $list[count($list) - 1]['next'] = $firsturl;
+
 
         $idlist = [];
         foreach ($list as $item) {
             $idlist[$item['id']] = $item;
         }
+
         $this->config->articlesList = $idlist;
     }
 
index 48d4e2af62ad5fea0bd49c89037c530d292a24e3..d01523392fa750192a2e9a8feaabb00a17dfe589 100644 (file)
@@ -76,6 +76,8 @@ class wsHTML5Link
         $init = wsLinks::decryptLink($init);
         $init = CubeIT_Util_Array::asArray($init);
 
+        $wdir = WS_BOOKS . '/' . $id . '/working/';
+
         $init['scorm'] = self::isScorm($init);
         $init['to'] = self::replaceCustomURL($init['to']);
 
@@ -257,7 +259,11 @@ class wsHTML5Link
             case 35:
                 return new textLink($id, $init, $compiler);
             case 36:
-                return new articleLink($id, $init, $compiler);
+                if (stripos($init['to'], '.pdf') !== false) {
+                    return new articlePDFLink($id, $init, $compiler);
+                } else {
+                    return new articleLink($id, $init, $compiler);
+                }
             case 37:
                 return new downloadPortionLink($id, $init, $compiler);
             case 38:
@@ -1013,7 +1019,7 @@ class htmlMultimediaPopupImage extends normalLink
         if ($this->read_mode) {
             $read = ' data-readmode="1"';
         }
-        return $res . ' ' . $read . ' data-md5="'.md5($this->alternative).'" data-multimedia="' . rawurlencode($markup) . '" ';
+        return $res . ' ' . $read . ' data-md5="' . md5($this->alternative) . '" data-multimedia="' . rawurlencode($markup) . '" ';
     }
 
     public function keep()
@@ -3052,6 +3058,44 @@ class iframeLink extends wsHTML5Link
 
 }
 
+class articlePDFLink extends articleLink
+{
+    public $title = '';
+    public $description = '';
+
+    public function init()
+    {
+        $this->compiler->addPDFJS(true);
+
+        $e = explode('.', $this->to);
+        array_pop($e);
+        $name = implode('.', $e);
+        $pdffile = $this->wdir . $this->to;
+
+        $htmlfile = $this->wdir . $name . '.html';
+        if (!file_exists($htmlfile)) {
+            $cmd = "pdftotext -f 1 -l 1 -htmlmeta $pdffile $htmlfile";
+            `$cmd`;
+        }
+        $infosfile = $this->wdir . $name . '.nfo';
+        if (!file_exists($infosfile)) {
+            $i = `pdfinfo -f 1 -l 1 -box $pdffile`;
+            if (preg_match('/Page\s+1\s+size:\s+([0-9\.]+) x ([0-9\.]+)/', $i, $matches)) {
+                $infos = ['width' => $matches[1], 'height' => $matches[2]];
+                file_put_contents($infosfile, json_encode($infos));
+            }
+        } else {
+            $infos = json_decode(file_get_contents($infosfile));
+        }
+
+        $this->copyExternalFile($this->to);
+        $text = file_get_contents($htmlfile);
+        $this->article = ['page' => $this->page, 'type' => 'pdf', 'id' => $name, 'url' => $name, 'infos' => $infos, 'content' => $this->to, 'textcontent' => $text];
+        $this->compiler->config->articlesList[] = $this->article;
+        $this->compiler->addSEOArticle('#/article/' . $name, $this->title, $this->description, '', $name, $name . '.html', $text);
+    }
+}
+
 class articleLink extends normalLink
 {
     protected $article;
@@ -3059,6 +3103,8 @@ class articleLink extends normalLink
     public function init()
     {
         parent::init();
+
+
         $this->article = $this->compiler->config->articlesList[$this->to];
         if (!isset($this->compiler->config->articlesList[$this->to]['page'])) {
             $this->compiler->config->articlesList[$this->to]['page'] = $this->page;