Copy images for html output.

WIP: Convert SVG to PNG for html output.
This commit is contained in:
Salman Qureshi
2012-09-18 10:15:37 +05:00
parent 37ef230b58
commit cc2d46a0b2
6 changed files with 116 additions and 17 deletions

View File

@@ -519,6 +519,7 @@ public abstract class WebHelpMojo extends AbstractWebhelpMojo {
int pos = targetExtQueryFile.lastIndexOf(File.separator);
targetExtQueryFile = targetExtQueryFile.substring(0, pos);
map.put("targetHtmlContentDir", baseDir+File.separator+targetExtQueryFile);
map.put("targetDir", baseDir.getAbsolutePath()+File.separator+"figures");
//makePdf is a POM configuration for generate-webhelp goal to control the execution of

View File

@@ -135,7 +135,6 @@ public class PDFBuilder {
// Extract all fonts into fonts directory
//
FileUtils.extractJaredDirectory("fonts",PDFBuilder.class,imageParentDirectory);
System.out.println("************************************** HELLO WORLD **************************************");
}
public File processSources(Map<String,String> map) throws MojoExecutionException{

View File

@@ -10,6 +10,7 @@ import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
@@ -77,7 +78,8 @@ public class CopyAndTransformXProcStep extends DefaultStep {
private URI getTargetDirectoryURI() {
RuntimeValue target = getOption(_target);
URI uri = target.getBaseURI().resolve(target.getString());
URI uri = target.getBaseURI().resolve(FilenameUtils.normalizeNoEndSeparator(target.getString()));
return uri;
}
@@ -98,6 +100,8 @@ public class CopyAndTransformXProcStep extends DefaultStep {
private XdmNode processInlineImages(XdmNode doc) {
// System.out.println("******************* getTargetDirectoryURI() = " + getTargetDirectoryURI());
// System.out.println("******************* getTargetHtmlConteURI() = " + getTargetHtmlContentDirectoryURI());
CopyTransformImage xpathRepl =
new CopyTransformImage( "//*:imagedata/@fileref",

View File

@@ -10,7 +10,7 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Random;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
@@ -18,6 +18,7 @@ import net.sf.saxon.s9api.XdmNode;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
@@ -36,6 +37,7 @@ import com.xmlcalabash.util.ProcessMatchingNodes;
public class CopyTransformImage implements ProcessMatchingNodes {
private String xpath;
private Map<String, String> baseUriToDirMap = new HashMap<String, String>();
private Map<String, String> imageMap = new HashMap<String, String>();
private ProcessMatch matcher;
@@ -79,11 +81,13 @@ public class CopyTransformImage implements ProcessMatchingNodes {
if (!file.exists()) {
getLog().error("DocBook File: '" + inputDocbookName + "' - File: '" + sourceImageUri.getPath() + "' - Problem: File does not exist!");
//throw new XProcException(stepNode, "Cannot copy: file does not exist: " + file.getAbsolutePath());
return null;
}
if (file.isDirectory()) {
getLog().error("DocBook File: '" + inputDocbookName + "' - File: '" + sourceImageUri.getPath() + "' - Problem: File is a directory!");
//throw new XProcException(stepNode, "Cannot copy: file is a directory: " + file.getAbsolutePath());
return null;
}
return file;
@@ -142,11 +146,94 @@ public class CopyTransformImage implements ProcessMatchingNodes {
}
private String processSelectedImage(XdmNode node) {
URI baseUri = node.getBaseURI();
String fileRef = node.getStringValue();
URI baseDirUri = baseUri.resolve(".");
String srcImgFilePath = FilenameUtils.normalize(baseDirUri.getPath() + File.separator + fileRef);
File srcImgFile = getFileHandle(srcImgFilePath);
if (outputType.equals("pdf")) {
//Need to check only for the existence of the image file
if (! fileExists(srcImgFile)) {
reportImageNotFoundError(baseUri, fileRef, srcImgFile);
return node.getStringValue();
} else {
return node.getStringValue();
}
}
else if (outputType.equals("html")) {
String targetDirPath = calculateTargetDirPath(baseUri.getPath(), fileRef);
File targetDir = makeDirs(targetDirPath);
//For HTML, we need a more elaborate check for missing images
if (fileExists(srcImgFile)) {
//simply copy the src file to the destination
File copiedFile = copyFile(srcImgFile, targetDir);
return RelativePath.getRelativePath(new File(targetHtmlContentDirectoryUri), copiedFile);
}
else if (fileExists(getFileHandle(FilenameUtils.removeExtension(srcImgFilePath) + ".svg"))) {
//convert the svg to the relevant type and copy
File copiedFile = copyFile(srcImgFile, targetDir);
return RelativePath.getRelativePath(new File(targetHtmlContentDirectoryUri), copiedFile);
}
else {
reportImageNotFoundError(baseUri, fileRef, srcImgFile);
return node.getStringValue();
}
}
else {
//we only know how to handle "pdf" and "html" outputTypes so just return the value
return node.getStringValue();
}
}
private String calculateTargetDirPath(String baseUriPath, String fileRef) {
String targetDirForBaseUri = "" + (10 + new Random().nextInt(90));
String targetDirForFileRef = FilenameUtils.getPathNoEndSeparator(fileRef.replaceAll("\\.\\.", "a"));
return targetDirForBaseUri + File.separator +
targetDirForFileRef;
}
private File copyFile(File srcFile, File targetDir) {
try {
FileUtils.copyFileToDirectory(srcFile, targetDir);
} catch (IOException e) {
return null;
}
return new File(targetDir.getAbsolutePath() + File.separator + srcFile.getName());
}
private void reportImageNotFoundError(URI baseUri, String fileRef, File srcImgFile) {
System.out.println("**********************************************************");
System.out.println("File NOT found : " + baseUri.getPath());
}
private boolean fileExists(File file) {
// TODO: Check for case sensitive file names
return file==null ? false : file.exists();
}
private File makeDirs(String relativePath) {
//TODO: handle cases where dirPath is path to a file
File dir = new File(targetDirectoryUri.getPath(), relativePath);
if (dir.mkdir() && dir.mkdirs()) {
}
return dir;
}
private File getFileHandle(String filePath) {
// TODO: Handle cases where the filePath refers to a directory
return new File(filePath);
}
private String processSelectedImageOLD(XdmNode node) {
System.out.println("****** Processing node: " + node.toString() + " --- baseUri="+node.getBaseURI().toString());
String value = node.getStringValue();
if(imageMap.containsKey(value)) {
//System.out.println("\n\n*********** already processed **************** "+imageMap.get(value));
System.out.println("\n\n*********** already processed **************** "+imageMap.get(value));
return imageMap.get(value);
}
@@ -182,6 +269,7 @@ public class CopyTransformImage implements ProcessMatchingNodes {
imageMap.put(value, sourceImageFile.getAbsolutePath());
}
else if(outputType.equals("html")) {
System.out.println("****** Entering elseif outputType=='html'");
//if the output type is html then we need to decide steps based on the image extension
//if the image is an SVG then transform and save a png in the html target directory
//if the image is a PNG then make a huge assumption that an SVG was specified in the Docbook before the png
@@ -197,7 +285,7 @@ public class CopyTransformImage implements ProcessMatchingNodes {
}
sourceImageFile = getHandleToImageFile(sourceImageUri);
if(sourceImageFile != null) {
System.out.println("****** Case 1");
File copiedFile = performFileCopyAndTransformation(targetDirectoryUri, inputDocbookName, sourceImageFile);
String correctRelativePathToImage = RelativePath.getRelativePath(new File(targetHtmlContentDirectoryUri), copiedFile);
imageMap.put(value, correctRelativePathToImage);
@@ -205,16 +293,21 @@ public class CopyTransformImage implements ProcessMatchingNodes {
//File not found in source and target folders.
}
} else if(sourceImageFile != null && !isSVG) {
String correctRelativePathToImage = RelativePath.getRelativePath( new File(targetHtmlContentDirectoryUri), sourceImageFile);
System.out.println("****** Case 2: " + sourceImageFile);
File copiedFile = performFileCopyAndTransformation(targetDirectoryUri, inputDocbookName, sourceImageFile);
String correctRelativePathToImage = RelativePath.getRelativePath( new File(targetHtmlContentDirectoryUri), copiedFile);
imageMap.put(value, correctRelativePathToImage);
} else if(sourceImageFile != null && isSVG) {
System.out.println("****** Case 3");
//There really isn't any need to copy the SVG for html output but it is being done right now. We only care about transforming it to a PNG.
File copiedFile = performFileCopyAndTransformation(targetDirectoryUri, inputDocbookName, sourceImageFile);
String correctRelativePathToImage = RelativePath.getRelativePath( new File(targetHtmlContentDirectoryUri), copiedFile);
imageMap.put(value, correctRelativePathToImage);
}
System.out.println("****** Exiting elseif outputType=='html'");
}
} catch (XProcException x) {
//getLog().error(x.getMessage());

View File

@@ -90,6 +90,11 @@ make sense
filelist = getPathList(f);
s = matchPathLists(homelist,filelist);
System.out.println("*************** Relative Path: ********************************");
System.out.println("*** home: " + home.getAbsolutePath());
System.out.println("*** file: " + f.getAbsolutePath());
System.out.println("*** path: " + s);
return s;
}
@@ -97,13 +102,10 @@ make sense
* test the function
*/
public static void main(String args[]) {
if (args.length != 2) {
System.out.println("RelativePath <home> <file>");
return;
}
System.out.println("home = " + args[0]);
System.out.println("file = " + args[1]);
System.out.println("path = " + getRelativePath(new File(args[0]),new
File(args[1])));
String home = "/home/mtariq/content/myfolder";
String file = "/home/myfolder/figures/fig.png";
System.out.println("home = " + home);
System.out.println("file = " + file);
System.out.println("path = " + getRelativePath(new File(home),new File(file)));
}
}

View File

@@ -824,7 +824,7 @@
</p:variable>
<cx:copy-transform name="loop">
<p:with-option name="target" select="concat('', 'file:///home/mtariq/Projects/rackspace/compute-api/openstack-compute-api-2/target/docbkx/webhelp/api/openstack-compute/2')"/>
<p:with-option name="target" select="concat('file://',$target.html.content.dir, '/../figures')"/>
<p:with-option name="targetHtmlContentDir" select="concat('file://',$target.html.content.dir)"/>
<p:with-option name="inputFileName" select="concat($input.docbook.file,'')"/>
<p:with-option name="outputType" select="concat($output.type,'')"/>