new.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import logging
  2. import os
  3. config_text = 'site_name: My Docs\n'
  4. index_text = """# Welcome to MkDocs
  5. For full documentation visit [mkdocs.org](https://www.mkdocs.org).
  6. ## Commands
  7. * `mkdocs new [dir-name]` - Create a new project.
  8. * `mkdocs serve` - Start the live-reloading docs server.
  9. * `mkdocs build` - Build the documentation site.
  10. * `mkdocs -h` - Print help message and exit.
  11. ## Project layout
  12. mkdocs.yml # The configuration file.
  13. docs/
  14. index.md # The documentation homepage.
  15. ... # Other markdown pages, images and other files.
  16. """
  17. log = logging.getLogger(__name__)
  18. def new(output_dir):
  19. docs_dir = os.path.join(output_dir, 'docs')
  20. config_path = os.path.join(output_dir, 'mkdocs.yml')
  21. index_path = os.path.join(docs_dir, 'index.md')
  22. if os.path.exists(config_path):
  23. log.info('Project already exists.')
  24. return
  25. if not os.path.exists(output_dir):
  26. log.info('Creating project directory: %s', output_dir)
  27. os.mkdir(output_dir)
  28. log.info('Writing config file: %s', config_path)
  29. with open(config_path, 'w', encoding='utf-8') as f:
  30. f.write(config_text)
  31. if os.path.exists(index_path):
  32. return
  33. log.info('Writing initial docs: %s', index_path)
  34. if not os.path.exists(docs_dir):
  35. os.mkdir(docs_dir)
  36. with open(index_path, 'w', encoding='utf-8') as f:
  37. f.write(index_text)