# HG changeset patch # User Steve Losh # Date 1328058582 18000 # Node ID 764b0d5ebaaa4855fb13a77881da17fee3b764b4 # Parent 8a9b14686d275ab84422daf845e0ce8cb3f00649 Hack in support for untitled docs. diff -r 8a9b14686d27 -r 764b0d5ebaaa d/base.py --- 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('' + title + '') + if title: + e('.markdown h1').html('' + title + '') + else: + e('.markdown').prepend('

' + fallback_title + '

') 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) diff -r 8a9b14686d27 -r 764b0d5ebaaa docs/02-usage.markdown --- 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. diff -r 8a9b14686d27 -r 764b0d5ebaaa publish.sh --- /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