Changeset 576:b2c7e272e075 in documentViewer


Ignore:
Timestamp:
Oct 17, 2012, 2:36:13 PM (12 years ago)
Author:
casties
Branch:
default
Message:

new w-tag solution with etree. search works now.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • MpiwgXmlTextServer.py

    r575 r576  
    99import urlparse
    1010import base64
     11
     12from datetime import datetime
    1113
    1214from SrvTxtUtils import getInt, getText, getHttpData
     
    187189       
    188190        logging.debug("getTextPage mode=%s, pn=%s"%(mode,pn))
     191        startTime = datetime.now()
    189192        # check for cached text -- but ideally this shouldn't be called twice
    190193        if pageinfo.has_key('textPage'):
     
    209212        if normMode == 'regPlusNorm':
    210213            normMode = 'norm'
    211                        
     214       
     215        # TODO: this should not be necessary when the backend is fixed               
     216        textParams['normalization'] = normMode
     217       
    212218        if not mode:
    213219            # default is dict
     
    241247        if 'dict' in modes:
    242248            textmode = 'dict'
    243             textParams['mode'] = 'tokenized'
    244249            textParams['outputFormat'] = 'html'
    245250        elif 'xml' in modes:
    246251            textmode = 'xml'
    247             textParams['mode'] = 'untokenized'
    248252            textParams['outputFormat'] = 'xmlDisplay'
    249             textParams['normMode'] = 'orig'
     253            normMode = 'orig'
    250254        elif 'gis' in modes:
    251255            #FIXME!
     
    254258            # text is default mode
    255259            textmode = 'plain'
    256             textParams['mode'] = 'untokenized'
    257260            textParams['outputFormat'] = 'html'
    258261       
     
    273276                # add textmode and normMode classes
    274277                pagediv.set('class', 'text %s %s'%(textmode, normMode))
    275                 #self._processWTags(textmode, normMode, pagediv)
     278                self._processWTags(textmode, normMode, pagediv)
    276279                #self._processPbTag(pagediv, pageinfo)
    277280                self._processFigures(pagediv, docinfo)
     
    288291                        if linkurl.path.endswith('GetDictionaryEntries'):
    289292                            #TODO: replace wordInfo page
    290                             # is dictionary link - change href (keeping parameters)
    291                             #l.set('href', href.replace('http://mpdl-proto.mpiwg-berlin.mpg.de/mpdl/interface/lt/wordInfo.xql','%s/template/viewer_wordinfo'%viewerurl))
    292293                            # add target to open new page
    293294                            l.set('target', '_blank')
    294                                                          
    295                         elif href.startswith('#note-'):
    296                             # note link FIXME!
    297                             l.set('href', href.replace('#note-',"%s#note-"%selfurl))
    298295                       
    299296                if punditMode:
    300297                    self._addPunditAttributes(pagediv, pageinfo, docinfo)
    301                    
    302                 return serialize(pagediv)
     298                 
     299                s = serialize(pagediv)
     300                logging.debug("getTextPage done in %s"%(datetime.now()-startTime))   
     301                return s
    303302           
    304303        # xml mode
     
    346345        """selects the necessary information from w-spans and removes the rest from pagediv"""
    347346        logging.debug("processWTags(textMode=%s,norm=%s,pagediv"%(repr(textMode),repr(normMode)))
     347        startTime = datetime.now()
    348348        wtags = pagediv.findall(".//span[@class='w']")
    349349        for wtag in wtags:
    350             text = None
    351             attr = None
    352350            if textMode == 'dict':
    353                 # take a-tag and matching child
    354                 attr = wtag.find('a').items()
    355                 text = wtag.find("a/span[@class='%s']"%normMode).text
     351                # delete non-a-tags
     352                wtag.remove(wtag.find("span[@class='nodictionary orig']"))
     353                wtag.remove(wtag.find("span[@class='nodictionary reg']"))
     354                wtag.remove(wtag.find("span[@class='nodictionary norm']"))
     355                # delete non-matching children of a-tag and suppress remaining tag name
     356                atag = wtag.find("a[@class='dictionary']")
     357                if normMode == 'orig':
     358                    atag.remove(atag.find("span[@class='reg']"))
     359                    atag.remove(atag.find("span[@class='norm']"))
     360                    atag.find("span[@class='orig']").tag = None
     361                elif normMode == 'reg':
     362                    atag.remove(atag.find("span[@class='orig']"))
     363                    atag.remove(atag.find("span[@class='norm']"))
     364                    atag.find("span[@class='reg']").tag = None
     365                elif normMode == 'norm':
     366                    atag.remove(atag.find("span[@class='orig']"))
     367                    atag.remove(atag.find("span[@class='reg']"))
     368                    atag.find("span[@class='norm']").tag = None
     369                   
    356370            else:
    357                 # take matching child
    358                 text = wtag.find("span[@class='nodictionary %s']"%normMode).text
     371                # delete a-tag
     372                wtag.remove(wtag.find("a[@class='dictionary']"))
     373                # delete non-matching children and suppress remaining tag name
     374                if normMode == 'orig':
     375                    wtag.remove(wtag.find("span[@class='nodictionary reg']"))
     376                    wtag.remove(wtag.find("span[@class='nodictionary norm']"))
     377                    wtag.find("span[@class='nodictionary orig']").tag = None
     378                elif normMode == 'reg':
     379                    wtag.remove(wtag.find("span[@class='nodictionary orig']"))
     380                    wtag.remove(wtag.find("span[@class='nodictionary norm']"))
     381                    wtag.find("span[@class='nodictionary reg']").tag = None
     382                elif normMode == 'norm':
     383                    wtag.remove(wtag.find("span[@class='nodictionary orig']"))
     384                    wtag.remove(wtag.find("span[@class='nodictionary reg']"))
     385                    wtag.find("span[@class='nodictionary norm']").tag = None
    359386               
    360             if text:
    361                 # replace wtag by new content
    362                 logging.debug("new w-tag attr=%s text=%s"%(attr,text))
    363                 wtag.clear()
    364                    
    365                 if attr:
    366                     # make dictionary link
    367                     wtag.tag = 'a'
    368                     wtag.attrib.update(dict(attr))
    369                    
    370                 # text content
    371                 wtag.text = text
    372                
     387            # suppress w-tag name
     388            wtag.tag = None
     389           
     390        logging.debug("processWTags in %s"%(datetime.now()-startTime))
    373391        return pagediv
    374392       
     
    410428    def _processFigures(self, pagediv, docinfo):
    411429        """processes figure-tags"""
    412         divs = pagediv.findall(".//span[@class='figure']")
     430        # unfortunately etree can not select class.startswith('figure')
     431        divs = pagediv.findall(".//span[@class]")
    413432        scalerUrl = docinfo['digilibScalerUrl']
    414433        viewerUrl = docinfo['digilibViewerUrl']
    415434        for d in divs:
     435            if not d.get('class').startswith('figure'):
     436                continue
     437           
    416438            try:
    417439                a = d.find('a')
     
    485507            dom = ET.fromstring(pagexml)
    486508            # page content is currently in multiple <td align=left>
    487             alldivs = dom.findall(".//td[@align='left']")
     509            alldivs = dom.findall(".//tr[@class='hit']")
    488510            for div in alldivs:
     511                # change tr to div
     512                div.tag = 'div'
     513                # change td to span
     514                for d in div.findall('td'):
     515                    d.tag = 'span'
     516                   
    489517                # TODO: can we put etree in the session?
    490518                results.append(div)
     
    517545            start = (pn - 1) * size
    518546
    519         fullresult = ET.fromstring(resultxml)
    520        
    521         if fullresult is not None:
     547        #fullresult = ET.fromstring(resultxml)
     548        #fullresult = resultxml
     549        #logging.debug("resultxml=%s"%repr(resultxml))
     550       
     551        if resultxml is not None:
    522552            # paginate
    523553            first = start-1
    524             len = size
    525             del fullresult[:first]
    526             del fullresult[len:]
    527             tocdivs = fullresult
    528            
    529             # check all a-tags
    530             links = tocdivs.findall(".//a")
    531             for l in links:
    532                 href = l.get('href')
    533                 if href:
    534                     # assume all links go to pages
    535                     linkUrl = urlparse.urlparse(href)
    536                     linkParams = urlparse.parse_qs(linkUrl.query)
    537                     # take some parameters
    538                     params = {'pn': linkParams['pn'],
    539                               'highlightQuery': linkParams.get('highlightQuery',''),
    540                               'highlightElement': linkParams.get('highlightElement',''),
    541                               'highlightElementPos': linkParams.get('highlightElementPos','')
    542                               }
    543                     url = self.getLink(params=params)
    544                     l.set('href', url)
     554            last = first+size
     555            tocdivs = resultxml[first:last]
     556            #del fullresult[:first]
     557            #del fullresult[len:]
     558            #tocdivs = fullresult
     559           
     560            toc = ET.Element('div', attrib={'class':'queryResultPage'})
     561            for div in tocdivs:
     562                # check all a-tags
     563                links = div.findall(".//a")
     564                for l in links:
     565                    href = l.get('href')
     566                    if href:
     567                        # assume all links go to pages
     568                        linkUrl = urlparse.urlparse(href)
     569                        linkParams = urlparse.parse_qs(linkUrl.query)
     570                        # take some parameters (make sure it works even if the link was already parsed)
     571                        params = {'pn': linkParams.get('page',linkParams.get('pn', None)),
     572                                  'highlightQuery': linkParams.get('highlightQuery',None),
     573                                  'highlightElement': linkParams.get('highlightElem',linkParams.get('highlightElement',None)),
     574                                  'highlightElementPos': linkParams.get('highlightElemPos',linkParams.get('highlightElementPos',None))
     575                                  }
     576                        if not params['pn']:
     577                            logging.warn("getResultsPage: link has no page: %s"%href)
     578                           
     579                        url = self.getLink(params=params)
     580                        l.set('href', url)
    545581                       
    546             return serialize(tocdivs)
     582                toc.append(div)
     583                       
     584            return serialize(toc)
    547585       
    548586        return "ERROR: no results!"
  • css/docuviewer.css

    r575 r576  
    269269    margin-bottom: 0.25em;
    270270}
    271 /* normalization forms */
     271/* normalization forms *
    272272div.col.main div.content.text div.text.orig span.w span.reg,
    273273div.col.main div.content.text div.text.orig span.w span.norm {
     
    282282    display: none;
    283283}
    284 /* dictionary forms */
     284/* dictionary forms *
    285285div.col.main div.content.text div.text.plain span.w a.dictionary {
    286286    display: none;
     
    288288div.col.main div.content.text div.text.dict span.w span.nodictionary {
    289289    display: none;
    290 }
     290}
     291*/
    291292/* page break */
    292293div.col.main div.content.text span.pb span.n,
     
    301302}
    302303/* note */
    303 div.col.main div.content.text span.note {
     304div.col.main div.content.text span.note span.noteBody {
    304305        display: block;
    305306        /* float: left; */
     
    310311}
    311312div.col.main div.content.text span.note span.noteSign {
    312         display: none;
     313    display: none;
     314    /* font-size: 70%;
     315        vertical-align: super; */
    313316}
    314317/* figure */
     
    355358        margin-bottom: 0.5em;
    356359}
     360div.col.results div.content div.hit {
     361    margin-bottom: 0.5em;
     362}
     363div.col.results div.content div.hit span.hitLink {
     364    margin-right: 0.5em;
     365}
    357366
    358367/*
     
    399408        font-family: Monaco,Courier,monospace;
    400409        font-size: 12px;
     410}
     411div.col.main div.content.xml ul {
     412    padding-left: 1em;
    401413}
    402414div.col.main div.content.xml div.pageHeaderTitle {
Note: See TracChangeset for help on using the changeset viewer.