#!/usr/bin/env python import sys, os from distutils.core import setup from distutils.command.install_scripts import install_scripts 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: from distutils.command.build_py import build_py_2to3 as build_py except ImportError: if sys.version_info >= (3, 0): raise ImportError("build_py_2to3 is required to build in Python 3.x.") from distutils.command.build_py import build_py version = '2.1.1' # The command line script name. Currently set to "markdown_py" so as not to # conflict with the perl implimentation (which uses "markdown"). We can't use # "markdown.py" as the default config on some systems will cause the script to # try to import itself rather than the library which will raise an error. SCRIPT_NAME = 'markdown_py' class md_install_scripts(install_scripts): """ Customized install_scripts. Create markdown_py.bat for win32. """ def run(self): install_scripts.run(self) if sys.platform == 'win32': try: script_dir = os.path.join(sys.prefix, 'Scripts') script_path = os.path.join(script_dir, SCRIPT_NAME) bat_str = '@"%s" "%s" %%*' % (sys.executable, script_path) bat_path = os.path.join(self.install_dir, '%s.bat' %SCRIPT_NAME) f = open(bat_path, 'w') f.write(bat_str) f.close() print ('Created: %s' % bat_path) except Exception: _, err, _ = sys.exc_info() # for both 2.x & 3.x compatability print ('ERROR: Unable to create %s: %s' % (bat_path, err)) class build_docs(Command): """ Build markdown documentation into html.""" description = '"build" documentation (convert markdown text to html)' user_options = [ ('build-base=', 'd', 'directory to "build" to'), ('force', 'f', 'forcibly build everything (ignore file timestamps)'), ] boolean_options = ['force'] def initialize_options(self): self.build_base = None self.force = None self.docs = None self.sitemap = '' def finalize_options(self): self.set_undefined_options('build', ('build_base', 'build_base'), ('force', 'force')) self.docs = self._get_docs() def _get_docs(self): for root, dirs, files in os.walk('docs'): for file in files: if not file.startswith('_'): path = os.path.join(root, file) yield path def _get_context(self, src, path): """ Build and return context to pass to template. """ # set defaults c = { 'title' : '', 'prev_url' : '', 'prev_title' : '', 'next_url' : '', 'next_title' : '', 'crumb' : '', 'version' : version, } c['body'] = self.md.convert(src) c['toc'] = self.md.toc for k, v in self.md.Meta.items(): c[k] = ' '.join(v) self.md.reset() # Manipulate path path = path.lstrip(os.path.join(self.build_base, 'docs/')) dir, file = os.path.split(path) name, ext = os.path.splitext(file) parts = [x for x in dir.split(os.sep) if x] c['source'] = '%s.txt' % name c['base'] = '../'*len(parts) # Build page title parts = [x.replace('_', ' ').capitalize() for x in parts[1:]] if name.lower() != 'index': parts.append(name.replace('_', ' ').capitalize()) if parts: c['page_title'] = ' | '.join(parts) + ' — Python Markdown' else: c['page_title'] = 'Python Markdown' # Build crumb trail if c['title']: c['crumb'] = '