Commit 5c1477f4 authored by Jérôme Chauveau's avatar Jérôme Chauveau
Browse files

Premier jet pour la gestion des images.

parent 35527fc2
Loading
Loading
Loading
Loading
+78 −3
Changes for XML2HTMLPlugin.php: 78 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ class XML2HTMLPlugin extends GenericPlugin
        if ($success && $this->getEnabled()) {
            HookRegistry::register('Templates::Article::Main', array($this, 'xml2html'));
            HookRegistry::register('TemplateManager::display', array($this, 'xml2htmlImports'));
            HookRegistry::register('LoadHandler', array($this, 'loadImageHandler'));
        }

        return $success;
@@ -64,7 +65,10 @@ class XML2HTMLPlugin extends GenericPlugin
        $request = $this->getRequest();
        $baseUrl = $request->getBaseUrl() . '/' . $this->getPluginPath();

        //plugin's css
        $templateMgr->addStyleSheet('xml2htmlStyles', $baseUrl . '/resources/styles/article.css');

        //plugin's js
        $templateMgr->addJavaScript('xml2htmlJavascript', $baseUrl . '/resources/javascript/xml2html.js');

        return false;
@@ -76,6 +80,7 @@ class XML2HTMLPlugin extends GenericPlugin
     * @param array $args
     * @return bool
     * @brief Displays transformed JATS source as HTML on article landing page
     * @throws Exception
     */
    function xml2html(string $hookname, array $args)
    {
@@ -91,16 +96,20 @@ class XML2HTMLPlugin extends GenericPlugin
        $fileMgr = new PrivateFileManager();
        $filesDir = $fileMgr->getBasePath() . DIRECTORY_SEPARATOR;
        $xmlFilePath = NULL;
        $imgFiles = [];
        $submissionFile = null;
        while ($sf = $resultFactory->next()) {
            $fileId = $sf->getData("fileId");
            $file = Services::get('file')->get($fileId);
            $filePath = $file->path;
            if (str_ends_with($filePath, '.xml')) {
            $mimeType = $file->mimetype;
            if($mimeType == 'text/xml'){
                $xmlFilePath = $filesDir . $filePath;
                break;
                $submissionFile = $sf;
            }
        }


        if (is_null($xmlFilePath)) {
            $output .= "<div>error / todo</div>";
            return false;
@@ -119,12 +128,78 @@ class XML2HTMLPlugin extends GenericPlugin
        $proc = new XSLTProcessor;
        $proc->importStyleSheet($xsl); // attach the xsl rules

        $output .= $proc->transformToXML($xml);
        $html = $this->resolveImagePaths($proc->transformToXML($xml), $submissionFile);
        $output .= $html;

        return false;

    }

    /**
     * @param $hookName string
     * @param $args array
     * @brief Handle associated files of the full-text, only images are supported
     */
    function loadImageHandler($hookName, array $args) {
        $page = $args[0];
        $op = $args[1];

        if ($page == 'article' && $op == 'image') {
            define('HANDLER_CLASS', 'ImageDataHandler');
            import('plugins.generic.xml2html.ImageDataHandler');
        }
    }


    /**
     * @param string $htmlString
     * @param SubmissionFile $submissionFile
     * @return string
     * @brief Substitute path to attached images in html source
     */
    function resolveImagePaths(string $htmlString, SubmissionFile $submissionFile) {

            $dependentFilesIterator = Services::get('submissionFile')->getMany([
                'assocTypes' => [ASSOC_TYPE_SUBMISSION_FILE],
                'assocIds' => [$submissionFile->getId()],
                'submissionIds' => [$submissionFile->getData('submissionId')],
                'fileStages' => [SUBMISSION_FILE_DEPENDENT],
                'includeDependentFiles' => true,
            ]);

            $request = $this->getRequest();
            foreach ($dependentFilesIterator as $dependentFile) {
                $filePath = $request->url(
                    null, 'article',
                    'image',
                    array(
                        $submissionFile->getData('submissionId'),
                        //$dependentFile->getData('assocId'),
                        $dependentFile->getData('fileId')
                    )
                );


                // Solution from HtmlArticleGalleyPlugin::_getHTMLContents
                $imageFiles = [];
                $imageFileNames = array_values($dependentFile->getData('name')); // localized
                foreach ($imageFileNames as $imageFileName) {
                    if (empty($imageFileName)) continue;
                    if (array_key_exists($imageFileName, $imageFiles)) continue;
                    $imageFiles[$imageFileName] = $filePath;
                    $pattern = preg_quote(rawurlencode($imageFileName));
                    $htmlString = preg_replace(
                        '/([Ss][Rr][Cc]|[Hh][Rr][Ee][Ff]|[Dd][Aa][Tt][Aa])\s*=\s*"([^"]*' . $pattern . ')"/',
                    '\1="' . $filePath . '"',
                        $htmlString);
                }

            }

            return $htmlString;

    }


}
+42 −10
Changes for certic-jats2html.xsl: 42 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -172,7 +172,7 @@

    <!--footnote-->
    <xsl:template match="xref[@ref-type='fn']">
        <span class="fn-call">
        <span class="xref-call fn-call">
            <xsl:attribute name="data-target">
                <xsl:value-of select="@rid"/>
            </xsl:attribute>
@@ -189,6 +189,7 @@
            <xsl:apply-templates/>
        </li>
    </xsl:template>

    <xsl:template match="fn/label[text()]">
        <span class="label" title="voir">
            <xsl:apply-templates/>
@@ -200,8 +201,28 @@
        </span>
    </xsl:template>


    <!-- bibr -->
    <xsl:template match="xref[@ref-type='bibr']">
        <span class="xref-call bibr-call">
            <xsl:attribute name="data-target">
                <xsl:value-of select="@rid"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <!--references-->
    <xsl:template match="ref/mixed-citation">
    <xsl:template match="ref[mixed-citation]">
        <div class="ref">
            <xsl:attribute name="id">
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <xsl:template match="mixed-citation">
        <div class="mixed-citation">
            <xsl:apply-templates/>
        </div>
@@ -245,7 +266,7 @@


    <!--generic inline template -->
    <xsl:template match="kwd">
    <xsl:template match="kwd | underline">
        <span>
            <xsl:attribute name="class">
                <xsl:value-of select="local-name(.)"></xsl:value-of>
@@ -255,6 +276,13 @@
    </xsl:template>


    <xsl:template match="bold">
        <strong>
            <xsl:apply-templates/>
        </strong>
    </xsl:template>


    <xsl:template match="article-id">
        <div>
            <xsl:attribute name="class">
@@ -309,22 +337,25 @@

    <xsl:template match="fig">
        <figure>
            <div>
                <strong>
                    FIGURE / TODO
                </strong>
            </div>
            <div>

            <xsl:apply-templates/>
            </div>
        </figure>
    </xsl:template>

    <xsl:template match="fig//title">
        <caption>
            <xsl:apply-templates/>
        </caption>
    </xsl:template>

    <xsl:template match="graphic">
        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="@xlink:href"/>
            </xsl:attribute>
        </img>
    </xsl:template>


    <xsl:template match="title">
        <xsl:variable name="ancestors">
@@ -399,6 +430,7 @@
        </em>
    </xsl:template>


    <!-- mathml -->
    <xsl:template match="disp-formula[@content-type='math/mathml']">
        <div class="disp-formula">
+18 −9
Changes for resources/javascript/xml2html.js: 18 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -64,32 +64,41 @@ function darkMode(bool) {
}

function bindFootnotes() {
    document.querySelectorAll('.fn-call').forEach((fnc) => {
        let footnote = document.getElementById(fnc.dataset.target)
        fnc.addEventListener('click', () => {
    document.querySelectorAll('.xref-call').forEach((xrefc) => {
        let focusTarget = document.getElementById(xrefc.dataset.target)
        xrefc.addEventListener('click', () => {
            if(xrefc.classList.contains('fn-call')) {
                openTab('footnotes');
            document.getElementById(fnc.dataset.target).scrollIntoView({
            }
            else {
                openTab('refs');
            }

            document.getElementById(xrefc.dataset.target).scrollIntoView({
                block: 'center',
                behavior: "smooth"
            });
            document.querySelectorAll('.footnote').forEach(
            document.querySelectorAll('.footnote, .ref').forEach(
                (fn) => fn.classList.remove('focus')
            );
            footnote.classList.add('focus');
            focusTarget.classList.add('focus');

        });

        //click on footnote label will focus to the footnote call anchor
        let footnoteLabel = footnote.querySelector('span.label');
        if(xrefc.classList.contains('fn-call')) {
            let footnoteLabel = focusTarget.querySelector('span.label');
            footnoteLabel.addEventListener('click', () => {
            fnc.scrollIntoView({
                xrefc.scrollIntoView({
                    block: 'start',
                    behavior: "smooth"
                });
                document.querySelectorAll('.footnote').forEach(
                    (fn) => fn.classList.remove('focus')
                );
            footnote.classList.add('focus');
                focusTarget.classList.add('focus');
            })
        }

    })
}
+15 −1
Changes for resources/styles/article.css: 15 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -240,6 +240,10 @@ figure{
    margin: 25px 0px;
}

img{
    width: 100%;
}


/* tables */
table caption{
@@ -334,6 +338,11 @@ span.fn-call {
    cursor: pointer;
}

span.bibr-call{
    text-decoration: underline;
    cursor: pointer;
}

#footnotes ul {
    list-style-type: none;
}
@@ -344,7 +353,7 @@ span.fn-call {
    border-bottom: 1px solid #888;
}

#footnotes li.focus{
#footnotes li.focus, #refs .ref.focus{
    border-right: 5px solid var(--third-color);
}

@@ -429,6 +438,11 @@ span.fn-call {
    margin-left:5px;
    content: "·";
}

.underline {
    text-decoration: underline;
}

/******************************/
div.boxed-text{
    border: 1px solid var(--grey-color);
+3
Changes for templates/articleMainView.tpl: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
<div class="plop" id="plip">
	{$text}
</div>