From: Karim Hamidou Date: Thu, 24 Mar 2016 04:47:53 +0000 (-0700) Subject: better TOC matching X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=157bb8a3e85fc48ea14f0d6dc35a2b23522abd17;p=rfcspot.git better TOC matching --- diff --git a/src/build-index.py b/src/build-index.py index 18ae75f7..ddd98c22 100644 --- a/src/build-index.py +++ b/src/build-index.py @@ -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) diff --git a/src/render_rfc.py b/src/render_rfc.py index ef3c3356..b949cf2c 100644 --- a/src/render_rfc.py +++ b/src/render_rfc.py @@ -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 """{} {}
""".format(anchor, indent, section_name, title) + return """{} {}
""".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"""\1. \2

LOL

""", data) + hl_toc_regexp = re.compile(r'^(Table of Contents)', re.IGNORECASE | re.MULTILINE) data = hl_toc_regexp.sub(r'

\1

', data) return data