-
Notifications
You must be signed in to change notification settings - Fork 2
/
fix_image.py
executable file
·59 lines (46 loc) · 1.47 KB
/
fix_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
""" Markdown file converter for Jekyll web pages
This script reformats markdown files to prepare them to
be hosted in my website.
Author: Uthpala Herath
"""
import argparse
import fileinput
import os
def make_web(args):
"""Fixes image path names."""
file_dest_dir = args.dest + "_posts/"
file_name = file_dest_dir + args.infile
# renaming the image directory path from images to /images
if not args.undo:
with fileinput.FileInput(
file_name,
inplace=True,
) as file:
for line in file:
print(line.replace("images/", "/images/"), end="")
file.close()
if args.undo:
with fileinput.FileInput(
file_name,
inplace=True,
) as file:
for line in file:
print(line.replace("/images/", "images/"), end="")
file.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This script reformats markdown files for web hosting."
)
parser.add_argument("infile", type=str, default=None, help="Input markdown file.")
parser.add_argument(
"-dest",
type=str,
default="/Users/uthpala/Dropbox/git/uthpalaherath.github.io/",
help="root directory of website",
)
parser.add_argument(
"-undo", help="Undo formatting image name for web hosting.", action="store_true"
)
args = parser.parse_args()
make_web(args)