ThaiTux.info

- Python-Web Demo Page
หน้านี้เป็นหน้าทดสอบ อาจเปลี่ยนเนื้อหาได้ทุกวันครับ
   
Python demo page
Source code of Python word:

Imported/child module list

cgi    

Source code

#!/usr/bin/env python
""" Syntax Highlighter Module
  Support: python, javascript, bash
  """

import cgi
#import dcgi

_alpha = '_'
_lang_list = ["python", "javascript", "bash", "php", "sql"]

def get_lang_list():
  return _lang_list

def get_keyword_dict(lang="python"):
  """\
  Keyword list
  kw1: RESERVED WORDS
  kw2: BUILTINS
  kw3: STANDARD LIBRARY
  kw4: SPECIAL METHODS
  sym: SYMBOLS
  cmm: ONE LINE COMMENT
  mcm: MULTILINE COMMENT
  str: STRING
  mst: MULTILINE STRING
  num: NUMBER
  css: CSS STYLE 
  """
  #----- ALL LANGUAGE IN _lang_list MUST BE NAME _lang.py AND SAVE INTO ./_sh/
  if lang in _lang_list:
    i = _lang_list.index(lang)
    exec "import _sh._%s as _dict" % (lang)
    return _dict.lang_dict

  #-----------------------OTHER----------------------------------
  return { 'kw1':(), 'kw2':(), 'kw3':(), 'kw4':(), 'sym':(), 'cmm':(), 'mcm':(), 'num':(), 'css':()  } 

def get_css_header(lang="python"):
    """get
    .kw1 {color: #cc6600;font-weight:bold;}    /* Reserved */
    """
    return '<style type="text/css">\n'+get_keyword_dict(lang).get('css')+'</style>\n'

def get_css_header_with_prefix(lang="python"):
    """get
    .python .kw1 {color: #cc6600;font-weight:bold;}    /* Reserved */
    """
    css_txt = get_keyword_dict(lang).get('css')
    new_txt = ""
    for line in css_txt.split('\n'):
      line = line.strip()
      if line != "" and line[0]==".":
        new_txt += ".%s " % (lang)+line+"\n"
      else:
        new_txt += line+"\n"
    return '<style type="text/css">\n'+new_txt+'</style>\n'

def insert_tag(phrase="", css_class=""):
    if phrase=="" or css_class=="":
      return phrase
    return '<span class="%s">%s</span>' % (css_class, phrase)

def insert_begin_tag(phrase="", css_class=""):
    if phrase=="" or css_class=="":
      return phrase
    return '<span class="%s">%s' % (css_class, phrase)

def process_line(line="", qstat=""):
    """Add tag <span> in line"""
    #kwdict=get_keyword_dict(lang)

    ######################
    #print 'line,qstat=---%s---%s---' % (line, qstat)
    ####################
    newline=""
    #CHECK PREVIOUS MULTILINE STRING/COMMENT
    if qstat!="":
      if qstat in line:
        temp1,temp2 = line.split(qstat,1)
        newline = newline+cgi.escape(temp1)+qstat+"</span>"
        qstat = ""
        if temp2=="":
          return (newline,qstat)
        else:
          templine,qstat = process_line(temp2, qstat)
          newline = newline+templine
          return newline,""
      else:
        return cgi.escape(line),qstat

    #CHECK SINGLE/MULTILINE STRING/COMMENT
    #FIND FIRST
    i=100000000
    qmin = qmin_end = ""
    qmark_beg = qmark_end = ""
    css=""
    for css_class in ('mcm', 'mst', 'cmm', 'str' ):
      qmark_list=kwdict.get(css_class)
      #CHECK BEGIN TAG/ END TAG
      if len(qmark_list)>0:
        for q in qmark_list:
          if type(q) in [ type([]), type(()) ]:
            qmark_beg, qmark_end = q
          else:
            qmark_beg = qmark_end = q

          if qmark_beg in line:
            j = line.index(qmark_beg)
            if j<i:
              qmin = qmark_beg
              qmin_end = qmark_end
              css=css_class
              i=j
    #IF FOUND
    if qmin!="": 
      qmark=qmin
      temp1,temp2 = line.split(qmark,1)
      qstat = qmin_end
      if temp1=="":
        newtemp1,temp = "",""
      else:
        newtemp1,temp = process_line(temp1,"")
        

      #IF SINGLELINE STRING/COMMENT
      if css=='cmm':
        newtemp2,qstat = cgi.escape(temp2),""
      else:
        newtemp2,qstat = process_line(temp2,qstat)

      newline = newline+newtemp1+insert_begin_tag(qmark,css)+newtemp2
      if css=='cmm':
        return newline+'</span>',""
      else:
        return newline,qstat

    #CHECK THE REST ALL KEYWORD
    #kw1: RESERVED WORDS
    #kw2: BUILTINS
    #kw3: STANDARD LIBRARY
    #kw4: SPECIAL METHODS
    #sym: SYMBOLS
    #num: NUMBER
    qmark_beg = qmark_end = ""
    for css_class in ('kw1', 'kw2', 'kw3', 'kw4', 'sym', 'num'):
      qmark_list=kwdict.get(css_class)
      if len(qmark_list)>0:
        for q in qmark_list:
          if type(q) in [ type([]), type(()) ]:
            qmark_beg, qmark_end = q
          else:
            qmark_beg = qmark_end = q

          qmark = qmark_beg
          if qmark in line:
            passcheck=False
            pos=line.index(qmark)
            ll=len(line)
            lq=len(qmark)
            if css_class in ('sym'):
                passcheck=True
            elif pos==0:  # BEGINNING OF LINE
              if ll>lq:
                if not (line[lq].isalpha() or line[lq].isdigit() or (line[lq] in _alpha)):
                  passcheck=True
              else:
                passcheck=True
            elif pos==(ll-lq):    # END OF LINE
              if not (line[ll-lq-1].isalpha() or line[ll-lq-1].isdigit() or (line[ll-lq-1] in _alpha)):
                passcheck=True
            else:
              if ( not (line[pos-1].isalpha() or line[pos-1].isdigit() or (line[pos-1] in _alpha))) \
                   and ( not (line[pos+lq].isalpha() or line[pos+lq].isdigit() or \
                   (line[pos+lq] in _alpha))): 
                passcheck=True

            if passcheck:
              temp1,temp2 = line.split(qmark, 1)
              qstat=""
              newtemp1,qstat = process_line(temp1, qstat)
              #???????????????
              qstat=""
              newtemp2,qstat = process_line(temp2, "")
              newline = newline+newtemp1+insert_tag(cgi.escape(qmark), css_class)+newtemp2 
              return (newline, qstat)
    #
    return (line, qstat)


def syntax_highlight_str(lang="python",source=""):
  """\
  Return syntax highlight string
  Copy from GeSHI - http://qbnz.com/highlighter/
  """
  global kwdict
  kwdict=get_keyword_dict(lang)
  #
  # BEGIN MAIN FUNCTION syntax_highlight_str
  line_list=source.split('\n')
  sh_list=[]
  qstat=''   # STATUS OF MULTILINE STRING OR MULTILINE COMMENT
  for line in line_list:
    newline,qstat = process_line(line, qstat)
    sh_list.append(newline)
  newline=""
  for line in sh_list:
    if newline=="":
      newline=newline+line
    else:
      newline=newline+'\n'+line
  
  return newline


def get_splitter(process="pre",txt=""):
  """get_splitter("pre",txt) -> check tag <pre lang="python"
  get_splitter("post",txt) -> check tag <pre class="python"
  """
  if process=="pre":
    tag = "lang"
  else:
    tag = "class"
 
  rear_splitter = "</pre>"
  front_splitter = ""
  fs_list = ['<pre %s="%s">' % (tag,i,) for i in _lang_list]
  fs_list = fs_list + ["<pre %s='%s'>" % (tag,i,) for i in _lang_list]
  pos_list = []
  for i in range(len(fs_list)):
    pos = txt.find(fs_list[i])
    if pos>=0:
      pos_list.append((pos,i))

  pos_list.sort()
  if len(pos_list) == 0:
    return False, "", "", ""

  front_splitter = fs_list[pos_list[0][1]]
  lang = front_splitter.strip("<pre %s=" % (tag,)).strip(">").strip('"').strip("'")
  return True, front_splitter, rear_splitter, lang

def get_sh(content=""):
  """get_sh: input with '<pre lang="python">'
             output to '<pre class="python"><span...'
  """
  is_remain = True
  newcontent = ""
  txt = content
  while is_remain and len(txt)>0:
    is_remain, front_splitter, rear_splitter, lang = get_splitter("pre",txt)
    if is_remain:
      front_txt, temp = txt.split(front_splitter,1)
      if rear_splitter in temp:
        pre_txt, txt = temp.split(rear_splitter,1)
      else:
        pre_txt, txt = temp, ""

      newcontent = newcontent + front_txt + '<pre class="%s">' % (lang) + \
                 syntax_highlight_str(lang,pre_txt) + rear_splitter

  return newcontent+txt

def get_all_lang_exist(process="pre", content=""): 
  """get_all_lang_exist("pre","") -> check <pre lang="python"> -> ["python",...]
  get_all_lang_exist("post","") -> check <pre class="python"> -> ["python",...]
  """
  lang_exist = []
  is_remain = True
  txt = content
  while is_remain and len(txt)>0:
    is_remain, front_splitter, rear_splitter, lang = get_splitter(process,txt)
    if is_remain:
      front_txt, temp = txt.split(front_splitter,1)
      if rear_splitter in temp:
        pre_txt, txt = temp.split(rear_splitter,1)
      else:
        pre_txt, txt = temp, ""

      lang_exist.append(lang)

  return lang_exist


if __name__=='__main__':
  print "sh - Syntax highlight module"
  #global kwdict
  #kwdict=get_keyword_dict('python')