Skip to content

Commit 5fa5124

Browse files
committed
Adds article about GitHub Pages plus Sphinx builds
1 parent 00ebc2e commit 5fa5124

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

_posts/articles/2019-04-20-github-pages-python-sphinx.md

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ last_modified_at: Sat Apr 20 09:43:00 CDT 2019
66
categories: articles
77
tags: [github, git, python, sphinx, documentation, developer, docs]
88
image:
9-
path: /images/guarded-lightbulb-rob-sinclair.jpg
10-
caption: "[Flickr rob-sinclair](https://flic.kr/p/8J2gDY)"
9+
path: /images/keyboard-mleung311.jpg
10+
caption: "[Flickr mleung311](https://flic.kr/p/fqJJbW)"
1111
comments: false
1212
share: true
1313
---
@@ -16,10 +16,76 @@ If you prefer to write in ReStructured Text instead of Markdown for your technic
1616

1717
Another great offering is GitHub Pages, with automatic publishing when a known branch, such as master or gh-pages is updated.
1818

19+
So, how about the best of both? Let's dive into two key configurations that enable you to publish Python Sphinx pages to GitHub Pages.
1920

21+
## Naming the GitHub repository can affect the resulting URL
2022

23+
If you want a URL like `<username>.github.io` or ``<orgname>.github.io`, name the repo `<username>.github.io` or `<orgname>.github.io`. Then, by default, GitHub Pages publishes to the URL matching the repo name.
2124

25+
## Enable GitHub Pages for the GitHub repository
2226

23-
I've been working on a talk titled, "Make an Instant Web Site with Webhooks" for DevNet Create. In it, I show how to publish and host on GitHub Pages, which is built to use Jekyll by default, using Python Sphinx instead.
27+
> Note: You must have admin privileges on the repository to see the **Settings** tab.
28+
29+
1. Go to the repository on the GitHub website and make sure you are logged in.
30+
1. Add a `/docs` directory to the `master` branch. Otherwise you do not get the **master branch /docs folder** for the Source option in the drop-down list.
31+
1. Click the **Settings** tab. You first go to the Options section.
32+
1. Scroll down to the **GitHub Pages** section and choose the drop-down list under Source.
33+
> Note: Your choices will differ based on whether you're in a User repo or an Org repo. [Read all about the differences in the GitHub docs](https://help.github.com/en/articles/user-organization-and-project-pages).
34+
35+
1. To keep source and output HTML separate, choose **master branch /docs folder** for Source.
36+
37+
Next, you set up the `.nojekyll` file to indicate you aren't using Jekyll as your static site generator in this repository.
38+
39+
## Add a .nojekyll file in the /docs directory
40+
41+
When GitHub sees a `.nojekyll` file, it serves the root `index.html` file. Read more in the [original blog post about this feature](https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/).
42+
43+
> Note: I'd recommend using the `/docs` directory on the `master` branch, so that you are sure to keep source and output HTML separate. You could also use a `_build` directory in the root of your repo as the served HTML.
44+
45+
1. Create a new branch locally, named `docs-config` in this example, based on `master`:
46+
```
47+
$ git checkout -b docs-config
48+
```
49+
1. Create an empty dot file, named `.nojekyll` within the `/docs` folder of your repo. A dot file has a period prefix and may not be visible in all text editors or your Finder windows, so be aware and don't panic if you can't "see" it later.
50+
1. Use the add command to make sure Git starts tracking the file:
51+
```
52+
$ git add docs/.nojekyll
53+
```
54+
1. Commit the change with the `-a` option to add new files to the commit, and the `-m` option for a message:
55+
```
56+
$ git commit -a -m "Adds a .nojekyll file for docs builds"
57+
```
58+
1. Push the change to the remote, in this case named "origin", so you can compare the change to `master`.
59+
```
60+
$ git push origin docs-config
61+
```
62+
1. On the GitHub website, go to your repository and create a Pull Request.
63+
1. Merge the Pull Request to add the `.nojekyll` file to the `master` branch.
64+
65+
## Create a "docsource" or "docsrc" folder for the source files
66+
67+
Now, in the root of your repository, you can create a source folder for your doc builds:
68+
* If you're working with code and docs in the same repo, you can name the folder `docsource` or `docsrc`.
69+
* If your repo is for only documentation, you can name the folder `source`.
70+
71+
Next, make sure that your `conf.py` file has been set up with these source and destination directories. The Sphinx documentation has a good [Configuration section](http://www.sphinx-doc.org/en/master/usage/configuration.html).
72+
73+
## Make sure you can still build Sphinx locally for reviews
74+
75+
Here's another pro tip I found while browsing [Issues in the Sphinx repository itself](https://github.com/sphinx-doc/sphinx/issues/3382#issuecomment-470772316). Since you want to keep the source and output separate, but still be able to both publish on GitHub Pages and preview builds locally, you can add an option to your `Makefile` to do both.
76+
77+
```
78+
github:
79+
@make html
80+
@cp -a _build/html/. ../docs
81+
```
82+
83+
Now you can run `make github` from the doc source directory to generate a local preview and move the docs where GitHub wants to serve them from.
84+
85+
> Note: Realize that there's a `conf.py` setting for both where the `make` command is run and where files are built to. Read up in the [Configuration section](http://www.sphinx-doc.org/en/master/usage/configuration.html) of the Sphinx docs for all the details.
86+
87+
## Pull it all together
88+
89+
I've been working on a talk titled, "Make an Instant Web Site with Webhooks" for DevNet Create. In it, I show how to publish and host on GitHub Pages, which is built to use Jekyll by default, using Python Sphinx instead. I'll post the slides when it's done. In the meantime, we'd love to see you next week! Visit the [DevNet Create site](https://developer.cisco.com/devnetcreate/2019) for more information.
2490

2591
{% include sign-up.html %}

images/keyboard-mleung311.jpg

561 KB
Loading

0 commit comments

Comments
 (0)