"Fossies" - the Fresh Open Source Software Archive

Member "recoll-1.26.3/filters/rclrtf.py" (4 Sep 2019, 1718 Bytes) of package /linux/privat/recoll-1.26.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "rclrtf.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/env python3
    2 from __future__ import print_function
    3 
    4 import rclexecm
    5 import rclexec1
    6 import re
    7 import sys
    8 import os
    9 
   10 # Processing the output from unrtf
   11 class RTFProcessData:
   12     def __init__(self, em):
   13         self.em = em
   14         self.out = []
   15         self.gothead = 0
   16         self.patendhead = re.compile(b'''</head>''')
   17         self.patcharset = re.compile(b'''^<meta http-equiv=''')
   18 
   19     # Some versions of unrtf put out a garbled charset line.
   20     # Apart from this, we pass the data untouched.
   21     def takeLine(self, line):
   22         if not self.gothead:
   23             if self.patendhead.search(line):
   24                 self.out.append(b'<meta http-equiv="Content-Type" ' + \
   25                              b'content="text/html;charset=UTF-8">')
   26                 self.out.append(line)
   27                 self.gothead = 1
   28             elif not self.patcharset.search(line):
   29                 self.out.append(line)
   30         else:
   31             self.out.append(line)
   32 
   33     def wrapData(self):
   34         return b'\n'.join(self.out)
   35 
   36 class RTFFilter:
   37     def __init__(self, em):
   38         self.em = em
   39         self.ntry = 0
   40 
   41     def reset(self):
   42         self.ntry = 0
   43             
   44     def getCmd(self, fn):
   45         if self.ntry:
   46             return ([], None)
   47         self.ntry = 1
   48         cmd = rclexecm.which("unrtf")
   49         if cmd:
   50             return ([cmd, "--nopict", "--html"], RTFProcessData(self.em))
   51         else:
   52             return ([], None)
   53 
   54 if __name__ == '__main__':
   55     if not rclexecm.which("unrtf"):
   56         print("RECFILTERROR HELPERNOTFOUND unrtf")
   57         sys.exit(1)
   58     proto = rclexecm.RclExecM()
   59     filter = RTFFilter(proto)
   60     extract = rclexec1.Executor(proto, filter)
   61     rclexecm.main(proto, extract)