From: Karim Hamidou Date: Sun, 28 Feb 2016 07:01:38 +0000 (-0800) Subject: works relatively okay X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=cb1bcf3949fbfda5003155513053c5943c069a28;p=rfcspot.git works relatively okay --- diff --git a/.gitignore b/.gitignore index a18eb177..fcc084e0 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ source_rfcs/* venv test.html out/ +html_rfcs/* diff --git a/README.md b/README.md new file mode 100644 index 00000000..619ab78b --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +Building instructions: + +1. Download the latest RFC archives from rfc-editor.org: + `rsync -avz ftp.rfc-editor.org::rfcs-text-only .` + +2. Run build.py diff --git a/build.py b/build.py index 1dd40d2c..239a945c 100644 --- a/build.py +++ b/build.py @@ -1,11 +1,15 @@ #!/usr/bin/env python # a simple incremental build script. # usage build.py src_dir/ output_dir +import os import sys import subprocess from os import listdir from os.path import isfile, join, getmtime +from convert_txt_rfc_to_html import convert_rfc_html +from src.format_rfc import format_rfc, ProcessingError +TMP_DIR = 'html_rfcs' def list_files(directory): return [f for f in listdir(directory) @@ -15,8 +19,12 @@ def execute(command): return subprocess.call(command) def build_file(infile, outfile): - command = ['python', 'src/format-rfc.py', infile, outfile] - execute(command) + print "Building {}".format(outfile) + + try: + format_rfc(infile, outfile) + except ProcessingError: + print "Skipping {}".format(infile) def copy_css(out_dir): print "Copying CSS." @@ -28,14 +36,7 @@ def generate_site_index(out_dir): command = ['python', 'src/generate-site-index.py', out_dir, join(out_dir, 'index.html')] execute(command) -def main(): - if len(sys.argv) != 3: - print "usage: build.py " - sys.exit(-1) - - src_dir = sys.argv[1] - dest_dir = sys.argv[2] - +def build_rfcs(src_dir, dest_dir): src_files = {filename: join(src_dir, filename) for filename in list_files(src_dir)} dest_files = {filename: join(dest_dir, filename) for filename in list_files(dest_dir)} @@ -46,9 +47,20 @@ def main(): if filename not in dest_files: build_file(src_path, dest_path) - if getmtime(src_path) > getmtime(dest_path): + if os.path.exists(dest_path) and getmtime(src_path) > getmtime(dest_path): build_file(src_path, dest_path) + +def main(): + if len(sys.argv) != 3: + print "usage: build.py " + sys.exit(-1) + + src_dir = sys.argv[1] + dest_dir = sys.argv[2] + + convert_rfc_html(src_dir, TMP_DIR) + build_rfcs(TMP_DIR, dest_dir) copy_css(dest_dir) generate_site_index(dest_dir) diff --git a/convert_txt_rfc_to_html.py b/convert_txt_rfc_to_html.py index 0703559a..3c84260c 100644 --- a/convert_txt_rfc_to_html.py +++ b/convert_txt_rfc_to_html.py @@ -22,18 +22,10 @@ def list_files(directory, extension=''): def build_file(infile, outfile): with open(outfile, 'w+') as fd: - command = ['python', 'src/rfcmarkup', dirname(infile), splitext(basename(infile))[0]] + command = ['python', 'src/rfcmarkup', dirname(infile), infile] subprocess.call(command, stdout=fd) - -def main(): - if len(sys.argv) != 3: - print "usage: convert_txt_to_html " - sys.exit(-1) - - src_dir = sys.argv[1] - dest_dir = sys.argv[2] - +def convert_rfc_html(src_dir, dest_dir): src_files = {filename: join(src_dir, filename) for filename in list_files(src_dir, 'txt')} dest_files = {filename: join(dest_dir, filename) for filename in list_files(dest_dir, 'html')} @@ -41,10 +33,8 @@ def main(): src_path = join(src_dir, filename) + '.txt' dest_path = join(dest_dir, filename) + '.html' - if filename not in dest_files: - build_file(src_path, dest_path) - - if getmtime(src_path) > getmtime(dest_path): + if filename not in dest_files or getmtime(src_path) > getmtime(dest_path): + print "Building {}".format(src_path) build_file(src_path, dest_path) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/format-rfc.py b/src/format_rfc.py similarity index 94% rename from src/format-rfc.py rename to src/format_rfc.py index b66d0ec0..f2e28d41 100644 --- a/src/format-rfc.py +++ b/src/format_rfc.py @@ -7,10 +7,10 @@ import sys from bs4 import BeautifulSoup, Tag, Comment from jinja2 import Environment, FileSystemLoader -class ProcessingException(Exception): +class ProcessingError(Exception): pass -def process_file(infile, outfile): +def format_rfc(infile, outfile): data = '' with open(infile) as fd: data = fd.read() @@ -107,10 +107,10 @@ def process_file(infile, outfile): soup = BeautifulSoup(contents, 'html.parser') # Finally, extract the body: - body_contents = soup.body.find('div', class_='content') + body_contents = soup.body # .find('div', class_='content') if body_contents is None: - raise ProcessingException("Couldn't find a content block") + raise ProcessingError("Couldn't find a content block") dct = dict(rfc=body_contents.prettify(), title=title) @@ -126,7 +126,7 @@ def main(): if len(sys.argv) != 3: print "usage: format-rfc rfc.html pretty_rfc.html" - process_file(sys.argv[1], sys.argv[2]) + format_rfc(sys.argv[1], sys.argv[2]) if __name__ == '__main__': main()