Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping Pandoc from escaping single quotes when converting from HTML to Markdown

If I convert a single quote ' from HTML to Markdown, it is automatically escaped:

 % echo "'" | pandoc -f html -t markdown
 \'

I'd like it to output without the slash, as it makes text with contractions rather much harder to read.

I thought this might be due to the "all_symbols_escapable" option, but it still happens, even when I turn that off:

% echo "'" | pandoc -f html -t markdown-all_symbols_escapable
\'

It isn't a problem, however, for markdown_strict:

% echo "'" | pandoc -f html -t markdown_strict
'

Any suggestions? I'd like to use the default Pandoc markdow with the options tweaked, or report this as a bug if it's not what others expect.

like image 234
kdannyob Avatar asked Dec 08 '18 00:12

kdannyob


People also ask

Can pandoc convert HTML to markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.

How do you use extensions in pandoc?

An extension can be enabled by adding +EXTENSION to the format name and disabled by adding -EXTENSION . For example, --from markdown_strict+footnotes is strict Markdown with footnotes enabled, while --from markdown-footnotes-pipe_tables is pandoc's Markdown without footnotes or pipe tables.

How do I convert markdown to PDF in pandoc?

Generating PDF from Markdown with Pandoc There are actually two steps involved in converting a Markdown file to a PDF file: The Markdown source file is converted to a LaTeX source file. Pandoc invokes the pdflatex or xelatex or other TeX command and converts the . tex source file to a PDF file.


1 Answers

Escaping is related to pandoc's smart extensions. This extension converts single quotes to the typographically correct opening/closing single quote or apostrophe when appropriate. This becomes most clear when looking at HTML output that uses only ASCII characters:

% echo "'hello'" | pandoc -f markdown -t html --ascii
<p>&lsquo;hello&rsquo;</p>

% echo "let's" | pandoc -f markdown -t html --ascii
<p>let&rsquo;s</p>

This smart treatment of quotes can be disabled on a per-case basis by escaping the character

% echo "let\'s" | pandoc -f markdown -t html --ascii
<p>let's</p>

or by disabling the smart extension for markdown:

% echo "let's" | pandoc -f markdown-smart -t html --ascii
<p>let's</p>

So whenever pandoc sees a ' character in HTML, it assumes that this character was chosen intentionally over the more correct single quote, and thus ensures that it won't be treated in a "smart" way when read back from Markdown.

The solution is thus to tell pandoc that it should ignore these details and will write Markdown as if it would not be subjected to the smart treatment of quotes:

% echo "'" | pandoc -f html -t markdown-smart
'

The smart extension is already disabled when using markdown_strict, which is why you got the desired behavior in that case.

like image 139
tarleb Avatar answered Oct 23 '22 14:10

tarleb