# 2. Go through every question & answer to find RFC references
# 3. Link the question and the RFC number together
# 4. Dump the result to a file.
+import re
import sys
-import lxml
+import json
+from lxml import etree
from collections import defaultdict
-questions_table = defaultdict([])
-xml_tree = None
def load_posts_file(filename):
- global xml_tree
+ 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')
+ post_id = int(post.get('Id'))
+ url = "https://security.stackexchange.com/questions/{}/".format(post_id)
+
+ questions_table[post_id] = dict(title=title, post_id=post_id, url=url)
+
+ return questions_table
+
+
+# Create a mapping between RFCs and stack exchange questions.
+def build_rfc_index(tree, post_index):
+ 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:
+ rfcs_table[rfc_id].append(parent_post)
+
+ return rfcs_table
def main():
if len(sys.argv) != 2:
print "usage: build-index Posts.xml"
- load_posts_file(sys.argv[1])
+ tree = load_posts_file(sys.argv[1])
+ post_index = build_post_index(tree)
+ rfc_index = build_rfc_index(tree, post_index)
+
+ with open('post_index.json', 'w+') as fd:
+ json.dump(post_index, fd, indent=4)
+
+ with open('rfc_index.json', 'w+') as fd:
+ json.dump(rfc_index, fd, indent=4)
+
if __name__ == '__main__':
main()
# format.
import re
import sys
-import pystache
from bs4 import BeautifulSoup, Tag, Comment
-from template import html_template
+from jinja2 import Environment, FileSystemLoader
class ProcessingException(Exception):
pass
raise ProcessingException("Couldn't find a content block")
dct = dict(rfc=body_contents.prettify(), title=title)
- rendered = pystache.render(html_template, dct)
+
+ env = Environment(loader=FileSystemLoader('templates'))
+ template = env.get_template('rfc.html')
+ rendered = template.render(**dct)
with open(outfile, 'w+') as fd:
fd.write(rendered.encode('utf-8'))