]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/util.py
wiki: Implement search
[ipfire.org.git] / src / backend / util.py
index ea7692a0dab3cb20210c9bb27e667da63d782c25..ff8248eaed4f1974308a48a3c79deaeef31efe90 100644 (file)
@@ -1,8 +1,28 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 import random
+import re
 import string
 
+def parse_search_query(query):
+       q = []
+       for word in query.split():
+               # Is this lexeme negated?
+               negated = word.startswith("!")
+
+               # Remove any special characters
+               word = re.sub(r"\W+", "", word, flags=re.UNICODE)
+               if not word:
+                       continue
+
+               # Restore negation
+               if negated:
+                       word = "!%s" % word
+
+               q.append(word)
+
+       return " & ".join(q)
+
 def format_size(s, max_unit=None):
        units = ("B", "kB", "MB", "GB", "TB")