Created
May 26, 2021 11:36
-
-
Save emareg/cfd15bc6b680600b133fe29ca4a7112e to your computer and use it in GitHub Desktop.
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