From 7e9e959548e665773f7eed388c79ff89d2469d24 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sun, 24 Oct 2010 19:31:06 -0700
Subject: [PATCH] LaTeX & ConTeXt writers: escape [ and ] as {[} and {]}.

This avoids unwanted interpretation as optional arguments
in some contexts, which caused the brackets to silently
disappear!
---
 src/Text/Pandoc/Writers/ConTeXt.hs |  2 ++
 src/Text/Pandoc/Writers/LaTeX.hs   |  2 ++
 tests/writer.context               | 12 ++++++------
 tests/writer.latex                 | 12 ++++++------
 4 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/Text/Pandoc/Writers/ConTeXt.hs b/src/Text/Pandoc/Writers/ConTeXt.hs
index 395bc2d30..a3a30f0a0 100644
--- a/src/Text/Pandoc/Writers/ConTeXt.hs
+++ b/src/Text/Pandoc/Writers/ConTeXt.hs
@@ -92,6 +92,8 @@ escapeCharForConTeXt ch =
     '#'    -> "\\#"
     '<'    -> "\\letterless{}"
     '>'    -> "\\lettermore{}"
+    '['    -> "{[}"
+    ']'    -> "{]}"
     '_'    -> "\\letterunderscore{}"
     '\160' -> "~"
     x      -> [x]
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index 720c00ac8..5fa345760 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -107,6 +107,8 @@ stringToLaTeX = escapeStringUsing latexEscapes
                        , ('|', "\\textbar{}")
                        , ('<', "\\textless{}")
                        , ('>', "\\textgreater{}")
+                       , ('[', "{[}")  -- to avoid interpretation as
+                       , (']', "{]}")  -- optional arguments
                        , ('\160', "~")
                        ]
 
diff --git a/tests/writer.context b/tests/writer.context
index 2c53fde5c..e8fc17114 100644
--- a/tests/writer.context
+++ b/tests/writer.context
@@ -743,9 +743,9 @@ Left brace: \letteropenbrace{}
 
 Right brace: \letterclosebrace{}
 
-Left bracket: [
+Left bracket: {[}
 
-Right bracket: ]
+Right bracket: {]}
 
 Left paren: (
 
@@ -795,7 +795,7 @@ Foo \useURL[14][/url/][][bar]\from[14].
 
 Foo \useURL[15][/url/][][bar]\from[15].
 
-With \useURL[16][/url/][][embedded [brackets]]\from[16].
+With \useURL[16][/url/][][embedded {[}brackets{]}]\from[16].
 
 \useURL[17][/url/][][b]\from[17] by itself should be a link.
 
@@ -805,7 +805,7 @@ Indented \useURL[19][/url][][twice]\from[19].
 
 Indented \useURL[20][/url][][thrice]\from[20].
 
-This should [not][] be a link.
+This should {[}not{]}{[}{]} be a link.
 
 \starttyping
 [not]: /url
@@ -886,10 +886,10 @@ footnote (as with list items).
 If you want, you can indent every line, but you can also be lazy
 and just indent the first line of each block.}
 This should {\em not} be a footnote reference, because it contains
-a space.[\letterhat{}my note] Here is an inline note.
+a space.{[}\letterhat{}my note{]} Here is an inline note.
 \footnote{This is {\em easier} to type. Inline notes may contain
 \useURL[31][http://google.com][][links]\from[31] and \type{]}
-verbatim characters, as well as [bracketed text].}
+verbatim characters, as well as {[}bracketed text{]}.}
 
 \startblockquote
 Notes can go in quotes.
diff --git a/tests/writer.latex b/tests/writer.latex
index 1621a4844..0d64e544f 100644
--- a/tests/writer.latex
+++ b/tests/writer.latex
@@ -704,9 +704,9 @@ Left brace: \{
 
 Right brace: \}
 
-Left bracket: [
+Left bracket: {[}
 
-Right bracket: ]
+Right bracket: {]}
 
 Left paren: (
 
@@ -756,7 +756,7 @@ Foo \href{/url/}{bar}.
 
 Foo \href{/url/}{bar}.
 
-With \href{/url/}{embedded [brackets]}.
+With \href{/url/}{embedded {[}brackets{]}}.
 
 \href{/url/}{b} by itself should be a link.
 
@@ -766,7 +766,7 @@ Indented \href{/url}{twice}.
 
 Indented \href{/url}{thrice}.
 
-This should [not][] be a link.
+This should {[}not{]}{[}{]} be a link.
 
 \begin{verbatim}
 [not]: /url
@@ -845,10 +845,10 @@ footnote (as with list items).
 If you want, you can indent every line, but you can also be lazy
 and just indent the first line of each block.}
 This should \emph{not} be a footnote reference, because it contains
-a space.[\^{}my note] Here is an inline note.%
+a space.{[}\^{}my note{]} Here is an inline note.%
 \footnote{This is \emph{easier} to type. Inline notes may contain
 \href{http://google.com}{links} and \verb!]! verbatim characters,
-as well as [bracketed text].}
+as well as {[}bracketed text{]}.}
 
 \begin{quote}
 Notes can go in quotes.%