script

 

ทดลองเขียนสคริปต์ช่วยผลิต html กับ bbcode เพื่อลดข้อผิดพลาด

ตั้งชื่อว่า d_genhtbb.py
$ vi d_genhtbb.py

#!/usr/bin/env python
""" Gen HTML code and BB code from text file
  "- " = <li>,                 [li]           : for listing
  "$ " = <code class="green">, [color=green]  : command line for user
  "# " = <code class="blue">,  [color=blue]   : command line for root
  "c " = <pre>,                [code]         : preformat/code block
  "g " = <code class="grey">,  [color=orange] : comment
"""

import sys

def usage(prog):
  test("- test list")
  test("$ test normal user")
  test("a@b:~# test root user")
  test("abcde <<<--- xxx yyy")
  test("c this is code")
  test("g this is comment")
  test("test normal")
  print "Usage: %s file.txt" % prog
  print "Will generate file.txt.html and file.txt.bbcode as above"


def bare_tag(tag):
  """ Determine bare tag from head tag """

  if " " in tag:
    return tag[:tag.find(" ")]
  elif "=" in tag:
    return tag[:tag.find("=")]
  else:
    return tag


def tag_ins(code_type=0, is_del_old=False, is_after=False, line="", d_tag="", tag=""):
  """ Find & replace of d_tag in line """

  if code_type==0: #HTML
    tag_beg="<"+tag+">"
    tag_end="</"+bare_tag(tag)+">"
  else: #BB-CODE
    tag_beg="["+tag+"]"
    tag_end="[/"+bare_tag(tag)+"]"
  
  if d_tag=="": #PLAIN
    return tag_beg+line+tag_end

  if is_del_old: #IS DELETE D_TAG
    return line.replace(d_tag,tag_beg)+tag_end
  else:
    if is_after: #INSERT BEFORE
      return line.replace(d_tag,d_tag+tag_beg)+tag_end
    else: #INSERT AFTER
      return line.replace(d_tag,tag_beg+d_tag).strip()+tag_end
    #
  #


def gen_html(line):
  """ Print html line using function tag_ins \n \
  def tag_ins(code_type=0, is_del_old=False, is_after=False, line="", d_tag="- ", tag="li"):
  """

  if line=="":
    return line
  
  if line[:2] in ("- "):
    return tag_ins(0, True, True, line, "- ", "li")
  elif line[:2] in ("$ "):
    return tag_ins(0, False, True, line, "$ ", 'code class="green"')
  elif line[:2] in ("# "):
    return tag_ins(0, False, True, line, "# ", 'code class="blue"')
  elif line[:2] in ("c "):
    return tag_ins(0, True, True, line, "c ", 'pre')
  elif line[:2] in ("g "):
    return tag_ins(0, True, True, line, "g ", 'code class="grey"')
  elif "~$ " in line:
    return tag_ins(0, False, True, line, "~$ ", 'code class="green"')
  elif "~# " in line:
    return tag_ins(0, False, True, line, "~# ", 'code class="blue"')
  elif "mysql> " in line:
    return tag_ins(0, False, True, line, "mysql> ", 'code class="blue"')
  elif "<<<--- " in line:
    return tag_ins(0, False, True, line, "<<<--- ", 'code class="red"')
  else:
    return tag_ins(0, False, True, line, "", "p")
  

def gen_bb(line):
  """ Print bb-code line using function tag_ins \n \
  def tag_ins(code_type=0, is_del_old=False, is_after=False, line="", d_tag="- ", tag="li"):
  """

  if line == "":
    return line
  
  if line[:2] in ("- "):
    return tag_ins(1, True, True, line, "- ", "li")
  elif line[:2] in ("$ "):
    return tag_ins(1, False, True, line, "$ ", "color=green")
  elif line[:2] in ("# "):
    return tag_ins(1, False, True, line, "# ", "color=blue")
  elif line[:2] in ("c "):
    return tag_ins(1, True, True, line, "c ", 'code')
  elif line[:2] in ("g "):
    return tag_ins(1, True, True, line, "g ", "color=teal")
  elif "~$ " in line:
    return tag_ins(1, False, True, line, "~$ ", "color=green")
  elif "~# " in line:
    return tag_ins(1, False, True, line, "~# ", "color=blue")
  elif "mysql> " in line:
    return tag_ins(1, False, True, line, "mysql> ", "color=blue")
  elif "<<<--- " in line:
    return tag_ins(1, False, True, line, "<<<--- ", 'color=red')
  else:
    return line


def test(line):
  """ Print out test tag """

  print "input =",line
  print "html  =",gen_html(line.strip())
  print "bbcode=",gen_bb(line.strip())
  print 


def main(sys_argv):
  """ Main routine """

  filename=sys_argv[1]
  try:
    fs=open(filename)
  except:
    print "Cannot open %s" % filename
    sys.exit(1)

  try:
    fs_html=open(filename+".html","w")
  except:
    print "Cannot open %s for writing" % filename+".html"
    sys.exit(2)
 
  try:
    fs_bbcode=open(filename+".bbcode","w")
  except:
    print "Cannot open %s for writing" % filename+".bbcode"
    sys.exit(3)

  for line in fs:
    fs_html.write(gen_html(line.strip())+"\n")
    fs_bbcode.write(gen_bb(line.strip())+"\n")
  
  fs.close()
  fs_html.close()
  fs_bbcode.close()
  print "%s -> %s and %s succeed" % (filename, filename+".html", filename+".bbcode")


if __name__=="__main__":
  if len(sys.argv)==1:
    usage(sys.argv[0])
  else:
    main(sys.argv)

เวลาสั่งรันก็ใช้
$ ./d_genhtbb.py x.txt
เขาจะผลิต x.txt.html กับ x.txt.bbcode ออกมา
แต่ยังต้องปรับแต่งด้วยมืออีกเพียบ
แล้วก็มีเรื่องต้องปรับปรุงอีกเยอะ แต่เวลาไม่เอื้อ เอาแค่นี้ก่อนละกัน

ตัวอย่าง ข้อมูลเข้า

- test list
$ test normal user
a@b:~# test root user
abcde <<<--- xxx yyy
c this is code
g this is comment
test normal

ได้ html แบบนี้

  • test list
  • $ test normal user
    a@b:~# test root user
    abcde <<<--- xxx yyy

    this is code

    this is comment

    test normal

    Subscribe to RSS - script