#!/bin/sh
set -e

if [ -z "$1" ]; then
	echo "Usage: $0 path/to/gemtext2html"
	exit 1
fi

tmpfile="$(mktemp /tmp/gemtext2html-tests-XXXXX.html)"
cleanup() {
	rm "$tmpfile"
}
trap cleanup EXIT

"$1" -i "$2" -o "$tmpfile"
diff -u "$tmpfile" - <<EOF
<!DOCTYPE html>
<html>
<head>
<title> Gemtext2HTML</title>
</head>
<body>
<h1 class='gemtext-heading1'> Gemtext2HTML</h1>

Gemtext2HTML is a simple ObjC library with a C interface and a command line utility that translates Gemtext to HTML.<br/>
<br/>
The command line utitlity acts as a filter, meaning it reads Gemtext from stdin and outputs HTML to stdout. This can be used to render Gemtext in Forgejo, including a &quot;README.gmi&quot; file, by adding the following to the configuration:<br/>
<br/>
<pre class='gemtext-preformatted' style='margin-top: 0; margin-bottom: 0'>
[markup.gmi]
ENABLED = true
NEED_POSTPROCESS = true
FILE_EXTENSIONS = .gmi
RENDER_COMMAND = gemtext2html
IS_INPUT_FILE = false
</pre>
<br/>
Gemtext2HTML is written by Jonathan Schleifer using ObjFW and released under the LGPLv3.<br/>
<br/>
<span class='gemtext-external-link-emoji'>🌐</span> <a class='gemtext-external-link' href='gemini://objfw.nil.im'>ObjFW</a><br/>
<span class='gemtext-external-link-emoji'>🌐</span> <a class='gemtext-external-link' href='https://git.nil.im/js?tab=stars'>More projects written by js</a><br/>
</body>
</html>
EOF

"$1" -e -i "$2" -o "$tmpfile"
diff -u "$tmpfile" - <<EOF
<h1 class='gemtext-heading1'> Gemtext2HTML</h1>

Gemtext2HTML is a simple ObjC library with a C interface and a command line utility that translates Gemtext to HTML.<br/>
<br/>
The command line utitlity acts as a filter, meaning it reads Gemtext from stdin and outputs HTML to stdout. This can be used to render Gemtext in Forgejo, including a &quot;README.gmi&quot; file, by adding the following to the configuration:<br/>
<br/>
<pre class='gemtext-preformatted' style='margin-top: 0; margin-bottom: 0'>
[markup.gmi]
ENABLED = true
NEED_POSTPROCESS = true
FILE_EXTENSIONS = .gmi
RENDER_COMMAND = gemtext2html
IS_INPUT_FILE = false
</pre>
<br/>
Gemtext2HTML is written by Jonathan Schleifer using ObjFW and released under the LGPLv3.<br/>
<br/>
<span class='gemtext-external-link-emoji'>🌐</span> <a class='gemtext-external-link' href='gemini://objfw.nil.im'>ObjFW</a><br/>
<span class='gemtext-external-link-emoji'>🌐</span> <a class='gemtext-external-link' href='https://git.nil.im/js?tab=stars'>More projects written by js</a><br/>
EOF

"$1" -e --image-extensions=xxx,yyy --local-link-emoji=LOCAL --external-link-emoji=EXTERNAL -o "$tmpfile" <<EOF
=> foo.png Image
=> foo.xxx Image
=> foo.yyy Image
=> gemini://x/y.jpg Image
EOF
diff -u "$tmpfile" - <<EOF
<span class='gemtext-local-link-emoji'>LOCAL</span> <a class='gemtext-local-link' href='foo.png'>Image</a><br/>
<img class='gemtext-image' src='foo.xxx' alt='Image'/><br/>
<img class='gemtext-image' src='foo.yyy' alt='Image'/><br/>
<span class='gemtext-external-link-emoji'>EXTERNAL</span> <a class='gemtext-external-link' href='gemini://x/y.jpg'>Image</a><br/>
EOF

"$1" -o "$tmpfile" <<EOF
## Foo
EOF
diff -u "$tmpfile" - <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<h2 class='gemtext-heading2'> Foo</h2>
</body>
</html>
EOF

"$1" -e -o "$tmpfile" <<EOF
EOF
diff -u "$tmpfile" - <<EOF
EOF
