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.pandoc_images.py: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[1], '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 ''))Example (text.md):This is some text.![This is an image.](image.png)### This is a header.Running 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?
Converting Pandoc Markdown images from captioned to inline
python;markdown;pdf
Instead of a ternary, use the or syntax for setting your booleans here. If is_first_line is True then True is returned, if it's not then not lines[index - 1] is evaluated and the result of that is returned whether it's True or False.preceding_blank = is_first_line or not lines[index - 1]But since you're setting is_first_line one line before and never using it again, I'd even fold that into this expression.preceding_blank = index == 0 or not lines[index - 1]I'd make both the same changes with is_last_line. Also I would substitute index for i, since i is idiomatic for index anyway and will save some characters.One last thing that you may want, adding .strip() to not line will strip out any whitespace on the line and make sure that even an empty whitespace line will be considered False because any character in a string will make it evaluate as True. This may or may not be beneficial to you, as whitespace could be meaningful.with open(sys.argv[1], 'r') as markdown:    lines = markdown.read().splitlines()    for i, line in enumerate(lines):        precedingBlank = i == 0 or not lines[i - 1].strip()        followingBlank = i == len(lines) - 1 or not lines[i + 1].strip()        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 ''))