]> khamidou — GitHub mirrors - rfcspot.git/commitdiff
better TOC matching
authorKarim Hamidou <khamidou@gmail.com>
Thu, 24 Mar 2016 04:47:53 +0000 (21:47 -0700)
committerKarim Hamidou <khamidou@gmail.com>
Thu, 24 Mar 2016 04:47:53 +0000 (21:47 -0700)
src/build-index.py
src/render_rfc.py

index 18ae75f7ad5c86c68975bd5912423ef5eacef5b5..ddd98c229777d5af91e06610e754e658d872c2f0 100644 (file)
@@ -11,20 +11,13 @@ from lxml import etree
 from collections import defaultdict
 
 
-def load_posts_file(filename):
-    xml_tree = None
-    with open(filename) as fd:
-        xml_tree = etree.parse(fd)
-
-    return xml_tree
-
-
 def build_post_index(tree):
     questions_table = defaultdict(dict)
 
     # Get all the questions.
     for post in tree.findall('//row[@PostTypeId="1"]'):
         title = post.get('Title')
+        print title
         post_id = int(post.get('Id'))
         url = "https://stackoverflow.com/questions/{}/".format(post_id)
 
@@ -34,61 +27,44 @@ def build_post_index(tree):
 
 
 # Create a mapping between RFCs and stack exchange questions.
-def build_rfc_index(tree, post_index):
+def build_rfc_index(filename):
     rfcs_table = defaultdict(list)
     rfc_regexp = re.compile(r'RFC\s*(\d+)', re.IGNORECASE)
 
-    # Get all the comments.
-    for post in tree.findall('//row[@PostTypeId="2"]'):
-        body = post.get('Body')
-        parent_post = int(post.get('ParentId'))
-
-        for match in rfc_regexp.findall(body):
-            # FIXME: we're limiting ourselves to top-level replies.
-            # Maybe do the same for replies to replies?
-            rfc_id = int(match)
-
-            if parent_post in post_index:
+    count = 0
+    with open(filename) as fd:
+        # Get all the comments.
+        for _, post in etree.iterparse(fd, events=('end',), tag='row'):
+            if post.get('PostTypeId') != '2':
+                continue
+
+            count += 1
+            if count % 10000 == 0:
+                print count
+
+            body = post.get('Body')
+            parent_post = int(post.get('ParentId'))
+
+            for match in rfc_regexp.findall(body):
+                # FIXME: we're limiting ourselves to top-level replies.
+                # Maybe do the same for replies to replies?
+                rfc_id = int(match)
                 rfcs_table[rfc_id].append(parent_post)
 
-    # Dedupe.
-    for rfc in rfcs_table:
-        rfcs_table[rfc] = list(set(rfcs_table[rfc]))
-
-    return rfcs_table
+        # Dedupe.
+        for rfc in rfcs_table:
+            rfcs_table[rfc] = list(set(rfcs_table[rfc]))
 
-
-# The post index is big. Only keep posts which are related to an RFC.
-def optimize_post_index(rfc_index, post_index):
-    posts = [item for sublist in rfc_index.values() for item in sublist]
-    optimized_index = dict()
-
-    for post in posts:
-        if post in post_index:
-            optimized_index[post] = post_index[post]
-
-    return optimized_index
+        return rfcs_table
 
 
 def main():
     if len(sys.argv) != 2:
         print "usage: build-index Posts.xml"
 
-    print "Building tree"
-    tree = load_posts_file(sys.argv[1])
-
-    print "Building post index"
-    post_index = build_post_index(tree)
-
+    filename = sys.argv[1]
     print "Building RFC index"
-    rfc_index = build_rfc_index(tree, post_index)
-
-    print "Building optimized index"
-    optimized_index = optimize_post_index(rfc_index, post_index)
-
-
-    with open('post_index.json', 'w+') as fd:
-        json.dump(optimized_index, fd, indent=4)
+    rfc_index = build_rfc_index(filename)
 
     with open('rfc_index.json', 'w+') as fd:
         json.dump(rfc_index, fd, indent=4)
index ef3c3356ac5d6f063dcd351235787961a041507c..b949cf2c5969c7235ef9cf138e9a219bcf76d72b 100644 (file)
@@ -39,16 +39,22 @@ def cleanup_author_header(data, **kwargs):
 
 def cleanup_toc(data, **kwargs):
     # Clean up the table of contents
-    # toc_regexp = re.compile(r'^\s+(.+?)\s*(.+?)(\.+)\s+\d+$', re.MULTILINE)
-    toc_regexp = re.compile(r'^\s+([\d\.]+)\s+(.+)(\.+)\s*(\d+)$', re.MULTILINE)
+    #toc_regexp = re.compile(r'^\s+([\d\.]+)\s+(.+)(\.+)\s*(\d+)$', re.MULTILINE)
+    toc_regexp = re.compile(r"^\s+(.+)\s+\.+\s+(\d+)", re.MULTILINE)
+    submatch_rx = re.compile(r"([\w\d\.]+)\s{2,}(.+)")
 
     def format_match(match):
-        section_name = match.group(1)
-        title = match.group(2)
+        submatch = submatch_rx.match(match.group(1))
+        if submatch:
+            section_name = submatch.group(1)
+            title = submatch.group(2)
+        else:
+            section_name = ''
+            title = match.group(1)
 
         anchor = section_name
-        if anchor[-1] == '.':
-            anchor = anchor[:-1]
+        if anchor and anchor[-1] == '.':
+            anchor = "#section-" + anchor[:-1]
 
         indent = ''
         # Indent parts depending on their section number.
@@ -57,10 +63,13 @@ def cleanup_toc(data, **kwargs):
         elif anchor.count('.') == 2:
             indent = 'indent-2'
 
-        return """<a href="#section-{}" class="{}">{} {}</a><br>""".format(anchor, indent, section_name, title)
+        return """<a href="{}" class="{}">{} {}</a><br>""".format(anchor, indent, section_name, title)
 
     data = toc_regexp.sub(format_match, data)
 
+    #special_part_rx = re.compile(r"^Table of Contents\n*\s*(\w.*)\s+\.+\s*(\d+)", re.MULTILINE | re.DOTALL)
+    #data = special_part_rx.sub(r"""<a href="section-\1">\1. \2</a><H1>LOL</H1>""", data)
+
     hl_toc_regexp = re.compile(r'^(Table of Contents)', re.IGNORECASE | re.MULTILINE)
     data = hl_toc_regexp.sub(r'<h2>\1</h2>', data)
     return data