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)

main.py wantedCode > result.txt

main.py

# -*- 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="")
        i=i+1        

    print("</pre>", end="")
    fh.close()    

if __name__ == "__main__":
    main()

Additional:

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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