Skip to content

Commit

Permalink
ImageFormulaConverter now saves images inside the temporary directory…
Browse files Browse the repository at this point in the history
… rather than seperate temporary files

New cli option to create only markup
Exported markup folder now contains also image files, now relative image filepaths are used
Update Version number to 0.5
  • Loading branch information
Sevyls committed Feb 3, 2015
1 parent e3fe56a commit e46647f
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 130 deletions.
216 changes: 137 additions & 79 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion latex2mobi-converter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<name>Latex to Mobi Formula Converter - Bachelor Thesis</name>
<groupId>at.ac.tuwien.ims</groupId>
<artifactId>latex2mobi-converter</artifactId>
<version>0.4.2</version>
<version>0.5.0</version>
<licenses>
<license>
<name>MIT License</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ public class Main {
// Flag options
private static boolean replaceWithPictures = false;
private static boolean debug = false;
private static boolean exportMarkup = false;
private static boolean noMobiConversion = false;

// Value options
private static String title = null;
private static String filename = "LaTeX2Mobi";

// Paths
private static ArrayList<Path> inputPaths = new ArrayList<Path>();
private static Path workingDirectory;
private static Path outputPath;
private static boolean exportMarkup;
private static String filename = "LaTeX2Mobi";


/**
Expand Down Expand Up @@ -110,6 +111,7 @@ public static void main(String[] args) {
converter.setTitle(title);
converter.setDebug(debug);
converter.setExportMarkup(exportMarkup);
converter.setNoMobiConversion(noMobiConversion);

Path resultFile = converter.convert();

Expand All @@ -131,6 +133,7 @@ private static void parseCli(String[] args) {
}

if (cmd.hasOption('d')) {
// Activate debug markup only - does not affect logging!
debug = true;
}

Expand All @@ -146,6 +149,11 @@ private static void parseCli(String[] args) {
filename = cmd.getOptionValue('f');
}

if (cmd.hasOption('n')) {
exportMarkup = true; // implicit markup export
noMobiConversion = true;
}

if (cmd.hasOption('i')) {
String[] inputValues = cmd.getOptionValues('i');
for (String inputValue : inputValues) {
Expand Down Expand Up @@ -281,6 +289,7 @@ private static void initializeOptions() {
options.addOption("f", "filename", true, "output filename");
options.addOption("o", "output-dir", true, "output directory");
options.addOption("m", "export-markup", false, "export html markup");
options.addOption("n", "no-mobi", false, "no Mobi conversion, just markup, NOTE: makes -m implicit!");
options.addOption("t", "title", true, "Document title");
options.addOption("h", "help", false, "show this help");
options.addOption("d", "debug", false, "show debug output in html markup");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.elements.Formula;

import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.output.XMLOutputter;
Expand Down Expand Up @@ -79,6 +81,7 @@ public class Converter {
private boolean debug = false;

private boolean exportMarkup = false;
private boolean noMobiConversion = false;

/**
* the directory path where the result will be written to
Expand Down Expand Up @@ -180,6 +183,14 @@ public void setInputPaths(ArrayList<Path> inputPaths) {
this.inputPaths = inputPaths;
}

public boolean isNoMobiConversion() {
return noMobiConversion;
}

public void setNoMobiConversion(boolean noMobiConversion) {
this.noMobiConversion = noMobiConversion;
}

/**
* Converts a single input file from LaTeX to Mobi
* @return Path of the resulting File
Expand Down Expand Up @@ -224,35 +235,42 @@ public Path convert() {
// Saving html file
File htmlFile = saveHtmlFile(document);

Path markupDir = null;
if (exportMarkup || noMobiConversion) {
markupDir = exportMarkup(htmlFile.toPath());
}


// Convert to MOBI format
logger.info("Converting completed HTML to MOBI format...");
File mobiFile = htmlToMobiConverter.convertToMobi(htmlFile);

// Save file
Path resultFilepath = null;
try {
// Don't overwrite files
Path outputFilepath;
int i = 1;
String replaceFilename = filename + FILE_EXTENSION;
while (true) {
outputFilepath = outputPath.resolve(replaceFilename);
if (Files.exists(outputFilepath) == false) {
break;
if (noMobiConversion) {
return markupDir.toAbsolutePath();
} else {
// Convert to MOBI format
logger.info("Converting completed HTML to MOBI format...");
File mobiFile = htmlToMobiConverter.convertToMobi(htmlFile);

// Save file
Path resultFilepath = null;
try {
// Don't overwrite files
Path outputFilepath;
int i = 1;
String replaceFilename = filename + FILE_EXTENSION;
while (true) {
outputFilepath = outputPath.resolve(replaceFilename);
if (Files.exists(outputFilepath) == false) {
break;
}
replaceFilename = filename + " (" + i + ")" + FILE_EXTENSION;
i++;
}
replaceFilename = filename + " (" + i + ")" + FILE_EXTENSION;
i++;
}

resultFilepath = Files.move(mobiFile.toPath(), outputFilepath);
logger.debug("Mobi file moved to: " + resultFilepath.toAbsolutePath().toString());
return resultFilepath.toAbsolutePath();
resultFilepath = Files.move(mobiFile.toPath(), outputFilepath);
logger.debug("Mobi file moved to: " + resultFilepath.toAbsolutePath().toString());
return resultFilepath.toAbsolutePath();

} catch (IOException e) {
logger.error(e.getMessage(), e);
return null;
} catch (IOException e) {
logger.error(e.getMessage(), e);
return null;
}
}
}

Expand All @@ -266,14 +284,19 @@ private File saveHtmlFile(Document document) {


try {
Path tempDir = Files.createTempDirectory("latex2mobi");
Path tempDirPath = formulaConverter.getTempDirPath();

ClassLoader classLoader = getClass().getClassLoader();
InputStream mainCssIs = classLoader.getResourceAsStream(MAIN_CSS_FILENAME);

logger.debug("Copying main.css file to temp dir: " + tempDir.toAbsolutePath().toString());
Files.copy(mainCssIs, tempDir.resolve(MAIN_CSS_FILENAME));
tempFilepath = tempDir.resolve("latex2mobi.html");
logger.debug("Copying main.css file to temp dir: " + tempDirPath.toAbsolutePath().toString());
try {
Files.copy(mainCssIs, tempDirPath.resolve(MAIN_CSS_FILENAME));
} catch (FileAlreadyExistsException e) {
// do nothing
}

tempFilepath = tempDirPath.resolve("latex2mobi.html");

logger.debug("tempFile created at: " + tempFilepath.toAbsolutePath().toString());

Expand All @@ -283,20 +306,6 @@ private File saveHtmlFile(Document document) {
logger.info("Debug markup will be generated.");
}

if (exportMarkup) {
Path markupDir = workingDirectory.resolve(title + "-markup");
try {
Files.createDirectory(markupDir);
} catch (FileAlreadyExistsException e) {
// do nothing
}
mainCssIs = classLoader.getResourceAsStream(MAIN_CSS_FILENAME);
Files.copy(mainCssIs, markupDir.resolve(MAIN_CSS_FILENAME), StandardCopyOption.REPLACE_EXISTING);

Files.copy(tempFilepath, markupDir.resolve(tempFilepath.getFileName()), StandardCopyOption.REPLACE_EXISTING);

logger.info("Exported markup to folder: " + markupDir.toAbsolutePath().toString());
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
// TODO Exception handling
Expand All @@ -305,4 +314,29 @@ private File saveHtmlFile(Document document) {
return tempFilepath.toFile();
}

private Path exportMarkup(Path tempFilepath) {
Path markupDir = workingDirectory.resolve(title + "-markup"); // TODO rename if already exists
try {
try {
Files.createDirectory(markupDir);
} catch (FileAlreadyExistsException e) {
// do nothing
}

Path tempDirPath = tempFilepath.getParent();
File tempDir = tempDirPath.toFile();

// Copy all files from temp folder to the markup output folder
String[] files = tempDir.list(FileFileFilter.FILE);
for (int i = 0; i < files.length; i++) {
Files.copy(tempDirPath.resolve(files[i]), markupDir.resolve(files[i]), StandardCopyOption.REPLACE_EXISTING);
}

logger.info("Exported markup to folder: " + markupDir.toAbsolutePath().toString());
} catch (IOException e) {
logger.error("Error saving markup files: " + e.getMessage(), e);
return null;
}
return markupDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import uk.ac.ed.ph.snuggletex.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -69,6 +71,7 @@ public abstract class FormulaConverter {
}

private boolean debug = false;
protected Path tempDirPath = null;

public boolean isDebug() {
return debug;
Expand All @@ -78,6 +81,14 @@ public void setDebug(boolean debug) {
this.debug = debug;
}

public FormulaConverter() {
try {
tempDirPath = Files.createTempDirectory("latex2mobi");
} catch (IOException e) {
logger.error("Error creating temporary directory!");
}
}

/**
* Parse latex formula code to entities, which afterward can be rendered to html
*
Expand Down Expand Up @@ -221,4 +232,8 @@ public Document replaceFormulas(Document doc, Map<Integer, Formula> formulaMap)
}
return doc;
}

public Path getTempDirPath() {
return tempDirPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public Formula parse(int id, String latexFormula) {
try {
final org.w3c.dom.Document doc = MathMLParserSupport.parseString(formula.getMathMl());

final File outFile = Files.createTempFile("formula", ".png").toFile();
final File outFile = Files.createTempFile(tempDirPath, "formula", ".png").toFile();

formula.setImageFilePath(outFile.toPath());

// Generate Html Image tag
Element imageTag = new Element("img");
imageTag.setAttribute("src", formula.getImageFilePath().toAbsolutePath().toString());
imageTag.setAttribute("src", formula.getImageFilePath().getFileName().toString());
imageTag.setAttribute("alt", FORMULA_ID_PREFIX + formula.getId());
formula.setHtml(imageTag);

Expand All @@ -86,7 +86,8 @@ public Formula parse(int id, String latexFormula) {
params.setParameter(Parameter.MATHSIZE, 25f);

// convert to image
Converter.getInstance().convert(doc, outFile, "image/png", params);
net.sourceforge.jeuclid.converter.Converter imageConverter = net.sourceforge.jeuclid.converter.Converter.getInstance();
imageConverter.convert(doc, outFile, "image/png", params);
logger.debug("Image file created at: " + outFile.toPath().toAbsolutePath().toString());
} catch (SAXException e1) {
logger.error(e1.getMessage(), e1);
Expand All @@ -96,14 +97,15 @@ public Formula parse(int id, String latexFormula) {
logger.error(e3.getMessage(), e3);
}


return formula;
}


/**
* Replaces formula placeholders with created images
*
* @deprecated This is an old implementation and is not being used anymore.
*
* @param document JDOM HTML Document with placeholders
* @param imagePaths Filepaths of Images to be replaced
* @return DOM of the resulting HTML Document with &lt;img&gt;-Tags
Expand All @@ -127,7 +129,7 @@ public Document replaceFormulasWithImages(Document document, Map<Integer, Path>
element.removeAttribute("class");
element.removeContent();
Element imageTag = new Element("img");
imageTag.setAttribute("src", imagePath.toAbsolutePath().toString());
imageTag.setAttribute("src", imagePath.toFile().getName());
imageTag.setAttribute("alt", FORMULA_ID_PREFIX + id);
element.addContent(imageTag);
}
Expand Down

0 comments on commit e46647f

Please sign in to comment.