Created
December 5, 2022 15:58
-
-
Save matthewdeanmartin/c618b8d712ccb8d37aef0c04744da045 to your computer and use it in GitHub Desktop.
ChatGpt Convert markdown to mediawiki
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 markdown_to_mediawiki(input_string: str) -> str: | |
| # Initialize the output string | |
| output_string = input_string | |
| # Convert bold and italic text | |
| output_string = output_string.replace('**', "'''") | |
| output_string = output_string.replace('__', "''") | |
| output_string = output_string.replace('*', "''") | |
| # Convert heading levels | |
| output_string = output_string.replace('#', '=') | |
| # Convert lists | |
| output_string = output_string.replace('- ', '* ') | |
| # Convert links | |
| output_string = re.sub(r'\[(.+?)\]\((.+?)\)', r'[[\2|\1]]', output_string) | |
| # Return the converted string | |
| return output_string | |
| def markdown_tables_to_mediawiki(input_string: str) -> str: | |
| # Split the input string into lines | |
| lines = input_string.split('\n') | |
| # Initialize the output string | |
| output_string = '' | |
| # Iterate through the lines in the input string | |
| for line in lines: | |
| # If the line is a table row, convert it to MediaWiki syntax | |
| if line.startswith('|'): | |
| # Split the line into cells | |
| cells = line.split('|')[1:-1] | |
| # Add MediaWiki syntax for the table row | |
| output_string += '|-\n' | |
| for cell in cells: | |
| output_string += '| {}\n'.format(cell.strip()) | |
| # If the line is not a table row, add it to the output string as-is | |
| else: | |
| output_string += '{}\n'.format(line) | |
| # Return the converted string | |
| return output_string | |
| def markdown_links_to_mediawiki(input_string: str) -> str: | |
| # Use a regular expression to find all Markdown links in the input string | |
| links = re.findall(r'\[(.+?)\]\((.+?)\)', input_string) | |
| # Iterate through the links and replace them with MediaWiki syntax | |
| for link in links: | |
| markdown_link = '[{}]({})'.format(link[0], link[1]) | |
| mediawiki_link = '[[{}|{}]]'.format(link[1], link[0]) | |
| input_string = input_string.replace(markdown_link, mediawiki_link) | |
| # Return the converted string | |
| return input_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment