#!/bin/bash set -e check() { input="$(mktemp)" expected_output="$(mktemp)" output="$(mktemp)" current="input" while IFS='$\n' read -r line; do if [[ "$line" == "---" ]]; then current="output" elif [[ "$current" == "input" ]]; then echo "$line" >> "$input" else echo "$line" >> "$expected_output" fi done awk -f markdown.awk "$input" >> "$output" result="success" if ! cmp -s "$output" "$expected_output"; then echo "FAIL" echo "--- input" cat "$input" echo "--- expected" cat "$expected_output" echo "--- got" cat "$output" echo "---" result="fail" else echo "SUCCESS" fi rm "$input" rm "$expected_output" rm "$output" if [[ "$result" == "fail" ]]; then exit 1 fi } check <<-EOF This is a simple sentence. ---

This is a simple sentence.

EOF check <<-EOF This is a simple sentence. ---

This is a simple sentence.

EOF check <<-EOF First paragraph. Second paragraph. ---

First paragraph.

Second paragraph.

EOF check <<-EOF # Header body ---

Header

body

EOF check <<-EOF # Header1 ## Header2 ### Header3 ---

Header1

Header2

Header3

EOF check <<-EOF # Header ### ---

Header

EOF check <<-EOF underlined header 1 =================== underlined header 2 ------------------- ---

underlined header 1

underlined header 2

EOF check <<-EOF **bold** ---

bold

EOF check <<-EOF **bold multiline** ---

bold multiline

EOF check <<-EOF **bold ---

**bold

EOF check <<-"EOF" ``` first line of code second line of code > < & ``` ---

first line of code

second line of code

> < &
EOF check <<-"EOF" ``` first line of code second line of code ---

``` first line of code second line of code

EOF check <<-"EOF" This is `inline code` block ---

This is inline code block

EOF check <<-"EOF" code indented by spaces ---
code

indented by

spaces
EOF check <<-"EOF" asdf * foo * bar - baz + qux ---

asdf

EOF check <<-"EOF" asdf * bullet 1 * bullet 2 ---

asdf

EOF check <<-"EOF" asdf 1. number 1 2. number 2 ---

asdf

  1. number 1
  2. number 2
EOF check <<-"EOF" * **basic formatting** * ```in list``` --- EOF check <<-"EOF" * first level 1 * second level 1 * second level 2 * third level * first level 2 --- EOF check <<-"EOF" * bullet1 1. number1 * bullet2 2. number2 ---
  1. number1
  1. number2
EOF check <<-"EOF" > foo > bar ---

foo bar

EOF check <<-"EOF" > foo > > > bar > > baz ---

foo

bar baz

EOF check <<-"EOF" > foo > >> bar >> baz ---

foo

bar baz

EOF check <<-"EOF" > code blocks > in blockquotes ---
code blocks
in blockquotes
EOF check <<-"EOF" > A list within a blockquote: > > * asterisk 1 > * asterisk 2 > * asterisk 3 ---

A list within a blockquote:

EOF check <<-"EOF" foo&bar 1 < 2 2 > 1 ---

foo&bar

1 < 2

2 > 1

EOF check <<-"EOF" foo bar ---

foo bar

EOF check <<-"EOF" \\ \` \* \_ \{ \} \[ \] \( \) \> \# \. \! \+ \- ---

\

`

*

_

{

}

[

]

(

)

>

#

.

!

+

-

EOF check <<-"EOF" `This shouldn't be escaped: \*` ---

This shouldn't be escaped: \*

EOF check <<-"EOF" ``` This shouldn't be escaped: \* ``` ---

This shouldn't be escaped: \*
EOF check <<-"EOF" Spimple link: [foo](/bar) Link with title: [foo](/bar "baz") ---

Spimple link: foo

Link with title: foo

EOF echo echo "All tests passed"