After writing a rather long document in Markdown and using pandoc to convert it to a PDF, I found, to my dismay, that many of the images were out of place, and that they all had their alternate text proudly displayed underneath them as captions. My document is rather instructional, so this rearrangement was harmful to its readability.
I eventually found a way to display the images as inline. I still wanted to write the document in standard Markdown, though, so I wrote a Python script to convert all the standalone images in a document to this inline form.
import sys # Convert standalone images in standard Markdown # to inline images in Pandoc's Markdown # (see http://pandoc.org/README.html#images) with open(sys.argv, 'r') as markdown: lines = markdown.read().splitlines() for index, line in enumerate(lines): is_first_line = index == 0 preceding_blank = True if is_first_line else not lines[index - 1] is_last_line = index == len(lines) - 1 following_blank = True if is_last_line else not lines[index + 1] is_standalone = preceding_blank and following_blank is_image = line.startswith('![') and '](' in line and line.endswith(')') print(line + ('\\\n' if is_standalone and is_image else ''))
This is some text. ![This is an image.](image.png) ### This is a header.
python3 pandoc_images.py text.md would produce:
This is some text. ![This is an image.](image.png)\ ### This is a header.
It seems like a lot of mess (
enumerate, bounds checking, etc.) for such a simple job, though. Is there any way I can improve any of this code?