Commits

Steve Losh  committed 8ab1fd0

OKAY GUYS I FIXED THE NEXT AND PREVIOUS LINKS

  • Participants
  • Parent commits f460a9c

Comments (0)

Files changed (4)

File bookmarkdown/bookmarkdown

     'ga_id': getattr(config, 'ga_id', ''),
     'gauges_id': getattr(config, 'gauges_id', ''),
 }
+md = markdown.Markdown(extensions=['toc'], safe_mode=False)
 # }}}
 
-# Utilities ------------------------------------------------------------------------
+# Utilities -------------------------------------------------------------------
 def mkdirs(path):
     def _md(acc, next):
         target = join(acc, next)
 
 
 
-# Guts -----------------------------------------------------------------------------
-def _build_html_file(filename, template, context={}):
-    source = '%s.markdown' % filename
+# Guts ------------------------------------------------------------------------
+
+def _get_next_prev(path, paths):
+    try:
+        i = paths.index(path)
+        next = ('/%s.html' % paths[i + 1]) if i < len(paths) - 1 else None
+        prev = ('/%s.html' % paths[i - 1]) if i > 0 else None
+    except ValueError:
+        next, prev = None, None
+
+    return next, prev
+
+def _build_html_file(path, template, paths=[], context={}):
+    source = '%s.markdown' % path
 
     if not os.path.exists(source):
         return
 
     with open(source, 'r') as f:
-        content = markdown.markdown(f.read())
+        raw_markdown = f.read()
+
+        if template == 'chapter':
+            raw_markdown = '[TOC]\n\n' + raw_markdown
+
+        content = md.convert(raw_markdown)
+
+    cq = pq(content)
 
     try:
-        name = pq(content)('h1').text()
+        name = cq('h1').text()
     except ValueError:
         name = "Untitled Chapter"
 
-    out = render(template, content=content, name=name, **context)
-    target = join('build', 'html', '%s.html' % filename)
+    try:
+        toc = cq('.toc').html()
+        cq('.toc').remove()
+        content = unicode(cq)
+    except ValueError:
+        toc = None
+
+    next, prev = _get_next_prev(path, paths)
+
+    out = render(template, content=content, name=name, next=next, prev=prev,
+                 toc=toc, **context)
+    target = join('build', 'html', '%s.html' % path)
 
     with open(target, 'w') as f:
         f.write(out)
     content = pq(out)
 
     return {'content': content, 'name': content('.content h1').text(),
-            'filename': filename}
+            'filename': path}
 
 def _build_index_file(chapters):
     source = 'introduction.markdown'
 
     _copy_static()
 
-    _build_html_file('license', 'single')
-    _build_html_file('preface', 'single')
-
-    chapters = []
+    paths = ['preface']
     for filename in os.listdir('chapters'):
         if filename.endswith('.markdown'):
             name = filename.rsplit('.')[0]
-            chapter = _build_html_file(join('chapters', name), 'chapter')
-            chapters.append(chapter)
+            paths.append(join('chapters', name))
+
+    _build_html_file('license', 'single', paths)
+    _build_html_file('preface', 'single', paths)
+
+    chapters = []
+    for path in paths[1:]:
+        chapter = _build_html_file(path, 'chapter', paths)
+        chapters.append(chapter)
 
     _build_index_file(chapters)
 

File bookmarkdown/static/styles/style.less

         }
     }
     .nav {
+        margin-top: 18px;
+
+        ul {
+            list-style-type: none;
+            font: inherit;
+        }
+        li {
+            margin-left: 0px;
+        }
+    }
+    .nav > ul > li > a {
+        text-decoration: none;
+        margin-bottom: 12px;
+        font-weight: bold;
+        font-size: 18px;
+        display: block;
+
+        &:hover {
+            text-decoration: underline;
+        }
+    }
+    .nav > ul > li > ul ul {
+        margin-left: 16px;
+    }
+    .nav > ul > li > ul {
+        margin-left: 0px;
     }
     .prevnext {
         .next {

File bookmarkdown/templates/chapter.html

 
 {% block content_full %}
     <section class="nav three columns">
-        {{ name }}
+        {{ toc }}
+
         <div class="prevnext">
-            <a class="prev" href="">&laquo; Previous</a>
-            <a class="next" href="">Next &raquo;</a>
+            {% if prev %}
+                <a class="prev" href="{{ prev }}">&laquo; Previous</a>
+            {% endif %}
+            {% if next %}
+                <a class="next" href="{{ next }}">Next &raquo;</a>
+            {% endif %}
         </div>
     </section>
 
     <section class="content twelve columns offset-by-one">
         {{ content }}
+
+        <div class="prevnext">
+            {% if prev %}
+                <a class="prev" href="{{ prev }}">&laquo; Previous</a>
+            {% endif %}
+            {% if next %}
+                <a class="next" href="{{ next }}">Next &raquo;</a>
+            {% endif %}
+        </div>
     </section>
 {% endblock %}

File bookmarkdown/templates/single.html

         {{ content }}
 
         <div class="prevnext">
-            <a class="prev" href="">&laquo; Previous</a>
-            <a class="next" href="">Next &raquo;</a>
+            {% if prev %}
+                <a class="prev" href="{{ prev }}">&laquo; Previous</a>
+            {% endif %}
+            {% if next %}
+                <a class="next" href="{{ next }}">Next &raquo;</a>
+            {% endif %}
         </div>
     </div>
 {% endblock %}