Skip to content

Commit

Permalink
Improve image file path processing
Browse files Browse the repository at this point in the history
Amend relative URLs for PDF processing
  • Loading branch information
jason-fox committed Oct 13, 2019
1 parent 0e6a03d commit 077d78c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
36 changes: 36 additions & 0 deletions resource/amend-uri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of the DITA Passthrough Plug-in project.
* See the accompanying LICENSE file for applicable licenses.
*/

//
// Converts a value to relative path
//
// @param path - The relative path of the file
// @param href - The relative path of the image
//

var path = attributes.get("path");
var href = attributes.get("href");

var hrefElements = href.split('/');
var pathElements = path.split('/');
var fullPath = [];

for each (var elem in pathElements) {
if (elem !== ''){
fullPath.push(elem)
}
}


for each (var elem in hrefElements) {
if (elem === ''){
} else if (elem === '..'){
fullPath.pop()
} else {
fullPath.push(elem)
}
}

project.setProperty(attributes.get("to"), fullPath.join('/'));
29 changes: 22 additions & 7 deletions resource/antlib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
<antlib xmlns:if="ant:if" xmlns:unless="ant:unless">

<!--
Converts a value to result as generated by unix machines
Returns the relative path of the src file
@param string - The value to convert
@param src - The value to convert
@param to - The property to set
-->
<scriptdef language="javascript" name="relative-path" src="${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/relative.js">
<scriptdef language="javascript" name="relative-path" src="${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/relative-path.js">
<attribute name="src" />
<attribute name="to" />
</scriptdef>

<!--
Converts a URI to make it relative to the ditamap root
@param path - The relative path of the file
@param href - The relative path of the image
-->
<scriptdef language="javascript" name="amend-uri" src="${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/amend-uri.js">
<attribute name="path" />
<attribute name="href" />
<attribute name="to" />
</scriptdef>


<!--
Individual file processing for pandoc.
Expand Down Expand Up @@ -124,6 +136,7 @@
<actions>
<local name="pandoc.image"/>
<local name="pandoc.image.exists"/>
<local name="pandoc.image.uri"/>


<condition property="pandoc.image" value="${src.dir}/@{href}">
Expand All @@ -133,11 +146,13 @@
</and>
</condition>


<echo level="error" message="${pandoc.image}"/>
<echo level="error" message="${copy-image.todir}${relative.path}@{href}"/>

<copy taskname="pandoc.copy-image" if:set="pandoc.image" toFile="${copy-image.todir}${relative.path}/@{href}" failonerror="false" file="${pandoc.image}"/>

<amend-uri path="${relative.path}" href="@{href}" to="pandoc.image.uri" />
<xmltask unless:set="copy-image.todir" taskname="update-uri" source="@{dest}" dest="@{dest}">
<attr path="//image[@href='@{href}']" attr="href" value="${pandoc.image.uri}" />
</xmltask>

</actions>
</call>
</xmltask>
Expand Down
File renamed without changes.

0 comments on commit 077d78c

Please sign in to comment.