Posting code to wordpress

Python, regex

  1. Add <pre> in the beginning of the code
  2. Add </pre> at the very end of the code
  3. If indentation is needed, use &nbsp;
    In this case, 4 backspace is converted into 4 &nbsp;
    (because double tab doesn’t seem working)

.bat (double click) wantedCode > result.txt

# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import re

def main():
    i = 0
    fh = open(sys.argv[1])    
    for line in fh:        	
	if i==0:
            print("<pre>", end="")
        line = re.sub(r"\s\s\s\s", "    ", line)
        #line = re.sub("    ", "\t", line)
        print(line, end="")

    print("</pre>", end="")

if __name__ == "__main__":


print(“&lt;pre&gt;”, end=””) print(“<pre>”, end=””)
print(“&lt;/pre&gt;”, end=””) print(“</pre>”, end=””)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s