Script order
4 files changed, 19 insertions(+), 15 deletions(-)

M dss.py
M test.py
M test/js/foo.js => test/js/first/foo.js
M tutorial.rst
M dss.py +8 -10
@@ 26,11 26,6 @@ default_template = string.Template('''<!
 </body></html>
 ''')
 
-def _prune_dirs(dirs):
-    for dirname in dirs:
-        if dirname[0] == '.' or dirname =='_site':
-            dirs.remove(dirname)
-
 def _normpath(p):
     return path.normcase(path.normpath(path.abspath(p)))
 

          
@@ 108,15 103,17 @@ class DeadSimpleSite(object):
                     continue
                 script_path = path.join(root, script)
                 relpath = path.relpath(script_path, self.source)
-                # TODO: in *.nix, files can have a backslash in their names... deal with that?
-                scripts.append(relpath.replace('\\', '/')) # for Windows
+                # In *.nix, paths may contain backslashes. Don't worry about psychos who do that.
+                scripts.insert(0, relpath.replace('\\', '/')) # for Windows
         return scripts
 
     def _walk_source(self):
         for root, dirs, files in os.walk(self.source):
-            for dirname in dirs:
-                if dirname[0] == '.' or dirname == '_site':
-                    dirs.remove(dirname)
+            dirs.sort()
+            files.sort()
+            remove = [d for d in dirs if d[0] == '.' or d == '_site']
+            for dirname in remove:
+                dirs.remove(dirname)
             yield root, dirs, [f for f in files if f[0] != '.' or f == '.htaccess']
 
     def compile(self):

          
@@ 159,6 156,7 @@ class DeadSimpleSite(object):
 
             def do_GET(request):
                 # TODO: expire immediately
+                # TODO: race condition when reloading during a compile
                 # TODO: tests for this
                 site.compile()
                 SimpleHTTPRequestHandler.do_GET(request)

          
M test.py +5 -4
@@ 43,16 43,17 @@ class TestCompile(unittest.TestCase):
         page_contains(r'<body>.*Becomes content.*</body>')
         page_contains(r'<link rel="stylesheet" href="style/global\.css">')
         # TODO: html escape js paths
+        page_contains(r'<script src="js/first/foo\.js"></script>')
         page_contains(r'<script src="js/bar\.js"></script>')
-        page_contains(r'<script src="js/foo\.js"></script>')
         page_contains(r'<link rel="shortcut icon" href="favicon.ico">')
 
     def test_render_default_subdir(self):
         DeadSimpleSite('test').compile()
         page_contains = page_contain_assertion(self, 'test/_site/subdir/page.html')
         page_contains(r'<link rel="stylesheet" href="\.\./style/global\.css">')
-        page_contains(r'<script src="\.\./js/bar\.js"></script>')
-        page_contains(r'<script src="\.\./js/foo\.js"></script>')
+        # TODO: test for script alphabetical ordering
+        # Notice this tests script depth ordering:
+        page_contains(r'<script src="\.\./js/first/foo\.js"></script>.*<script src="\.\./js/bar\.js"></script>.*')
         # TODO: rst includes
         DeadSimpleSite('test').compile()
         not_page_contains = page_contain_assertion(self, 'test/_site/subdir/page.html', invert=True)

          
@@ 63,8 64,8 @@ class TestCompile(unittest.TestCase):
         DeadSimpleSite('test').compile()
         page_contains = page_contain_assertion(self, 'test/_site/custom.html')
         page_contains(r'<link rel="stylesheet" href="style/global\.css">')
+        page_contains(r'<script src="js/first/foo\.js"></script>')
         page_contains(r'<script src="js/bar\.js"></script>')
-        page_contains(r'<script src="js/foo\.js"></script>')
         page_contains(r'Title From Content')
         page_contains(r'ReStructured Text content')
         page_contains(r'<p>Some html content</p>')

          
M test/js/foo.js => test/js/first/foo.js +0 -0

        
M tutorial.rst +6 -1
@@ 30,11 30,16 @@ very top of the file, add the line::
 
 Make sure a blank line follows it.
 
-When you reload the page, its source should appear:
+When Dead Simple compiles a site, it links all files with a ``.js`` extension in output html, so
+when you reload the page, its source should appear:
 
 .. image:: img/simple-world-with-source.png
     :alt: Hello world page displaying source screenshot
     :target: tutorial/2/index.html
 
+So scripts can load in a well-defined order, Dead Simple sorts scripts by deepest nested first, then
+alphabetically. If your JavaScript lives in the ``js`` folder, then, and depends on jQuery, put
+jQuery in ``js/lib`` to ensure that it loads first.
+
 .. _getting started: index.html#getting-started
 .. _dss.py: dss.py