Skip to content

Commit

Permalink
The build_docs command now uses extra and toc extensions and a templa…
Browse files Browse the repository at this point in the history
…te on the file system.
  • Loading branch information
Waylan Limberg committed Aug 4, 2011
1 parent 4cabf3b commit 85f355b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
20 changes: 20 additions & 0 deletions docs/_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>%(title)s</title>
<style type="text/css">
.nav { float:right; margin:1em;}
</style>
</head>
<body>
<div id="nav">
<p><a href="/">Index</a></p>
%(toc)s
</div>
<div id="content">
%(body)s
</div>
<p id="foot">&copy; 2010 Python Markdown Project<p>
</body>
</html>
45 changes: 14 additions & 31 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from distutils.command.build import build
from distutils.core import Command
from distutils.util import change_root, newer
import codecs

# Try to run 2to3 automaticaly when building in Python 3.x
try:
Expand Down Expand Up @@ -43,29 +44,6 @@ def run(self):
print ('ERROR: Unable to create %s: %s' % (bat_path, err))


doc_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>%(title)s</title>
<style type="text/css">
.nav { float:right; margin:1em;}
</style>
</head>
<body>
<div id="nav">
%(menu)s
</div>
<div id="content">
"""

doc_footer = """
</div>
<p id="foot">&copy; 2010 Python Markdown Project<p>
</body>
</html>
"""

class build_docs(Command):
""" Build markdown documentation into html."""

Expand Down Expand Up @@ -99,8 +77,9 @@ def finalize_options(self):
def _get_docs(self):
for root, dirs, files in os.walk('docs'):
for file in files:
path = os.path.join(root, file)
yield (path, self._get_page_title(path))
if not file.startswith('_'):
path = os.path.join(root, file)
yield (path, self._get_page_title(path))

def _get_page_title(self, path):
""" Get page title from file name (and path). """
Expand All @@ -125,7 +104,8 @@ def run(self):
except ImportError:
print ('skipping build_docs: Markdown "import" failed!')
else:
md = markdown.Markdown()
template = codecs.open('docs/_template.html', encoding='utf-8').read()
md = markdown.Markdown(extensions=['extra', 'toc'])
menu = md.convert(self.sitemap)
md.reset()
for infile, title in self.docs:
Expand All @@ -138,12 +118,15 @@ def run(self):
if self.verbose:
print ('Converting %s -> %s' % (infile, outfile))
if not self.dry_run:
doc = open(outfile, 'wb')
header = doc_header % {'title': title, 'menu': menu}
doc.write(header.encode('utf-8'))
md.convertFile(infile, doc)
src = codecs.open(infile, encoding='utf-8').read()
out = template % {
'title': title,
'body' : md.convert(src),
'toc' : md.toc,
}
md.reset()
doc.write(doc_footer.encode('utf-8'))
doc = open(outfile, 'wb')
doc.write(out.encode('utf-8'))
doc.close()


Expand Down

0 comments on commit 85f355b

Please sign in to comment.