Skip to content

Instantly share code, notes, and snippets.

@MattiooFR
Last active November 12, 2021 13:55
Show Gist options
  • Save MattiooFR/64874ed6d586a19adfe5dce8706844c3 to your computer and use it in GitHub Desktop.
Save MattiooFR/64874ed6d586a19adfe5dce8706844c3 to your computer and use it in GitHub Desktop.
This little program convert all the indented code blocks in a markdown file to a fenced code blocks style.
"""
This little program convert all the indented code blocks
in a markdown file to a fenced code blocks style.
It adds python language highlight argument by default.
This program is argely inspired by the php gist made by tobice
https://gist.github.com/tobice/1beab9616bb48e56ac1f1eb48c11dd97
-p, --path : path to the file to convert
-t, --type : change the language highlight for the code blocks
default is python
Returns:
It is converted in the file itself.
"""
import re
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument(
"-p",
"--path",
help='file path to convert',
type=str)
parser.add_argument(
"-t",
"--type",
help='language type for the code blocks',
type=str,
default="python")
args = parser.parse_args()
def isCode(line, indentedCode):
"""Check if the line is code by looking for indent at the beginning
Arguments:
line {string} -- line is a string of one line the markdown file being processed
Returns:
boolean -- Returns True or False
"""
if indentedCode:
return re.search("^ ", line)
else:
return re.search("^ ", line)
def removeIndent(line):
"""Remove the indentation of the line
Arguments:
line {string} -- line is a string of one line the markdown file being processed
Returns:
string -- Returns the line without indentation
"""
return re.sub("^ ", "", line)
def isEmpty(line):
"""Check if the line is empty
Arguments:
line {string} -- line is a string of one line the markdown file being processed
Returns:
boolean -- Returns True or False
"""
return line == "" or re.search("^[\r\n]$", line)
with open(args.path, 'r', encoding='utf8') as f:
inCode = False
indentedCode = False
output = []
for line in f: # process each line in the markdown file one by one
if inCode: # things to do when an indented code part is detected
if isEmpty(line): # if an empty line is detected we just append it to the output array
output.append(line)
# if the line was not empty and is code, append it without the indent
elif isCode(line, indentedCode):
output.append(removeIndent(line))
else: # if the line is not empty, and not code
# output.append("\n") # append a new line
# change the last line to be the end of the code block
if indentedCode:
output[len(output)-1] = " ```\n"
else:
output[len(output)-1] = "```\n"
output.append("\n")
output.append(line)
inCode = False
indentedCode = False
else:
if isCode(line, indentedCode): # first line of code block detected
# append a fenced line with code type
if re.search("^ ", line):
indentedCode = True
output.append(" ```" + args.type + "\n")
else:
indentedCode = False
output.append("```" + args.type + "\n")
output.append(removeIndent(line))
inCode = True
else:
output.append(line)
with open(args.path, 'w') as f:
f.write("".join(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment