Commit 32432a83 authored by Euan Imperial's avatar Euan Imperial
Browse files

Extracted some functions for code duplication and made PDF extend Markdown

parent 70ae76de
Pipeline #172840 passed with stages
in 17 seconds
......@@ -22,6 +22,21 @@ public class MarkdownResultPage implements Page {
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File markdown = getFile();
outputStreams(markdown);
// Remove temporary file
markdown.delete();
}
void outputStreams(File markdown) throws IOException {
// Byte streams
InputStream inputStream = new FileInputStream(markdown);
OutputStream outputStream = new FileOutputStream(markdown);
inputStream.transferTo(outputStream);
}
File getFile() throws IOException {
File markdown = new File("result.md");
// Header
FileWriter writer = new FileWriter("result.md");
......@@ -34,13 +49,6 @@ public class MarkdownResultPage implements Page {
} else {
writer.write(answer);
}
// Byte streams
InputStream inputStream = new FileInputStream(markdown);
OutputStream outputStream = new FileOutputStream(markdown);
inputStream.transferTo(outputStream);
// Remove temporary file
markdown.delete();
return markdown;
}
}
......@@ -3,47 +3,28 @@ package ic.doc.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public class PDFResultPage implements Page {
private final String query;
private final String answer;
public class PDFResultPage extends MarkdownResultPage {
public PDFResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
super(query, answer);
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File markdown = new File("result.md");
// Header
FileWriter writer = new FileWriter("result.md");
writer.write(query + "\n");
writer.write("======\n");
// Content
if (answer == null || answer.isEmpty()) {
writer.write("Sorry, we did not understand your query.\n");
} else {
writer.write(answer);
}
writer.close();
File markdown = getFile();
ProcessBuilder pb = new ProcessBuilder();
pb.command("pandoc -s -o result.pdf result.md");
pb.start();
File pdf = new File("result.pdf");
// Byte streams
InputStream inputStream = new FileInputStream(pdf);
OutputStream outputStream = new FileOutputStream(pdf);
inputStream.transferTo(outputStream);
outputStreams(markdown);
// Remove temporary file
markdown.delete();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment