Skip to content

Instantly share code, notes, and snippets.

@spookylukey
Last active August 29, 2015 14:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spookylukey/fc9fa268de67fd19a567 to your computer and use it in GitHub Desktop.
Save spookylukey/fc9fa268de67fd19a567 to your computer and use it in GitHub Desktop.
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