Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to post code snippets (especially Python) into comment systems that don't preserve whitespace
#!/usr/bin/env python
# How to post code snippets (especially Python) into comment systems
# that don't preserve whitespace.
# Save this as a script called 'convert_leading_spaces_to_nonbreaking'
# in your PATH and do "chmod +x" on it.
#
# In Linux, you can then use it by installing xsel and doing:
#
# xsel | convert_leading_spaces_to_nonbreaking | xsel
#
# after you've copied the text to the clipboard.
#
# For Mac, it is something like:
#
# pbpaste | convert_leading_spaces_to_nonbreaking | pbcopy
from __future__ import unicode_literals
import sys
def fix_line(l):
if not l:
return l
if l[0] == " ":
return "\u00a0" + fix_line(l[1:])
else:
return l
if __name__ == '__main__':
for l in sys.stdin.readlines():
sys.stdout.write(fix_line(l.decode('utf-8')).encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.