Commit cfca3fea authored by Tianyi Zuo's avatar Tianyi Zuo
Browse files

Reformat and added class pdf creator but cannot install pandoc on lab computers

parent 7182d3af
......@@ -19,8 +19,13 @@ public class MarkDownCreator implements Page {
this.answer = answer;
}
private File toMarkDown(String result) throws IOException {
File temp = File.createTempFile(query,".markdown",null);
public static File toMarkDown(String result, String query) throws IOException {
if (result == null || result.isEmpty()) {
result = "# Sorry\nSorry, we didn't understand" + query;
}else{
result ="# "+ query + "\n" + result;
}
File temp = File.createTempFile(query,".md",null);
System.out.println(temp.getAbsolutePath());
BufferedWriter writer = new BufferedWriter(new FileWriter(temp));
writer.write(result);
......@@ -30,15 +35,9 @@ public class MarkDownCreator implements Page {
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/md");
resp.setContentType("text/markdown; variant=CommonMark");
OutputStream writer = resp.getOutputStream();
// Content
if (answer == null || answer.isEmpty()) {
answer = "# Sorry\nSorry, we didn't understand" + query;
}else{
answer ="# "+ query + "\n" + answer;
}
File markdown = toMarkDown(answer);
File markdown = toMarkDown(answer, query);
InputStream stream = new FileInputStream(markdown);
writer.write(stream.readAllBytes());
writer.close();
......
package ic.doc;
import static ic.doc.MarkDownCreator.toMarkDown;
import ic.doc.web.Page;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public class PdfCreator implements Page {
private final String query;
private String answer;
public PdfCreator(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("application/pdf");
OutputStream writer = resp.getOutputStream();
File markdown = toMarkDown(answer, query);
// turn markdown to a pdf using pandoc
InputStream stream = new FileInputStream(markdown);
writer.write(stream.readAllBytes());
writer.close();
markdown.delete();
}
}
......@@ -5,7 +5,6 @@ import ic.doc.web.IndexPage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -29,7 +28,6 @@ public class WebServer {
String query = req.getParameter("q");
String type = req.getParameter("t");
execute(query, type, resp);
}
}
......@@ -44,7 +42,8 @@ public class WebServer {
System.out.println("Is a markdown");
new MarkDownCreator(query, result).writeTo(resp);
}else{
//PDF
System.out.println("Is a pdf");
new PdfCreator(query, result).writeTo(resp);
}
}
}
......
......@@ -21,10 +21,9 @@ public class IndexPage implements Page {
"<h1>Welcome!!</h1>" +
"<p>Enter your query in the box below: " +
"<form>" +
"<input type=\"text\" name=\"q\" />" +
" <input type=\"radio\" name=\"t\" value=\"markdown\"> markdown<br>"+
//// "<input type=\"radio\" name=\"gender\" value=\"female\"> Female<br>"+
//// "<input type=\"radio\" name=\"gender\" value=\"other\"> Other"+
"<input type=\"text\" name=\"q\" /><br>" +
"<input type=\"radio\" name=\"t\" value=\"markdown\"> markdown<br>"+
"<input type=\"radio\" name=\"t\" value=\"pdf\"> pdf<br>"+
"<br><br><input type=\"submit\">" +
"</form>" +
"</p>");
......
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