Skip to content

Commit 077d78c

Browse files
committed
Improve image file path processing
Amend relative URLs for PDF processing
1 parent 0e6a03d commit 077d78c

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

resource/amend-uri.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the DITA Passthrough Plug-in project.
3+
* See the accompanying LICENSE file for applicable licenses.
4+
*/
5+
6+
//
7+
// Converts a value to relative path
8+
//
9+
// @param path - The relative path of the file
10+
// @param href - The relative path of the image
11+
//
12+
13+
var path = attributes.get("path");
14+
var href = attributes.get("href");
15+
16+
var hrefElements = href.split('/');
17+
var pathElements = path.split('/');
18+
var fullPath = [];
19+
20+
for each (var elem in pathElements) {
21+
if (elem !== ''){
22+
fullPath.push(elem)
23+
}
24+
}
25+
26+
27+
for each (var elem in hrefElements) {
28+
if (elem === ''){
29+
} else if (elem === '..'){
30+
fullPath.pop()
31+
} else {
32+
fullPath.push(elem)
33+
}
34+
}
35+
36+
project.setProperty(attributes.get("to"), fullPath.join('/'));

resource/antlib.xml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66
<antlib xmlns:if="ant:if" xmlns:unless="ant:unless">
77

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

19+
<!--
20+
Converts a URI to make it relative to the ditamap root
21+
22+
@param path - The relative path of the file
23+
@param href - The relative path of the image
24+
-->
25+
<scriptdef language="javascript" name="amend-uri" src="${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/amend-uri.js">
26+
<attribute name="path" />
27+
<attribute name="href" />
28+
<attribute name="to" />
29+
</scriptdef>
30+
1931

2032
<!--
2133
Individual file processing for pandoc.
@@ -124,6 +136,7 @@
124136
<actions>
125137
<local name="pandoc.image"/>
126138
<local name="pandoc.image.exists"/>
139+
<local name="pandoc.image.uri"/>
127140

128141

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

136-
137-
<echo level="error" message="${pandoc.image}"/>
138-
<echo level="error" message="${copy-image.todir}${relative.path}@{href}"/>
139-
140149
<copy taskname="pandoc.copy-image" if:set="pandoc.image" toFile="${copy-image.todir}${relative.path}/@{href}" failonerror="false" file="${pandoc.image}"/>
150+
151+
<amend-uri path="${relative.path}" href="@{href}" to="pandoc.image.uri" />
152+
<xmltask unless:set="copy-image.todir" taskname="update-uri" source="@{dest}" dest="@{dest}">
153+
<attr path="//image[@href='@{href}']" attr="href" value="${pandoc.image.uri}" />
154+
</xmltask>
155+
141156
</actions>
142157
</call>
143158
</xmltask>
File renamed without changes.

0 commit comments

Comments
 (0)