From 704fb7f115355709fb92a99deba7e9b2b22da85c Mon Sep 17 00:00:00 2001 From: mpj Date: Sun, 15 Nov 2009 16:12:35 +0000 Subject: [PATCH] Switched to certik's Python rewrite --- README.rst | 31 ++++++++++++-------- sphinx-to-github | 76 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 73 insertions(+), 34 deletions(-) diff --git a/README.rst b/README.rst index bb38ca6..3430c91 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ Sphinx to GitHub ================ -A basic shell script for preparing the html output of the sphinx documentation +A Python script for preparing the html output of the sphinx documentation system for github pages. It renames any top level folders which start with an underscore and edits any @@ -19,24 +19,31 @@ start of folder names for static content. How? ---- -The script should be run from the top level of the html output, ie. from within -the folder which contains ``index.html`` and the offending underscore folders. +Run the script with the path to the ``html`` output directory as the first +argument. Requirements ------------ -To uses: - -* bash -* sed - -Might make sense to have a pure python script but ``sed`` makes it so easy. +The script uses ``/usr/bin/env`` and ``python``. Note ---- -This has been written and tested for a very basic project. If you find -complications within more involved setups please let me know and/or patch up the -code a little. +The first incarnation of this script was written in bash using ``find`` and +``sed``. It was delightfully old school but not very portable and not very fast. +It is available as ``sphinx-to-github.legacy`` because the author isn't ready to +let go yet. + +Credits +------- + +Thank you to: + +* `mikejs ` +* `certik ` + +For their contributions, to Georg Brandl for `Sphinx ` +and the github crew for the pages functionality. diff --git a/sphinx-to-github b/sphinx-to-github index eab16e5..9ee7b37 100755 --- a/sphinx-to-github +++ b/sphinx-to-github @@ -1,23 +1,55 @@ -#!/bin/bash - -sed=`which gsed || which sed` - -for underscore_folder in _* -do - echo "Processing matches for: " $underscore_folder - - folder_without_underscore=`echo -n $underscore_folder | $sed 's/^.//'` - - for underscore_file in $( find $underscore_folder -type f ) - do - file_without_underscore=`echo -n $underscore_file | $sed 's/^.//'` - - for file in $( find . -type f -name "*.html" ) - do - $sed -i "s/${underscore_file//\//\/}/${file_without_underscore//\//\/}/g" $file - done - done - - mv $underscore_folder $folder_without_underscore -done +#! /usr/bin/env python + +import os +import sys +import re + +dir = sys.argv[1] + +def convert_file(file, convert_dict): + print "Converting: %s" % file + text = open(file).read() + for old_link in convert_dict: + new_link = convert_dict[old_link] + text = text.replace(old_link, new_link) + open(file, "w").write(text) + +def remove_underscores(path): + i = path.find("_") + if i != -1: + path = path[:i] + path[i+1:] + return remove_underscores(path) + else: + return path + +def strip_beginning_dotslash(path): + if "./" == path[:2]: + path = path[2:] + return path + +# build up the global dictionary with files we need to convert: +d = {} +for old_root, dirs, files in os.walk(dir): + if "_" in old_root: + new_root = remove_underscores(old_root) + assert new_root != old_root + os.mkdir(new_root) + for file in files: + old_path = os.path.join(old_root, file) + new_path = os.path.join(new_root, file) + os.rename(old_path, new_path) + new_path = strip_beginning_dotslash(new_path) + old_path = strip_beginning_dotslash(old_path) + d[old_path] = new_path + +# delete the old "_" directories +for old_root, dirs, files in os.walk(dir, topdown=False): + if "_" in old_root: + os.rmdir(old_root) + +# convert all html files: +for root, dirs, files in os.walk(dir): + for file in files: + if file.endswith(".html"): + convert_file(os.path.join(root, file), d)