Skip to content

Instantly share code, notes, and snippets.

@devtooligan
Created March 18, 2025 20:35
Show Gist options
  • Save devtooligan/9801db5b668d3c6468311a45ee6589f8 to your computer and use it in GitHub Desktop.
Save devtooligan/9801db5b668d3c6468311a45ee6589f8 to your computer and use it in GitHub Desktop.
Convert string to .md
import sys
import re
def convert_to_markdown(input_path):
try:
with open(input_path, 'r', encoding='utf-8') as input_file:
content = input_file.read()
# Replace escaped newlines with actual newlines
content = content.replace('\\n', '\n')
# Split into lines and process
lines = content.split('\n')
markdown_lines = []
in_code_block = False
for i, line in enumerate(lines):
stripped = line.strip()
# Detect start of code block (either ``` or ```solidity)
if not in_code_block and (stripped == '```' or stripped == '```solidity'):
in_code_block = True
markdown_lines.append('```solidity')
continue
# Detect end of code block (only with ```)
elif in_code_block and stripped == '```':
in_code_block = False
markdown_lines.append('```')
continue
line = line.replace("\\", '')
markdown_lines.append(line)
# Ensure code block is closed if file ends
if in_code_block:
markdown_lines.append('```')
# Join lines and print to screen
markdown_content = '\n'.join(markdown_lines).strip()
print(markdown_content)
except FileNotFoundError:
print(f"Error: File '{input_path}' not found")
except Exception as e:
print(f"Error during conversion: {str(e)}")
def main():
if len(sys.argv) != 2:
print("Usage: python script.py <input_file_path>")
print("Example: python script.py reentrancy.txt")
sys.exit(1)
input_path = sys.argv[1]
convert_to_markdown(input_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment