]> khamidou — GitHub mirrors - rfcspot.git/commitdiff
latest changes
authorKarim Hamidou <karim@MacBook.local>
Sun, 14 Feb 2016 06:58:56 +0000 (22:58 -0800)
committerKarim Hamidou <karim@MacBook.local>
Sun, 14 Feb 2016 06:58:56 +0000 (22:58 -0800)
.gitignore
src/build-index.py
src/format-rfc.py
src/generate-site-index.py [new file with mode: 0644]
templates/rfc.html [moved from src/template.py with 62% similarity]

index 01b2020b09210dc8299c380336390f991e78c4a6..42ba6cc66ae3419f8c8cc6b4b021fde0753a90f2 100644 (file)
@@ -4,5 +4,6 @@
 .DS_Store
 stackexchange/*
 rfcs/*
+source_rfcs/*
 venv
 test.html
index 76bbd5994c651125fe38f77ecca63c8e6cdfbbba..fef824fab7efc79bea5fb15865c5ae17997f93e3 100644 (file)
@@ -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()
index 38b05996ec43fc938f1dd06b2850e2739453e713..b66d0ec0ed1c062b4fa8db50011bfc2382b732c9 100644 (file)
@@ -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 (file)
index 0000000..49b2be9
--- /dev/null
@@ -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()
similarity index 62%
rename from src/template.py
rename to templates/rfc.html
index 3ca912d3ae4a1cd6b11e630ceca7968e74e0fd9c..857e7213206263038ec9b654899b62353a22d018 100644 (file)
@@ -1,4 +1,3 @@
-html_template = """\
 <!DOCTYPE html>
 <html>
 <head>
@@ -19,14 +18,19 @@ html_template = """\
   <tbody>
     <tr id='section-{{ num }}'>
       <td class=docs>
-        {{{ rfc }}}
+        {{ rfc }}
       </td>
       <td class=code>
-        <div class='highlight'><pre>{{{ code_html }}}</pre></div>
+        <div class='highlight'>
+            <b>Related Stack Overflow questions:</b>
+            <ul>
+                {% for question in questions %}
+                    <li><a href={{ question['url'] }}>{{ question['title'] }}</a></li>
+                {% endfor %}
+            </ul>
+        </div>
       </td>
     </tr>
   </table>
 </div>
 </body>
-"""
-