Created
August 7, 2024 22:09
-
-
Save judell/84e208cac171fccb8337385be5f50ed8 to your computer and use it in GitHub Desktop.
gdoc updater replace patterns
This file contains hidden or 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
def replace_patterns(content): | |
requests = [] | |
patterns_and_replacements = [ | |
# Pattern 1: Replace underscores with hyphens in filenames | |
( | |
re.compile(r''' | |
\[image: # Literal "[image:" | |
\s* # Zero or more whitespace characters | |
( # Start of capturing group | |
[a-zA-Z0-9_]+ # One or more alphanumeric characters or underscores | |
) # End of capturing group | |
\] # Literal closing square bracket | |
''', re.VERBOSE), | |
lambda m: f'[image: {m.group(1).replace("_", "-")}]' | |
# Replace underscores with hyphens in the captured filename | |
), | |
# Pattern 2: Remove aws-start-n- from the filename, but keep the rest | |
( | |
re.compile(r''' | |
\[image: # Literal "[image:" | |
\s* # Zero or more whitespace characters | |
aws-start- # Literal "aws-start-" | |
\d+ # One or more digits | |
- # Literal hyphen | |
( # Start of capturing group | |
.+ # One or more of any character (except newline) | |
) # End of capturing group | |
\] # Literal closing square bracket | |
''', re.VERBOSE), | |
lambda m: f'[image: {m.group(1)}]' | |
# Keep only the part after "aws-start-n-" | |
), | |
] | |
for item in content: | |
if 'paragraph' in item: | |
for element in item['paragraph']['elements']: | |
if 'textRun' in element: | |
text = element['textRun']['content'] | |
original_text = text | |
for pattern, replacement in patterns_and_replacements: | |
text = re.sub(pattern, replacement, text) | |
if text != original_text: | |
requests.append({ | |
'replaceAllText': { | |
'containsText': { | |
'text': original_text, | |
'matchCase': True, | |
}, | |
'replaceText': text, | |
} | |
}) | |
return requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment