示例#1
0
    def parse(self):
        ''' parse buf line by line and return a document object'''
        doc_obj=Struct()
        doc_obj.sect  = []
        doc_obj.lists = []
        doc_obj.exp   = []
        doc_obj.tables= []
        doc_obj.block = []
        doc_order_lst = []
        buf = self.buf
        
        in_sect, in_list, in_table ,in_exp , in_block = [0,0,0,0,0]
        foldlevel, fdl_pre_list, fdl_pre_exp, fdl_pre_table, fdl_pre_block = [
                0,0,0,0,0]

        for i in range(0,len(buf)):
            c_line = buf[i]
            n_line = buf[i+1] if i+1 in buf else ""
            n2_line = buf[i+2] if i+2 in buf else ""
            p_line = buf[i-1] if i-1 in buf else ""
            nnb_i = int(veval("nextnonblank("+ str(i+2) +")"))-1
            if riv_ptn.blank.match(c_line):
                # obj = {'typ':'blk','bgn':i}
                # doc_order_lst.append(obj)
                pass
            elif ( c_line==".." and riv_ptn.blank.match(n_line) ):
                # can stop 
                obj = {'typ':'i_exp','bgn':i}
                doc_order_lst.append(obj)
            # sect
            elif riv_ptn.section.match(c_line) \
                    and riv_ptn.section.match(n2_line) \
                    and riv_ptn.nonblank.match(n_line):
                sect = {'typ':'sec','bgn':i,'title_row':3,'title_type':c_line[0]}
                doc_obj.sect.append(sect)
                doc_order_lst.append(sect)
            elif riv_ptn.section.match(n_line) \
                    and riv_ptn.nonblank.match(c_line)\
                    and c_line != n_line    \
                    and len(c_line) <= len(n_line):
                # parse next line here?
                obj = {'typ':'sec','bgn':i,'title_row':2,'title_type':c_line[0]}
                doc_obj.sect.append(obj)
                doc_order_lst.append(obj)
            # lists
            elif riv_ptn.lists.match(c_line) \
                    and in_exp == 0:
                c_idt = self.indent(i)
                # nnb_idt = self.indent(nnb_i)
                # if c_idt < nnb_idt:
                #     if in_list==0:
                #         in_list = 1
                obj = {'typ':'lst','bgn':i, 'indent':c_idt }
                doc_obj.lists.append(obj)
                doc_order_lst.append(obj)
            elif riv_ptn.exp_m.match(c_line) \
                    and riv_ptn.s_bgn.match(n_line) \
                    and riv_ptn.s_bgn.match(n2_line):
                obj = {'type':'exp','bgn':i }
                doc_obj.exp.append(obj)
                doc_order_lst.append(obj)
            elif riv_ptn.blank.match(c_line) and riv_ptn.blank.match(p_line) \
                    and riv_ptn.S_bgn.match(buf[nnb_i]):
                if in_exp ==1:
                    obj = {'typ':'exp','end':i }
                    doc_obj.exp.append(obj)
                    doc_order_lst.append(obj)
                    in_exp = 0
                if in_list == 1:
                    obj = {'typ':'lst','end':i }
                    in_exp = 0
                    in_list = 0
                    doc_obj.lists.append(obj)
                    doc_order_lst.append(obj)
            elif riv_ptn.table_all.match(c_line) and in_table==0:
                obj = {'typ':'tbl','bgn':i}
                in_table=1
                doc_order_lst.append(obj)
            else: 
                # obj = {'bgn':i}
                # doc_order_lst.append(obj)
                pass
        return [doc_obj,doc_order_lst]
示例#2
0
 def __init__(self):
     self.buf = vim.current.buffer
     self.buf_num = vim.current.buffer.number
     self.shiftwidth = int(veval('&sw'))
 def __init__(self):
     self.buf = vim.current.buffer
     self.buf_num = vim.current.buffer.number
     self.shiftwidth = int(veval('&sw'))
    def parse(self):
        ''' parse buf line by line and return a document object'''
        doc_obj = Struct()
        doc_obj.sect = []
        doc_obj.lists = []
        doc_obj.exp = []
        doc_obj.tables = []
        doc_obj.block = []
        doc_order_lst = []
        buf = self.buf

        in_sect, in_list, in_table, in_exp, in_block = [0, 0, 0, 0, 0]
        foldlevel, fdl_pre_list, fdl_pre_exp, fdl_pre_table, fdl_pre_block = [
            0, 0, 0, 0, 0
        ]

        for i in range(0, len(buf)):
            c_line = buf[i]
            n_line = buf[i + 1] if i + 1 in buf else ""
            n2_line = buf[i + 2] if i + 2 in buf else ""
            p_line = buf[i - 1] if i - 1 in buf else ""
            nnb_i = int(veval("nextnonblank(" + str(i + 2) + ")")) - 1
            if riv_ptn.blank.match(c_line):
                # obj = {'typ':'blk','bgn':i}
                # doc_order_lst.append(obj)
                pass
            elif (c_line == ".." and riv_ptn.blank.match(n_line)):
                # can stop
                obj = {'typ': 'i_exp', 'bgn': i}
                doc_order_lst.append(obj)
            # sect
            elif riv_ptn.section.match(c_line) \
                    and riv_ptn.section.match(n2_line) \
                    and riv_ptn.nonblank.match(n_line):
                sect = {
                    'typ': 'sec',
                    'bgn': i,
                    'title_row': 3,
                    'title_type': c_line[0]
                }
                doc_obj.sect.append(sect)
                doc_order_lst.append(sect)
            elif riv_ptn.section.match(n_line) \
                    and riv_ptn.nonblank.match(c_line)\
                    and c_line != n_line    \
                    and len(c_line) <= len(n_line):
                # parse next line here?
                obj = {
                    'typ': 'sec',
                    'bgn': i,
                    'title_row': 2,
                    'title_type': c_line[0]
                }
                doc_obj.sect.append(obj)
                doc_order_lst.append(obj)
            # lists
            elif riv_ptn.lists.match(c_line) \
                    and in_exp == 0:
                c_idt = self.indent(i)
                # nnb_idt = self.indent(nnb_i)
                # if c_idt < nnb_idt:
                #     if in_list==0:
                #         in_list = 1
                obj = {'typ': 'lst', 'bgn': i, 'indent': c_idt}
                doc_obj.lists.append(obj)
                doc_order_lst.append(obj)
            elif riv_ptn.exp_m.match(c_line) \
                    and riv_ptn.s_bgn.match(n_line) \
                    and riv_ptn.s_bgn.match(n2_line):
                obj = {'type': 'exp', 'bgn': i}
                doc_obj.exp.append(obj)
                doc_order_lst.append(obj)
            elif riv_ptn.blank.match(c_line) and riv_ptn.blank.match(p_line) \
                    and riv_ptn.S_bgn.match(buf[nnb_i]):
                if in_exp == 1:
                    obj = {'typ': 'exp', 'end': i}
                    doc_obj.exp.append(obj)
                    doc_order_lst.append(obj)
                    in_exp = 0
                if in_list == 1:
                    obj = {'typ': 'lst', 'end': i}
                    in_exp = 0
                    in_list = 0
                    doc_obj.lists.append(obj)
                    doc_order_lst.append(obj)
            elif riv_ptn.table_all.match(c_line) and in_table == 0:
                obj = {'typ': 'tbl', 'bgn': i}
                in_table = 1
                doc_order_lst.append(obj)
            else:
                # obj = {'bgn':i}
                # doc_order_lst.append(obj)
                pass
        return [doc_obj, doc_order_lst]
示例#5
0
文件: trans.py 项目: mattn/trans.vim
# coding=utf-8
# Credit: google tranlsate from jiazhoulvke
import urllib,urllib2

from vim import eval as veval
from vim import command as vcmd

headers = { 'User-Agent': veval('g:trans_header_agent') }

google_url = veval('g:trans_google_url')
bing_url = veval('g:trans_bing_url')
bing_appid = veval('g:trans_bing_appid')

def trans_google(word,lang_from,lang_to):
    word=word.replace('\n','')
    rword = urllib.urlencode({'text':word})

    url = google_url+'?client=firefox-a&langpair='+lang_from+'%7c'+lang_to+'&ie=UTF-8&oe=UTF-8&'+rword

    req = urllib2.Request(
        url = url,
        headers = headers
    )
    gtresult = urllib2.urlopen(req)
    resultstr=''
    if gtresult.getcode()==200:
        gtresultstr=gtresult.read()

        po=eval(gtresultstr)
        resultstr=''
        for poi in po['sentences']: