Skip to content

Commit 0e6a03d

Browse files
committed
Improve image file path processing
1 parent 2781e1c commit 0e6a03d

File tree

7 files changed

+185
-150
lines changed

7 files changed

+185
-150
lines changed

process_pandoc.xml

Lines changed: 4 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,6 @@
33
See the accompanying LICENSE file for applicable licenses.
44
-->
55
<project name="fox.jason.passthrough.pandoc" xmlns:if="ant:if" xmlns:unless="ant:unless">
6-
7-
<!--
8-
Individual file processing for pandoc.
9-
Take an input format run pandoc against it and convert to DITA
10-
11-
@param src - The input file to convert to DITA
12-
@param dest - The output file location
13-
@param title - The title of the converted file
14-
@param metadata - The metadata of the converted file
15-
-->
16-
<macrodef name="add-pandoc-file">
17-
<attribute name="src" />
18-
<attribute name="dest" />
19-
<attribute name="title" />
20-
<attribute name="metadata"/>
21-
<sequential>
22-
<local name="pandoc.result"/>
23-
<local name="pandoc.error"/>
24-
<local name="pandoc.metadata"/>
25-
<local name="metadata.txt"/>
26-
27-
<echo if:set="pandoc.installed" taskname="pandoc" level="info" message="Processing @{src}" />
28-
29-
<tempfile deleteonexit="true" destdir="${dita.temp.dir}"
30-
property="metadata.txt" suffix="txt"/>
31-
<xslt unless:blank="@{metadata}" in="@{metadata}" out="${metadata.txt}"
32-
style="${dita.plugin.fox.jason.passthrough.pandoc.dir}/xsl/topicmeta.xsl"
33-
taskname="topic-meta"/>
34-
<loadfile unless:blank="@{metadata}" property="pandoc.metadata" srcfile="${metadata.txt}" failonerror="false"/>
35-
<property if:blank="@{metadata}" name="pandoc.metadata" value=""/>
36-
37-
<!-- For Unix run Pandoc as an executable -->
38-
<exec executable="pandoc" dir="${dita.temp.dir}" osfamily="unix" taskname="pandoc" errorproperty="pandoc.error" resultproperty="pandoc.result" >
39-
<arg line="&quot;@{src}&quot; -t ${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/topic.lua -o &quot;@{dest}&quot; -M title=&quot;@{title}&quot; ${pandoc.metadata}"/>
40-
</exec>
41-
<!-- For Windows run from a DOS command -->
42-
<exec if:set="pandoc.installed" executable="cmd" dir="${dita.temp.dir}" osfamily="windows" taskname="pandoc" errorproperty="pandoc.error" resultproperty="pandoc.result">
43-
<arg value="/C"/>
44-
<arg value="pandoc &quot;@{src}&quot; -t ${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/topic.lua -o &quot;@{dest}&quot; -M title=&quot;@{title}&quot; ${pandoc.metadata}"/>
45-
</exec>
46-
47-
<process-pandoc-errors src="@{src}" result="${pandoc.result}" message="${pandoc.error}"/>
48-
<process-pandoc-images src="@{src}" dest="@{dest}"/>
49-
<!--copy file="@{dest}" tofile="${output.dir}/test.xml"/-->
50-
</sequential>
51-
</macrodef>
52-
53-
<!--
54-
Flag up any error messages created during file processing
55-
56-
@param src - The input file to convert to DITA
57-
@param result - Whether Pandoc succeeded or failed
58-
@param message - err.out from Pandoc
59-
-->
60-
<macrodef name="process-pandoc-errors">
61-
<attribute name="result" />
62-
<attribute name="message" />
63-
<attribute name="src" />
64-
<sequential>
65-
<local name="pandoc.isError"/>
66-
<local name="pandoc.message.code"/>
67-
68-
<condition property="pandoc.isError">
69-
<not>
70-
<equals arg1="@{result}" arg2="0"/>
71-
</not>
72-
</condition>
73-
74-
<echo taskname="pandoc" if:set="pandoc.isError" level="error" message="@{message}"/>
75-
76-
77-
<dita-ot-fail id="PAND003F" if:set="pandoc.isError">
78-
<param name="1" value="@{src}"/>
79-
</dita-ot-fail>
80-
81-
<condition property="pandoc.message.code" value="PAND002W">
82-
<contains string="@{message}" substring="Multiple Root Nodes detected."/>
83-
</condition>
84-
85-
<dita-ot-echo if:set="pandoc.message.code" id="${pandoc.message.code}">
86-
<param name="1" value="@{src}"/>
87-
</dita-ot-echo>
88-
</sequential>
89-
</macrodef>
90-
91-
92-
<!--
93-
Ensure that any images associated with the coverted file are copied to
94-
the output
95-
96-
@param src - The input file to convert to DITA
97-
@param dest - The output file location
98-
-->
99-
<macrodef name="process-pandoc-images">
100-
<attribute name="src" />
101-
<attribute name="dest" />
102-
<sequential>
103-
<local name="src.dir"/>
104-
<dirname property="src.dir" file="@{src}"/>
105-
106-
<xmltask taskname="pandoc.read-images" source="@{dest}">
107-
<call path="//image[not(contains(@href, ':'))]">
108-
<param default="" name="href" path="@href" />
109-
<actions>
110-
<local name="pandoc.image"/>
111-
<condition property="pandoc.image">
112-
<and>
113-
<available file="${src.dir}/@{href}"/>
114-
<not>
115-
<available file="{copy-image.todir}/@{href}"/>
116-
</not>
117-
</and>
118-
</condition>
119-
<copy taskname="pandoc.copy-image" if:set="pandoc.image" toFile="${copy-image.todir}/@{href}" failonerror="false" file="${src.dir}/@{href}"/>
120-
</actions>
121-
</call>
122-
</xmltask>
123-
</sequential>
124-
</macrodef>
125-
1266
<!--
1277
Setting up Pandoc processing. Check that Pandoc is installed
1288
-->
@@ -145,6 +25,10 @@
14525

14626
<dita-ot-echo unless:set="pandoc.installed" id="PAND001W"/>
14727
<echo if:set="pandoc.installed" taskname="pandoc" level="info" message="${pandoc.out}"/>
28+
29+
<typedef file="${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/antlib.xml"/>
30+
<dirname property="args.input.dir" file="${args.input}"/>
31+
14832
</target>
14933

15034
<!--

resource/antlib.xml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--ant-->
2+
<!--
3+
This file is part of the DITA-OT Passthrough Pandoc Plug-in project.
4+
See the accompanying LICENSE file for applicable licenses.
5+
-->
6+
<antlib xmlns:if="ant:if" xmlns:unless="ant:unless">
7+
8+
<!--
9+
Converts a value to result as generated by unix machines
10+
11+
@param string - The value to convert
12+
@param to - The property to set
13+
-->
14+
<scriptdef language="javascript" name="relative-path" src="${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/relative.js">
15+
<attribute name="src" />
16+
<attribute name="to" />
17+
</scriptdef>
18+
19+
20+
<!--
21+
Individual file processing for pandoc.
22+
Take an input format run pandoc against it and convert to DITA
23+
24+
@param src - The input file to convert to DITA
25+
@param dest - The output file location
26+
@param title - The title of the converted file
27+
@param metadata - The metadata of the converted file
28+
-->
29+
<macrodef name="add-pandoc-file">
30+
<attribute name="src" />
31+
<attribute name="dest" />
32+
<attribute name="title" />
33+
<attribute name="metadata"/>
34+
<sequential>
35+
<local name="pandoc.result"/>
36+
<local name="pandoc.error"/>
37+
<local name="pandoc.metadata"/>
38+
<local name="metadata.txt"/>
39+
40+
<echo if:set="pandoc.installed" taskname="pandoc" level="info" message="Processing @{src}" />
41+
42+
<tempfile deleteonexit="true" destdir="${dita.temp.dir}"
43+
property="metadata.txt" suffix="txt"/>
44+
<xslt unless:blank="@{metadata}" in="@{metadata}" out="${metadata.txt}"
45+
style="${dita.plugin.fox.jason.passthrough.pandoc.dir}/xsl/topicmeta.xsl"
46+
taskname="topic-meta"/>
47+
<loadfile unless:blank="@{metadata}" property="pandoc.metadata" srcfile="${metadata.txt}" failonerror="false"/>
48+
<property if:blank="@{metadata}" name="pandoc.metadata" value=""/>
49+
50+
<!-- For Unix run Pandoc as an executable -->
51+
<exec executable="pandoc" dir="${dita.temp.dir}" osfamily="unix" taskname="pandoc" errorproperty="pandoc.error" resultproperty="pandoc.result" >
52+
<arg line="&quot;@{src}&quot; -t ${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/topic.lua -o &quot;@{dest}&quot; -M title=&quot;@{title}&quot; ${pandoc.metadata}"/>
53+
</exec>
54+
<!-- For Windows run from a DOS command -->
55+
<exec if:set="pandoc.installed" executable="cmd" dir="${dita.temp.dir}" osfamily="windows" taskname="pandoc" errorproperty="pandoc.error" resultproperty="pandoc.result">
56+
<arg value="/C"/>
57+
<arg value="pandoc &quot;@{src}&quot; -t ${dita.plugin.fox.jason.passthrough.pandoc.dir}/resource/topic.lua -o &quot;@{dest}&quot; -M title=&quot;@{title}&quot; ${pandoc.metadata}"/>
58+
</exec>
59+
60+
<process-pandoc-errors src="@{src}" result="${pandoc.result}" message="${pandoc.error}"/>
61+
<process-pandoc-images src="@{src}" dest="@{dest}"/>
62+
<!--copy file="@{dest}" tofile="${output.dir}/test.xml"/-->
63+
</sequential>
64+
</macrodef>
65+
66+
<!--
67+
Flag up any error messages created during file processing
68+
69+
@param src - The input file to convert to DITA
70+
@param result - Whether Pandoc succeeded or failed
71+
@param message - err.out from Pandoc
72+
-->
73+
<macrodef name="process-pandoc-errors">
74+
<attribute name="result" />
75+
<attribute name="message" />
76+
<attribute name="src" />
77+
<sequential>
78+
<local name="pandoc.isError"/>
79+
<local name="pandoc.message.code"/>
80+
81+
<condition property="pandoc.isError">
82+
<not>
83+
<equals arg1="@{result}" arg2="0"/>
84+
</not>
85+
</condition>
86+
87+
<echo taskname="pandoc" if:set="pandoc.isError" level="error" message="@{message}"/>
88+
89+
90+
<dita-ot-fail id="PAND003F" if:set="pandoc.isError">
91+
<param name="1" value="@{src}"/>
92+
</dita-ot-fail>
93+
94+
<condition property="pandoc.message.code" value="PAND002W">
95+
<contains string="@{message}" substring="Multiple Root Nodes detected."/>
96+
</condition>
97+
98+
<dita-ot-echo if:set="pandoc.message.code" id="${pandoc.message.code}">
99+
<param name="1" value="@{src}"/>
100+
</dita-ot-echo>
101+
</sequential>
102+
</macrodef>
103+
104+
105+
<!--
106+
Ensure that any images associated with the coverted file are copied to
107+
the output
108+
109+
@param src - The input file to convert to DITA
110+
@param dest - The output file location
111+
-->
112+
<macrodef name="process-pandoc-images">
113+
<attribute name="src" />
114+
<attribute name="dest" />
115+
<sequential>
116+
<local name="src.dir"/>
117+
<local name="relative.path"/>
118+
<dirname property="src.dir" file="@{src}"/>
119+
<relative-path src="${src.dir}" to="relative.path"/>
120+
121+
<xmltask taskname="pandoc.read-images" source="@{dest}">
122+
<call path="//image[not(contains(@href, ':'))]">
123+
<param default="" name="href" path="@href" />
124+
<actions>
125+
<local name="pandoc.image"/>
126+
<local name="pandoc.image.exists"/>
127+
128+
129+
<condition property="pandoc.image" value="${src.dir}/@{href}">
130+
<and>
131+
<available file="${src.dir}/@{href}"/>
132+
<isset property="copy-image.todir"/>
133+
</and>
134+
</condition>
135+
136+
137+
<echo level="error" message="${pandoc.image}"/>
138+
<echo level="error" message="${copy-image.todir}${relative.path}@{href}"/>
139+
140+
<copy taskname="pandoc.copy-image" if:set="pandoc.image" toFile="${copy-image.todir}${relative.path}/@{href}" failonerror="false" file="${pandoc.image}"/>
141+
</actions>
142+
</call>
143+
</xmltask>
144+
</sequential>
145+
</macrodef>
146+
147+
</antlib>

resource/relative.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 src - The value to convert
10+
// @param to - The property to set
11+
//
12+
13+
var src = attributes.get("src");
14+
var temp = project.getProperty("args.input.dir")
15+
var relative = src.replace(temp, '');
16+
17+
project.setProperty(attributes.get("to"), relative);

test/image-processing/document.ditamap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">
33
<bookmap>
44
<chapter format="pandoc" processing-role="normal" type="topic" href="markdown.md"/>
5-
<chapter format="dita" processing-role="normal" type="topic" href="topic.dita"/>
5+
<chapter format="pandoc" processing-role="normal" type="topic" href="topics/markdown.md"/>
66
</bookmap>

test/image-processing/markdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Chapter
1+
# Chapter 1
22

33
## Topic 1
44

test/image-processing/topic.dita

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Chapter 2
2+
3+
## Topic 3
4+
5+
Lorem ipsum dolor sit amet consectetur adipiscing, elit malesuada ligula vehicula netus sollicitudin sociis, feugiat scelerisque luctus parturient pharetra. Dis pretium ante fringilla nunc velit nisi risus primis felis vel, venenatis auctor nascetur viverra non sagittis habitant condimentum integer. Himenaeos iaculis placerat neque nisl sodales faucibus ut porttitor maecenas et quam posuere mauris. This is an inline image ![](../icon-black.png) venenatis tortor scelerisque suspendisse magna malesuada rhoncus commodo feugiat pretium urna justo.
6+
7+
## Topic 4
8+
9+
Class ornare feugiat arcu parturient lacus mollis purus at, primis turpis morbi libero ultricies hendrerit. Volutpat sed ullamcorper bibendum mi sagittis ultricies ridiculus sociis nam, dapibus proin curae odio lacus est dignissim ad, commodo ornare nec phasellus condimentum laoreet vehicula porttitor.
10+
11+
![](../img/icon-blue.png)
12+
13+
Himenaeos nunc aptent massa ridiculus auctor nisi pellentesque maecenas a eleifend, fermentum fringilla semper nam viverra vivamus velit metus in posuere, conubia tellus venenatis rutrum proin erat enim mi libero.
14+
15+

0 commit comments

Comments
 (0)