From: Karim Hamidou Date: Sun, 14 Feb 2016 06:58:56 +0000 (-0800) Subject: latest changes X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=f2a1c7f1655c99e214ff5a750fae5d4e63e8be64;p=rfcspot.git latest changes --- diff --git a/.gitignore b/.gitignore index 01b2020b..42ba6cc6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ .DS_Store stackexchange/* rfcs/* +source_rfcs/* venv test.html diff --git a/src/build-index.py b/src/build-index.py index 76bbd599..fef824fa 100644 --- a/src/build-index.py +++ b/src/build-index.py @@ -4,22 +4,70 @@ # 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() diff --git a/src/format-rfc.py b/src/format-rfc.py index 38b05996..b66d0ec0 100644 --- a/src/format-rfc.py +++ b/src/format-rfc.py @@ -4,9 +4,8 @@ # 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 @@ -114,7 +113,10 @@ def process_file(infile, outfile): 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')) diff --git a/src/generate-site-index.py b/src/generate-site-index.py new file mode 100644 index 00000000..49b2be97 --- /dev/null +++ b/src/generate-site-index.py @@ -0,0 +1,8 @@ +# generate-site-index --- create an index.html page for the site. +def main(): + +if __name__ == '__main__': + if len(sys.argv) != 2: + print "usage: generate-site-index rfcs/ index.html" + + main() diff --git a/src/template.py b/templates/rfc.html similarity index 62% rename from src/template.py rename to templates/rfc.html index 3ca912d3..857e7213 100644 --- a/src/template.py +++ b/templates/rfc.html @@ -1,4 +1,3 @@ -html_template = """\ @@ -19,14 +18,19 @@ html_template = """\ - {{{ rfc }}} + {{ rfc }} -
{{{ code_html }}}
+
+ Related Stack Overflow questions: + +
-""" -