Commit 9e6c28e2 authored by Tianyi Zuo's avatar Tianyi Zuo
Browse files

Added markdown

parent 9d159196
package ic.doc;
import ic.doc.web.Page;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
public class MarkDownCreator implements Page {
private final String query;
private String answer;
public MarkDownCreator(String query, String answer) {
this.query = query;
this.answer = answer;
}
private File toMarkDown(String result) throws IOException {
File temp = File.createTempFile(query,".md",null);
System.out.println(temp.getAbsolutePath());
BufferedWriter writer = new BufferedWriter(new FileWriter(temp));
writer.write(result);
writer.close();
return temp;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/md");
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);
InputStream stream = new FileInputStream(markdown);
writer.write(stream.readAllBytes());
markdown.delete();
}
}
package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import java.io.File;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -28,13 +30,18 @@ public class WebServer {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String type = req.getParameter("t");
if(type == "markdown"){
System.out.println("Is a mark down!");
}
if (query == null) {
new IndexPage().writeTo(resp);
} else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
String result = new QueryProcessor().process(query);
if(type == null){
new HTMLResultPage(query, result).writeTo(resp);
}else if (type.equals("markdown")){
new MarkDownCreator(query, result).writeTo(resp);
}else{
//PDF
}
}
}
}
......
......@@ -6,6 +6,7 @@ import java.io.PrintWriter;
public class IndexPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
......@@ -21,11 +22,9 @@ public class IndexPage implements Page {
"<p>Enter your query in the box below: " +
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<form action=\"\">" +
" <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"+
"</form>"+
//// "<input type=\"radio\" name=\"gender\" value=\"female\"> Female<br>"+
//// "<input type=\"radio\" name=\"gender\" value=\"other\"> Other"+
"<br><br><input type=\"submit\">" +
"</form>" +
"</p>");
......
......@@ -4,6 +4,5 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface Page {
void writeTo(HttpServletResponse resp) throws IOException;
}
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