Created
July 17, 2019 05:03
-
-
Save ceshine/6611d8d71badcd02c2c131e0f486be03 to your computer and use it in GitHub Desktop.
A simple script to convert markdown image specification to hugo shortcode (result is automatically copied to clipboard)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import Popen, PIPE | |
def image_markdown_conversion(): | |
try: | |
while True: | |
text = input("Input:").strip() | |
brackets, split_point = 1, 0 | |
description = "" | |
assert len(text) > 5, "wrong format!" | |
assert text[0] == "!", "wrong format!" | |
assert text[1] == "[", "wrong format!" | |
for i, c in enumerate(text[2:]): | |
if c == "[": | |
brackets += 1 | |
elif c == "]": | |
brackets -= 1 | |
if brackets == 0: | |
assert text[i+2+1] == "(", "wrong format!" | |
split_point = i + 2 + 2 | |
description = "".join(text[2:split_point-2]) | |
break | |
parentheses = 1 | |
image_url = "" | |
for i, c in enumerate(text[split_point:]): | |
if c == "(": | |
parentheses += 1 | |
elif c == ")": | |
parentheses -= 1 | |
if parentheses == 0: | |
image_url = text[split_point:split_point+i] | |
break | |
result = "{{{{< figure src=\"{}\" caption=\"{}\" >}}}}".format( | |
image_url, description | |
) | |
p = Popen(['xsel', '-bi'], stdin=PIPE) | |
p.communicate(input=result.encode('utf-8')) | |
print(result) | |
except KeyboardInterrupt: | |
pass | |
if __name__ == "__main__": | |
image_markdown_conversion() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment