/\[QUOTE/ {

printing_quote = 1

l = $0

l = gensub(/\[QUOTE=([^]]*)]/, "Originally posted by \\1:\n> ", 1, l)

l = gensub(/\[QUOTE]/, "> ", 1, l)

if (sub(/\[\/QUOTE]/, "", l)) {

# Special case: quote begins and ends in a single line

printing_quote = 0

}

print(l)

next

}

!printing_quote {

l = $0

# Line in the format "[B]Game Title[/B] - Platform" turns into level 1 header with subtitle

l = gensub(/^\[B]([^[]*)\[\/B] - ([^[]*)/, "# \\1\n* \\2", 1, l)

# Extremely hacky: multiline link in the format

# "[URL=...][B]Game Title[/B] - Platform

# [IMG]...[/IMG][/URL]"

l = gensub(/^\[URL=([^]]*)]\[B]([^[]*)\[\/B] - ([^[]*)$/, "# \\2\n* \\3\n=> \\1", 1, l)

sub(/^\[IMG][^[]*\[\/IMG]\[\/URL]/, "", l)

# Another multiline link thing

# "...[URL=...]Some text

# [IMG]...[/IMG][/URL]"

l = gensub(/\[URL=([^]]*)]([^[]*)$/, "\n=> \\1 \\2", 1, l)

# image will be removed by previous code

# Link lines

l = gensub(/^\[IMG]([^[]*)\[\/IMG]$/, "=> \\1 Link to image", 1, l)

l = gensub(/^\[YOUTUBE]([^[]*)\[\/YOUTUBE]$/, "=> \\1 Youtube video", 1, l)

l = gensub(/^\[URL]([^[]*)\[\/URL]/, "=> \\1", 1, l)

l = gensub(/^\[URL=([^]]*)]([^[]*)\[\/URL]/, "=> \\1 \\2", 1, l)

l = gensub(/^\[URL=([^]]*)]\[IMG][^[]*\[\/IMG]\[\/URL]$/, "=> \\1", 1, l)

# Line in the format "[B][URL]Text[/URL][/B]" turns into a regular link

l = gensub(/^\[B]\[URL]([^[]*)\[\/URL]\[\/B]$/, "=> \\1", 1, l)

# Line in the format "[B]Text[/B]" turns into level 2 header

l = gensub(/^\[B]([^[]*)\[\/B]$/, "## \\1", 1, l)

# Inline formatting

l = gensub(/\[B]([^[]*)\[\/B]/, "*\\1*", "g", l)

l = gensub(/\[I]([^[]*)\[\/I]/, "/\\1/", "g", l)

l = gensub(/\[U]([^[]*)\[\/U]/, "_\\1_", "g", l)

l = gensub(/\[S]([^[]*)\[\/S]/, "~~\\1~~", "g", l)

links_count = 0

while (match(l, /\[URL=([^]]*)]([^[]*)\[\/URL]/, url) > 0) {

links_url[links_count] = url[1]

links_name[links_count] = url[2]

links_count++

l = gensub(/\[URL=[^]]*]([^[]*)\[\/URL]/, "\\1", 1, l)

}

# Inline formatting again, to handle some nested formatting

l = gensub(/\[B]([^[]*)\[\/B]/, "*\\1*", "g", l)

l = gensub(/\[I]([^[]*)\[\/I]/, "/\\1/", "g", l)

l = gensub(/\[U]([^[]*)\[\/U]/, "_\\1_", "g", l)

l = gensub(/\[S]([^[]*)\[\/S]/, "~~\\1~~", "g", l)

# Inline formatting that spans across multiple lines:

# This looks weird in Gemtext, so let's remove it

sub(/\[\/?B]/, "", l)

sub(/\[\/?I]/, "", l)

sub(/\[\/?U]/, "", l)

sub(/\[\/?S]/, "", l)

# Keep the spoiler tags, except replace square brackets

# because they break the rest of this code

gsub(/\[SPOILER]/, "<SPOILER>", l)

gsub(/\[\/SPOILER]/, "</SPOILER>", l)

print(l)

if (links_count > 0) {

for (n = 0; n < links_count; n++) {

print("=>", links_url[n], links_name[n])

}

}

}

printing_quote {

if (sub(/\[\/QUOTE]/, "")) {

printing_quote = 0

}

print(">", $0)

}