eric6/WebBrowser/Bookmarks/NsHtmlReader.py

changeset 7775
4a1db75550bd
parent 7360
9190402e4505
child 7923
91e843545d9a
diff -r 9eed155411f0 -r 4a1db75550bd eric6/WebBrowser/Bookmarks/NsHtmlReader.py
--- a/eric6/WebBrowser/Bookmarks/NsHtmlReader.py	Sat Oct 10 16:03:53 2020 +0200
+++ b/eric6/WebBrowser/Bookmarks/NsHtmlReader.py	Sun Oct 11 17:54:52 2020 +0200
@@ -7,8 +7,9 @@
 Module implementing a class to read Netscape HTML bookmark files.
 """
 
+import re
 
-from PyQt5.QtCore import QObject, QIODevice, QFile, QRegExp, Qt, QDateTime
+from PyQt5.QtCore import QObject, QIODevice, QFile, QDateTime
 
 from .BookmarkNode import BookmarkNode
 
@@ -27,32 +28,17 @@
         """
         super(NsHtmlReader, self).__init__()
         
-        self.__folderRx = QRegExp("<DT><H3(.*)>(.*)</H3>", Qt.CaseInsensitive)
-        self.__folderRx.setMinimal(True)
-        
-        self.__endFolderRx = QRegExp("</DL>", Qt.CaseInsensitive)
-        
-        self.__bookmarkRx = QRegExp("<DT><A(.*)>(.*)</A>", Qt.CaseInsensitive)
-        self.__bookmarkRx.setMinimal(True)
-        
-        self.__descRx = QRegExp("<DD>(.*)", Qt.CaseInsensitive)
-        
-        self.__separatorRx = QRegExp("<HR>", Qt.CaseInsensitive)
-        
-        self.__urlRx = QRegExp('HREF="(.*)"', Qt.CaseInsensitive)
-        self.__urlRx.setMinimal(True)
-        
-        self.__addedRx = QRegExp(r'ADD_DATE="(\d*)"', Qt.CaseInsensitive)
-        self.__addedRx.setMinimal(True)
-        
-        self.__modifiedRx = QRegExp(
-            r'LAST_MODIFIED="(\d*)"', Qt.CaseInsensitive)
-        self.__modifiedRx.setMinimal(True)
-        
-        self.__visitedRx = QRegExp(r'LAST_VISIT="(\d*)"', Qt.CaseInsensitive)
-        self.__visitedRx.setMinimal(True)
-        
-        self.__foldedRx = QRegExp("FOLDED", Qt.CaseInsensitive)
+        self.__folderRx = re.compile("<DT><H3(.*?)>(.*?)</H3>", re.IGNORECASE)
+        self.__endFolderRx = re.compile("</DL>", re.IGNORECASE)
+        self.__bookmarkRx = re.compile("<DT><A(.*?)>(.*?)</A>", re.IGNORECASE)
+        self.__descRx = re.compile("<DD>(.*)", re.IGNORECASE)
+        self.__separatorRx = re.compile("<HR>", re.IGNORECASE)
+        self.__urlRx = re.compile('HREF="(.*?)"', re.IGNORECASE)
+        self.__addedRx = re.compile(r'ADD_DATE="(\d*?)"', re.IGNORECASE)
+        self.__modifiedRx = re.compile(r'LAST_MODIFIED="(\d*?)"',
+                                       re.IGNORECASE)
+        self.__visitedRx = re.compile(r'LAST_VISIT="(\d*?)"', re.IGNORECASE)
+        self.__foldedRx = re.compile("FOLDED", re.IGNORECASE)
     
     def read(self, fileNameOrDevice):
         """
@@ -79,49 +65,64 @@
         
         while not dev.atEnd():
             line = str(dev.readLine(), encoding="utf-8").rstrip()
-            if self.__folderRx.indexIn(line) != -1:
+            match = (
+                self.__folderRx.search(line) or
+                self.__endFolderRx.search(line) or
+                self.__bookmarkRx.search(line) or
+                self.__descRx.search(line) or
+                self.__separatorRx.search(line)
+            )
+            if match is None:
+                continue
+            
+            if match.re is self.__folderRx:
                 # folder definition
-                arguments = self.__folderRx.cap(1)
-                name = self.__folderRx.cap(2)
+                arguments = match.group(1)
+                name = match.group(2)
                 node = BookmarkNode(BookmarkNode.Folder, folders[-1])
                 node.title = Utilities.html_udecode(name)
-                node.expanded = self.__foldedRx.indexIn(arguments) == -1
-                if self.__addedRx.indexIn(arguments) != -1:
+                node.expanded = self.__foldedRx.search(arguments) is None
+                addedMatch = self.__addedRx.search(arguments)
+                if addedMatch is not None:
                     node.added = QDateTime.fromTime_t(
-                        int(self.__addedRx.cap(1)))
+                        int(addedMatch.group(1)))
                 folders.append(node)
                 lastNode = node
             
-            elif self.__endFolderRx.indexIn(line) != -1:
+            elif match.re is self.__endFolderRx:
                 # end of folder definition
                 folders.pop()
             
-            elif self.__bookmarkRx.indexIn(line) != -1:
+            elif match.re is self.__bookmarkRx:
                 # bookmark definition
-                arguments = self.__bookmarkRx.cap(1)
-                name = self.__bookmarkRx.cap(2)
+                arguments = match.group(1)
+                name = match.group(2)
                 node = BookmarkNode(BookmarkNode.Bookmark, folders[-1])
                 node.title = Utilities.html_udecode(name)
-                if self.__urlRx.indexIn(arguments) != -1:
-                    node.url = self.__urlRx.cap(1)
-                if self.__addedRx.indexIn(arguments) != -1:
+                match1 = self.__urlRx.search(arguments)
+                if match1 is not None:
+                    node.url = match1.group(1)
+                match1 = self.__addedRx.search(arguments)
+                if match1 is not None:
                     node.added = QDateTime.fromTime_t(
-                        int(self.__addedRx.cap(1)))
-                if self.__modifiedRx.indexIn(arguments) != -1:
+                        int(match1.group(1)))
+                match1 = self.__modifiedRx.search(arguments)
+                if match1 is not None:
                     node.modified = QDateTime.fromTime_t(
-                        int(self.__modifiedRx.cap(1)))
-                if self.__visitedRx.indexIn(arguments) != -1:
+                        int(match1.group(1)))
+                match1 = self.__visitedRx.search(arguments)
+                if match1 is not None:
                     node.visited = QDateTime.fromTime_t(
-                        int(self.__visitedRx.cap(1)))
+                        int(match1.group(1)))
                 lastNode = node
             
-            elif self.__descRx.indexIn(line) != -1:
+            elif match.re is self.__descRx:
                 # description
                 if lastNode:
                     lastNode.desc = Utilities.html_udecode(
-                        self.__descRx.cap(1))
+                        match.group(1))
             
-            elif self.__separatorRx.indexIn(line) != -1:
+            elif match.re is self.__separatorRx:
                 # separator definition
                 BookmarkNode(BookmarkNode.Separator, folders[-1])
         

eric ide

mercurial