Skip to content

Commit f5fc313

Browse files
Merge pull request #7 from encode/cleanup
Clean up
2 parents ef4c6a9 + 2d83748 commit f5fc313

8 files changed

Lines changed: 174 additions & 30 deletions

File tree

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# MkDocs
2+
3+
MkDocs is a smart, simple, website design tool.
4+
5+
## Installation
6+
7+
Install the `mkdocs` command line tool...
8+
9+
```shell
10+
$ pip install mkdocs
11+
```
12+
13+
## Getting started
14+
15+
1. Create a `README.md` page.
16+
2. Run `mkdocs serve` to view your documentation in a browser.
17+
3. Run `mkdocs build` to build a static website ready to host.
18+
19+
MkDocs supports [GitHub Flavored Markdown](writing.md) for page authoring.
20+
21+
## Adding pages
22+
23+
1. Create additional markdown pages.
24+
2. Use markdown interlinking between pages.
25+
3. Create a `mkdocs.toml` config file to define the site navigation.
26+
27+
## Custom styling
28+
29+
1. Create a `templates/base.html` file to customise the layout.
30+
2. Create a `css/base.css` file to override the default styles.
31+
3. Create a `js/base.css` file to override the default scripts.
32+
33+
Simple [styling adaptations](styling.md) include customising the colour scheme, the typography, or choosing the code highlighting style.

docs/configuration.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Configuration
2+
3+
Site navigation and other configuration is defined in a `mkdocs.toml` file.
4+
5+
```toml
6+
[mkdocs]
7+
nav = [
8+
{title="Introduction", path="index.md"},
9+
{title="Writing Markdown", path="writing.md"},
10+
{title="Site Navigation", path="navigation.md"},
11+
{title="HTML Styling", path="styling.md"}
12+
]
13+
```
14+
15+
Navigation can include subsections...
16+
17+
```toml
18+
[mkdocs]
19+
nav = [
20+
{title="Introduction", path="README.md"},
21+
{title="Guides", children=[
22+
{title="Writing Markdown", path="writing.md"},
23+
{title="Site Navigation", path="navigation.md"},
24+
{title="HTML Styling", path="styling.md"}
25+
]}
26+
]
27+
```
28+
29+
### Loaders
30+
31+
Loaders determine where documentation resources are collected from.
32+
33+
```toml
34+
loaders = [
35+
{type="directory", dir="docs"},
36+
{type="package", pkg="mkdocs.theme"}
37+
]
38+
```
39+
40+
Customise this to load a different theme, from a ZIP archive...
41+
42+
```toml
43+
loaders = [
44+
{type="directory", dir="docs"},
45+
{type="url", url="https://www.example.com/theme.zip"}
46+
]
47+
```
48+
49+
Or from a GitHub repo...
50+
51+
```toml
52+
loaders = [
53+
{type="directory", dir="docs"},
54+
{type="github", url="mkdocs/default"}
55+
]
56+
```
57+
58+
<!--
59+
### Markdown
60+
61+
The derault set of extensions are geared towards GitHub Flavored Markdown.
62+
63+
```toml
64+
[markdown]
65+
extensions = {...}
66+
config = {...}
67+
```
68+
69+
### Resources
70+
71+
Resources include markdown pages, static media, and HTML templates.
72+
73+
* `pages` - `*.md`
74+
* `statics` - `*`
75+
* `templates` - `templates/*`
76+
77+
### Commands
78+
79+
The command line tool allows you to work with mkdocs locally,
80+
or to build and host your sites on a provider of your choosing.
81+
82+
* `mkdocs serve`
83+
* `mkdocs build`
84+
-->

docs/index.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
# MkDocs
22

3-
MkDocs is a smart, simple website design tool using [Markdown](writing.md), [templated HTML](styling.md#templates), and static [media files](styling.md#media).
3+
MkDocs is a smart, simple, website design tool.
44

55
## Installation
66

7-
Install the mkdocs command line tool...
7+
Install the `mkdocs` command line tool...
88

99
```shell
1010
$ pip install mkdocs
1111
```
1212

1313
## Getting started
1414

15-
1. Create a `README.md` file, and get authoring.
15+
1. Create a `README.md` page.
1616
2. Run `mkdocs serve` to view your documentation in a browser.
1717
3. Run `mkdocs build` to build a static website ready to host.
1818

1919
MkDocs supports [GitHub Flavored Markdown](writing.md) for page authoring.
2020

2121
## Adding pages
2222

23-
1. Create other markdown files, such as `CONTRIBUTING.md`.
24-
2. Link between your documents, eg. `See the [contributing](CONTRIBUTING.md) page for more details`.
25-
3. Create a `mkdocs.json` config file to add site navigation.
26-
27-
Sites can be single page, multipage, or deeply nested. Just use regular interlinking between your markdown documents, with `mkdocs.json` to define an overall [site layout](navigation.md).
23+
1. Create additional markdown pages.
24+
2. Use markdown interlinking between pages.
25+
3. Create a `mkdocs.toml` config file to define the site navigation.
2826

2927
## Custom styling
3028

docs/navigation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Back to the [homepage](../index.md).
3737

3838
You can include site-wide navigation by using the `mkdocs.toml` configuration.
3939

40-
```
40+
```toml
4141
[mkdocs]
4242
version = 2
4343

@@ -56,7 +56,7 @@ This allows the theme to display navigation controls, as well as including `←
5656

5757
The navigation configuration can also include nested elements.
5858

59-
```
59+
```toml
6060
[mkdocs]
6161
version = 2
6262

docs/styling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Styling is handled with HTML templating and regular web design.
44

55
## Templates
66

7-
Anything in the `/templates/` directory is treated as a [Jinja template](https://jinja.palletsprojects.com/en/stable/templates/), and is used to render pages, rather than being served directly. You can override templates locally and adapt them to make layout changes.
7+
Anything in the `/templates/` directory is treated as a [Jinja template](https://jinja.palletsprojects.com/en/stable/templates/), and is used to render markdown pages. You can override templates locally and adapt them to make layout changes.
88

99
The base template for rendering markdown pages is **`templates/base.html`**.
1010

src/mkdocs/mkdocs.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# import io
12
import pathlib
23
import typing
4+
# import zipfile
35

46
import httpx
57
import jinja2
@@ -110,6 +112,32 @@ def __repr__(self):
110112
return f'<Package {self._pkg!r}>'
111113

112114

115+
# class ZipURL(Handler):
116+
# def __init__(self, url: str) -> None:
117+
# self._url = url
118+
# self._topdir = ''
119+
120+
# def load_paths(self) -> list[pathlib.Path]:
121+
# r = httpx.get(self._url)
122+
# b = io.BytesIO(r.body)
123+
# with zipfile.ZipFile(b, 'r') as zip_ref:
124+
# names = [
125+
# pathlib.PosixPath(name) for name in zip_ref.namelist()
126+
# if not name.endswith('/')
127+
# ]
128+
# if len(set([name.parts[0] for name in names])) == 1:
129+
# self._topdir = names[0].parts[0]
130+
# names = [pathlib.PosixPath(*name.parts[1:]) for name in names]
131+
# return names
132+
133+
# def read(self, path: pathlib.Path) -> bytes:
134+
# r = httpx.get(self._url)
135+
# b = io.BytesIO(r.body)
136+
# with zipfile.ZipFile(b, 'r') as zip_ref:
137+
# path = f"{self._topdir}/{path}" if self._topdir else path
138+
# with zip_ref.open(str(path)) as f:
139+
# return f.read()
140+
113141
# Resources & Templates...
114142

115143
class Resource:
@@ -157,7 +185,9 @@ def get_source(self, environment, template: str):
157185

158186

159187
class MkDocs:
160-
def __init__(self):
188+
def __init__(self, config: str = '', theme: str = ''):
189+
self.config = config or 'mkdocs.toml'
190+
self.theme = theme
161191
self.content_types = {
162192
".json": "application/json",
163193
".js": "application/javascript",
@@ -175,7 +205,7 @@ def path_to_url(self, path: pathlib.Path) -> str:
175205
# 'index.html' -> '/'
176206
return pathlib.Path('/').joinpath(path).parent.as_posix().lower()
177207
if path.name.lower() in ('readme.md', 'index.md', 'index.html'):
178-
# 'topics/README.md' -> '/'
208+
# 'topics/README.md' -> '/topics/'
179209
# 'topics/index.html' -> '/topics/'
180210
return pathlib.Path('/').joinpath(path).parent.as_posix().lower() + '/'
181211
elif path.suffix == '.md':
@@ -196,12 +226,12 @@ def load_config(self, filename: str) -> dict:
196226
except tomllib.TOMLDecodeError as exc:
197227
raise ConfigError(f"Invalid TOML in config '{filename}'\n{exc}")
198228

199-
if 'mkdocs' not in config:
200-
raise ConfigError(f"Config '{filename}' missing '[mkdocs]' section.")
201-
if 'version' not in config['mkdocs']:
202-
raise ConfigError(f"Config '{filename}' missing 'version=...' in '[mkdocs]' section.")
203-
if config['mkdocs']['version'] != 2:
204-
raise ConfigError(f"Config '{filename}' expected 'version=2' in '[mkdocs]' section.")
229+
# if 'mkdocs' not in config:
230+
# raise ConfigError(f"Config '{filename}' missing '[mkdocs]' section.")
231+
# if 'version' not in config['mkdocs']:
232+
# raise ConfigError(f"Config '{filename}' missing 'version=...' in '[mkdocs]' section.")
233+
# if config['mkdocs']['version'] != 2:
234+
# raise ConfigError(f"Config '{filename}' expected 'version=2' in '[mkdocs]' section.")
205235

206236
default = {
207237
'mkdocs': {
@@ -211,19 +241,13 @@ def load_config(self, filename: str) -> dict:
211241
'title': 'MkDocs',
212242
'favicon': '📘',
213243
'nav': [],
214-
# 'nav': [
215-
# {'title': 'Introduction', 'path': 'README.md'},
216-
# {'title': 'Writing content', 'path': 'writing.md'},
217-
# {'title': 'Site navigation', 'path': 'navigation.md'},
218-
# {'title': 'Themes & styling', 'path': 'styling.md'},
219-
# ],
220244
},
221245
# 'handlers': [
222246
# {'type': 'mkdocs.Package', 'pkg': 'mkdocs:theme'}
223247
# {'type': 'mkdocs.Directory', 'dir': 'docs'}
224248
# ],
225249
'markdown': {
226-
'extensions': {
250+
'extensions': [
227251
'fenced_code',
228252
'footnotes',
229253
'tables',
@@ -233,7 +257,7 @@ def load_config(self, filename: str) -> dict:
233257
'mkdocs.rewrite_urls',
234258
'mkdocs.short_codes',
235259
'mkdocs.strike_thru',
236-
},
260+
],
237261
'configs': {
238262
'footnotes': {'BACKLINK_TITLE': ''},
239263
'toc': {'anchorlink': True, 'marker': ''}
@@ -246,6 +270,11 @@ def load_config(self, filename: str) -> dict:
246270
return Config(config, filename=filename)
247271

248272
def load_handlers(self, config: dict) -> list[Handler]:
273+
if not pathlib.Path('docs').is_dir():
274+
return [
275+
Package('mkdocs'),
276+
Directory('.'),
277+
]
249278
return [
250279
Package('mkdocs'),
251280
Directory('docs'),

src/mkdocs/theme/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<head>
33
<meta charset="utf-8">
44
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5-
<title>{{ config['site']['title'] }}</title>
5+
<title>{{ config.site.title }}</title>
66
<link rel="icon" href="data:image/svg+xml,{% include 'favicon.svg' %}">
77
<link rel="stylesheet" href="/css/highlightjs.min.css">
88
<link rel="stylesheet" href="/css/highlightjs-copy.min.css">
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{% if page.previous or page.next %}
22
<div class="pagination">
33
{% if page.previous %}
4-
<a class="previous" href="{{ page.previous['url']}}">← {{ page.previous['title']}}</a>
4+
<a class="previous" href="{{ page.previous.url }}">← {{ page.previous.title }}</a>
55
{% endif %}
66
{% if page.next %}
7-
<a class="next" href="{{ page.next['url']}}">{{ page.next['title']}} →</a>
7+
<a class="next" href="{{ page.next.url }}">{{ page.next.title }} →</a>
88
{% endif %}
99
</div>
1010
{% endif %}

0 commit comments

Comments
 (0)