Migrate Bootstrap 4 to 5 via RegEx
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
# table for regex replacements | |
tabRegEx=[ | |
[r"ml(-\d)", r"ms\1"], | |
[r"mr(-\d)", r"me\1"], | |
[r"pl(-\d)", r"ps\1"], | |
[r"pr(-\d)", r"pr\1"], | |
[r"data-toggle=([\"'](?:collapse|dropdown|tab)[\"'])", r"data-bs-toggle=\1"], | |
[r"data-target=", r"data-bs-target="], | |
["float-left", "float-start"], | |
["float-right", "float-end"], | |
["text-left", "text-start"], | |
["text-right", "text-end"], | |
["border-left", "border-start"], | |
["border-right", "border-end"], | |
["text-monospace", "font-monospace"], | |
["no-gutters", "g-0"], | |
] | |
def replaceText(text): | |
for regex in tabRegEx: | |
#match = re.search(r"(?<=\W)"+regex[0]+r"(?=\W)", text) | |
#if match: print(regex[0], match[0]) | |
text = re.sub(r"(?<=\W)"+regex[0]+r"(?=\W)", regex[1], text) | |
return text | |
def processFile(filename): | |
with open(filename, 'r') as file: | |
text = file.read() | |
text = replaceText(text) | |
if text=="": return # abbort on error | |
with open(filename, 'w') as file: | |
file.write(text) | |
import sys | |
import re | |
# read all files and replace | |
for filename in sys.argv[1:]: | |
processFile(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment