Debian Codegraph @

mkdocs/commands/new.py

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