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=””)

Tabel dan Code snippet di blog

Buat mendandani blog. Sering pake tapi hobi kopas, mending sekalian bikin postingannya aja.

UNTUK TABEL

Menggunakan tag table , tr untuk row, td untuk column, b atau strong untuk bold

<table border=”2″ cellpadding=”5″ cellspacing=”3″><tbody>
    <tr><td><b>Title 1</b></td>
    <td><b>Title 2</b></td>
    <td><b>Title 3</b></td>
</tr>
<tr>
    <td>Content 1</td>
    <td>Content 2</td>
    <td>Content 3</td>
</tr>
</tbody></table>

Hasil

Title 1 Title 2 Title 3
Content 1 Content 2 Content 3

UNTUK CODE SNIPPET

Menggunakan <code> dan atau <pre>

Code 1: only pre

<pre>bla
bla1
bla2</pre>

Code 2: only code

<code>bla
bla1
bla2</code>

Code 3: pre and code

<pre><code>bla
bla1
bla2</code></pre>

Hasil 1: only pre

bla
bla1
bla2

Hasil 2: only code

bla
bla1
bla2

Hasil 3: pre & code

bla
bla1
bla2

oshimai