Commits

anatoly techtonik  committed dfb8763

Partitioning and reassembly logic is ready

  • Participants
  • Parent commits 4052582

Comments (0)

Files changed (1)

 """
-wikify - micro-framework for text wikification
+wikify your texts! micro-framework for text wikification
 
 goals - easy to extend and debug
 
    - exclude processed text from further processing
 
 example - replace all wiki:something with HTML links
- [ ] wrap text into list with single item
- [ ] split text into three parts using regexp `wiki:\w+`
- [ ] copy 1st part (not-matched) into the resulting list
- [ ] replace matched part with link, insert (processed)
+ [x] wrap text into list with single item
+ [x] split text into three parts using regexp `wiki:\w+`
+ [x] copy 1st part (not-matched) into the resulting list
+ [x] replace matched part with link, insert (processed)
      into the resulting list
  [ ] process (the-rest) until text list doesn't change
 
  [ ] repeat the above for the rest of rules, skipping
      (processed) parts
 
- [ ] reassemble text from the list
+ [x] reassemble text from the list
 
 
 notes - (flat algorithm) doesn't process nested markup,
     return None
   return (text[:match.start()], "[link]", text[match.end():])
 
-print rule_linkify("a web site http://google.com")
+#print rule_linkify("a web site http://google.com")
 
 # [ ] sets of common rules
 # [ ] configurable replacements
+
+# --- execute rules ---
+
 # [ ] indented prints after every step
+
+def wikify(text, rules):
+  texts = []  # store processed pieces
+  subst = []  # store replacements
+
+  texts = [text]
+  for rule in rules:
+    subidx = 0  # index in replacements array
+    for idx,part in enumerate(texts):
+      if part == None:
+        subidx += 1
+        continue
+      res = rule(part)
+      if res == None:
+        continue
+      else:
+        subst.append(res[1])
+        # replacing current text with three elements
+        # ( not-matched, None, the-rest )
+        # the None component will be picked on next
+        # iterationand subidx will be increased
+
+        # [ ] check endless cycle condition
+        texts[idx:] = [res[0], None, res[2]]
+
+  # -- reasseble
+  # [ ] optimize for memory usage
+  # [ ] sanity check count(None) == len(subst)
+  restext = ''
+  subidx = 0
+  for part in texts:
+    if part == None:
+      # substitute
+      restext += subst[subidx]
+      subidx += 1
+    else:
+      restext += part
+  return restext
+
+#print wikify("a web site http://google.com", [rule_linkify])