Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Wang, Vincent H
simplewebapp
Commits
32432a83
Commit
32432a83
authored
Jan 24, 2021
by
Euan Imperial
Browse files
Extracted some functions for code duplication and made PDF extend Markdown
parent
70ae76de
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/ic/doc/web/MarkdownResultPage.java
View file @
32432a83
...
...
@@ -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
;
}
}
src/main/java/ic/doc/web/PDFResultPage.java
View file @
32432a83
...
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment