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)
# 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)
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.
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