Skip to content

Commit 8b3de7a

Browse files
committed
pandocode-live: add image output
1 parent e720cbc commit 8b3de7a

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

flake.nix

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,38 @@
4545

4646
contents = [
4747
pkgs.lighttpd
48+
pkgs.pandoc
49+
pkgs.poppler_utils
50+
(pkgs.texlive.combine {
51+
inherit (pkgs.texlive) scheme-basic algorithmicx xcolor standalone preview;
52+
})
4853
(pkgs.writeShellScriptBin "python-wrapper" ''
4954
export PYTHONPATH=${pkgs.runCommand "pandocode-module" {} ''
5055
mkdir -p $out
5156
cp -r ${./.}/. $out/pandocode
5257
''}
53-
exec ${python}/bin/python3 "$@"
58+
exec ${pkgs.python3.withPackages (pypi: with pypi; [
59+
panflute
60+
pdf2image
61+
pillow
62+
])}/bin/python3 "$@"
5463
'')
5564
(pkgs.runCommand "content" { } ''
56-
mkdir -p $out/var/www/cgi-bin
65+
mkdir -p $out/var/www
5766
cp -vr ${pandocode-live-frontend}/. $out/var/www/
5867
cp -vr ${./live/cgi-bin}/. $out/var/www/cgi-bin
68+
cp -vr ${./live/etc}/. $out/etc
5969
'')
6070
];
6171

72+
enableFakechroot = true;
73+
fakeRootCommands = ''
74+
mkdir -p /root /etc /tmp
75+
echo 'root:x:0:0::/root:/noshell' > /etc/passwd
76+
'';
77+
6278
config = {
63-
Cmd = [ "/bin/lighttpd" "-D" "-f" "${./live/lighttpd.conf}" ];
79+
Cmd = [ "/bin/lighttpd" "-D" "-f" "/etc/lighttpd.conf" ];
6480
Env = [ "" ];
6581
ExposedPorts = {
6682
"8080/tcp" = { };

live/cgi-bin/image.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# print every line back
2+
import subprocess
3+
import sys
4+
from io import BytesIO
5+
from pdf2image import convert_from_bytes
6+
from pandocode.codeprocessor import process_code
7+
8+
print("Content-type: image/webp")
9+
print("Access-Control-Allow-Origin: *")
10+
print("")
11+
sys.stdout.flush()
12+
13+
processed = process_code(sys.stdin.read())
14+
process = subprocess.run(
15+
[
16+
'pandoc',
17+
'-f', 'markdown',
18+
'-t', 'pdf',
19+
'--template', '/etc/template.tex',
20+
'--pdf-engine', 'pdflatex',
21+
'--pdf-engine-opt', '-disable-write18',
22+
'-o', '-'
23+
],
24+
input=processed.encode('utf-8'),
25+
stdout=subprocess.PIPE,
26+
)
27+
if process.returncode != 0:
28+
exit(process.returncode)
29+
30+
images = convert_from_bytes(process.stdout)
31+
image_bytes = BytesIO()
32+
images[0].save(image_bytes, format='WebP')
33+
image_bytes = image_bytes.getvalue()
34+
35+
sys.stdout.buffer.write(image_bytes)
36+
sys.stdout.flush()

live/cgi-bin/latex.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# print every line back
21
import sys
32
from pandocode.codeprocessor import process_code
43

54
print("Content-type: text/plain")
65
print("Access-Control-Allow-Origin: *")
76
print("")
7+
sys.stdout.flush()
88

9-
print(process_code(sys.stdin.read()))
9+
sys.stdout.write(process_code(sys.stdin.read()))
10+
sys.stdout.flush()
File renamed without changes.

live/etc/template.tex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
\documentclass[preview]{standalone}
2+
\usepackage[noend]{algpseudocode}
3+
\begin{document}
4+
$body$
5+
\end{document}

0 commit comments

Comments
 (0)