Skip to content

Commit 9b0f4bb

Browse files
committed
pandocode-live: add image output
1 parent e720cbc commit 9b0f4bb

File tree

4 files changed

+59
-3
lines changed

4 files changed

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