Hack in support for untitled docs.
author |
Steve Losh <steve@stevelosh.com> |
date |
Tue, 31 Jan 2012 20:09:42 -0500 |
parents |
8a9b14686d27
|
children |
99b9a6d2ba65
|
branches/tags |
(none) |
files |
d/base.py docs/02-usage.markdown publish.sh |
Changes
--- a/d/base.py Tue Jan 31 13:47:00 2012 -0500
+++ b/d/base.py Tue Jan 31 20:09:42 2012 -0500
@@ -111,20 +111,31 @@
return toc
def _fix_md_toc(content):
- """Remove the first heading level from the Markdown-generated TOC."""
+ """Remove the first heading level from the Markdown-generated TOC.
+
+ Only do so if it's on its own, though.
+
+ """
e = pq(content)
if not e('.toc'):
return content
+ lis = e('.toc > ul > li')
+ if len(lis) > 1:
+ return content
+
subtoc = e('.toc > ul > li > ul').html()
e('.toc > ul').html(subtoc)
return unicode(e)
-def _linkify_title(content):
+def _linkify_title(content, fallback_title):
e = pq(content)
title = e('.markdown h1').text()
- e('.markdown h1').html('<a href="">' + title + '</a>')
+ if title:
+ e('.markdown h1').html('<a href="">' + title + '</a>')
+ else:
+ e('.markdown').prepend('<h1><a href="">' + fallback_title + '</a></h1>')
return unicode(e)
@@ -132,6 +143,39 @@
if not os.path.isdir(path):
os.makedirs(path)
+def _get_fallback_title(path):
+ title = path.split('.', 1)[0]
+ if '-' in title and all([c in '0123456789' for c in title.split('-', 1)[0]]):
+ title = title.split('-', 1)[1]
+
+ title = title.replace('-', ' ').replace('_', ' ')
+
+ if title.lower() == title:
+ title = title.capitalize()
+
+ return title
+
+def _find_title(content):
+ # TODO: Make this less ugly.
+ lines = content.splitlines()
+
+ if len(lines) == 0:
+ return None
+ first_line = lines[0].strip()
+
+ if first_line.startswith('#'):
+ return first_line.lstrip('#')
+
+ if len(lines) == 1:
+ return None
+
+ second_line = lines[1].strip()
+
+ if second_line and all(c == '=' for c in second_line):
+ return first_line
+
+ return None
+
def _render(title, footer, path, target, page_type, toc=None):
if page_type == 'index':
@@ -142,12 +186,10 @@
with open(path) as f:
data = f.read()
+ fallback_title = _get_fallback_title(path)
+
if page_type == 'content':
- try:
- page_title = data.splitlines()[0].lstrip('#').strip()
- except IndexError:
- sys.stdout.write('Documentation file %s must start with a level 1 heading!\n' % path)
- sys.exit(1)
+ page_title = _find_title(data) or fallback_title
title_tag = page_title + ' / ' + title
else:
page_title = title_tag = title
@@ -158,7 +200,7 @@
content += post.format(footer=footer)
if page_type == 'content':
- content = _linkify_title(_fix_md_toc(content))
+ content = _linkify_title(_fix_md_toc(content), fallback_title)
if not os.path.isdir(target):
os.makedirs(target)
--- a/docs/02-usage.markdown Tue Jan 31 13:47:00 2012 -0500
+++ b/docs/02-usage.markdown Tue Jan 31 20:09:42 2012 -0500
@@ -69,6 +69,9 @@
Every page other than the introduction needs a level 1 heading as the first
line. It will be used as the title of the page.
+If you don't have a level 1 heading as the first line, `d` will try to guess
+that page title based on the filename. It may or may not do a good job.
+
### Other Headings
There shouldn't be any other level 1 headings. Levels 2/3/4/5/6 are fine.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/publish.sh Tue Jan 31 20:09:42 2012 -0500
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+python setup.py sdist upload