fletcher / peg-multimarkdown

An implementation of MultiMarkdown in C, using a PEG grammar - a fork of jgm's peg-markdown. No longer under active development - see MMD 5.
Other
525 stars 55 forks source link

% inside url breaks the resulting latex #119

Closed maparent closed 11 years ago

maparent commented 12 years ago

Multimarkdown quotes the URL in the footnote; if the URL contains a % (among others) we get a % in a href in a footnote, which is known to cause issues: http://tex.stackexchange.com/questions/12855/getting-those-signs-in-the-footnote The easy solution is to add bigfoot, but it does not play nice with babel. The more annoying solution is to escape those characters: %#!^&

Here is simple code to do so:

diff --git a/markdown_output.c b/markdown_output.c
index 441cce9..33fa7a8 100644
--- a/markdown_output.c
+++ b/markdown_output.c
@@ -46,6 +46,7 @@ static void print_html_string(GString *out, char *str, bool obfuscate);
 static void print_html_element_list(GString *out, element *list, bool obfuscate);
 static void print_html_element(GString *out, element *elt, bool obfuscate);
 static void print_latex_string(GString *out, char *str);
+static void print_latex_string_url(GString *out, char *str);
 static void print_latex_element_list(GString *out, element *list);
 static void print_latex_element(GString *out, element *elt);
 static void print_groff_string(GString *out, char *str);
@@ -814,6 +815,26 @@ static void print_latex_string(GString *out, char *str) {
     }
 }

+/* print_latex_string_url - print string, minimal escaping for URL inside footnotes */
+static void print_latex_string_url(GString *out, char *str) {
+    char *tmp;
+    while (*str != '\0') {
+        switch (*str) {
+          case '$': case '%': case '!':
+          case '&': case '_': case '#':
+            g_string_append_printf(out, "\\%c", *str);
+            break;
+        case '^':
+            g_string_append_printf(out, "\\^{}");
+            break;
+        default:
+            g_string_append_c(out, *str);
+        }
+    str++;
+    }
+}
+
+
 static void print_latex_endnotes(GString *out) {
     GSList *note;
     element *note_elt;
@@ -931,7 +952,9 @@ static void print_latex_element(GString *out, element *elt) {
             print_latex_element_list(out, elt->contents.link->label);
             g_string_append_printf(out, "}");
             if ( no_latex_footnote == FALSE ) {
-                g_string_append_printf(out, "\\footnote{\\href{%s}{", elt->contents.link->url);
+                g_string_append(out, "\\footnote{\\href{");
+                print_latex_string_url(out, elt->contents.link->url);
+                g_string_append(out, "}{");
                 print_latex_string(out, elt->contents.link->url);
                 g_string_append_printf(out, "}}");
             }
fletcher commented 12 years ago

Can you send me an example URL that doesn't work?

http://example.com/what%is

works just fine.

maparent commented 12 years ago

Sorry I dropped the ball on this.

This is an example with your URL.

Latex Input:    mmd-article-header
Latex Input:    mmd-article-begin-doc

This is a test with a footnote[^fn] and [a url](http://example.com/what%is).

[^fn]: This is the footnote with the [same url](http://example.com/what%is).

Here is the resulting .tex file with (github tip) stock multimarkdown -t latex

\input{mmd-article-header}
\input{mmd-article-begin-doc}
This is a test with a footnote\footnote{This is the footnote with the \href{http://example.com/what%is}{same url}\footnote{\href{http://example.com/what\%is}{http:/\slash example.com\slash what\%is}}.} and \href{http://example.com/what%is}{a url}\footnote{\href{http://example.com/what\%is}{http:/\slash example.com\slash what\%is}}.
\end{document}

The first usage of the URL works just fine, as you pointed out; but the one inside the footnote confuses latex. Applying pdflatex to this tex file fails thus:

Runaway argument?
{This is the footnote with the \href {http://example.com/what\end {do\ETC.
! File ended while scanning use of \@footnotetext.
<inserted text> 
                \par 

This problem is solved by my patch. I hope this is clearer.

fletcher commented 11 years ago

Can you see if this works properly in MMD-4? I'm still not clear on what you believe the proper output to be. I don't get any failures to parse either way, and none of the links with a "%" in them seem active in PDFs viewed in the "Preview" app.

maparent commented 11 years ago

OK... first I cannot reproduce my bug, i.e. I get exactly the same behaviour as you do. Might be due to updates to pdflatex on both our machines. To be clearer than I was: my patch changes the output so that both instances of \href{http://example.com/what%is} became \href{http://example.com/what\%is}, which used to make a difference to parsing. I do not remember whether the links used to work, I was more worried about parsing and printing. I am fine with the bug being closed at this point.

fletcher commented 11 years ago

If it comes up again, let me know.

Thanks for the reply.

F-