Skip to content

Commit

Permalink
Allow redirect URLs file to contain markdown lists
Browse files Browse the repository at this point in the history
- This allows redirect files to put redirects on lines starting with '- ' so
  the file renders better on github.com as a list.
  • Loading branch information
durden committed Apr 6, 2016
1 parent 7fba78d commit a831556
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 4 additions & 3 deletions docs/github_repo_layout.rst
Expand Up @@ -64,9 +64,10 @@ redirects.md

This file contains mapping of old guide URLs to new URLs. The purpose of this
file is to accomodate changing guide titles/paths and maintaining old URLs with
temporary 301 redirects. The format of this file is <old_url> <new_url> i.e
space separated. Keep in mind the URLs must be fully formed including the
domain otherwise the redirect will be based on the current domain.
temporary 301 redirects. The format of this file is '`<old_url> <new_url>`' or
'`- <old_urL> <new_url>`' i.e space separated and as an optional markdown list
item. Keep in mind the URLs must be fully formed including the domain otherwise
the redirect will be based on the current domain.

This file is optional and must be manually created.

Expand Down
18 changes: 14 additions & 4 deletions pskb_website/models/file.py
Expand Up @@ -130,6 +130,8 @@ def read_redirects(branch=u'master'):
This means redirect http://www.xyz.com to http://www.xyz.com/1 and redirect
http://www.xyz.com/2 to http://www.xyz.com/3.
Each line can start with an optional '- ', which will be ignored.
Any lines starting with a '#' or not containing two tokens is ignored.
"""

Expand All @@ -152,10 +154,18 @@ def read_redirects(branch=u'master'):
if line.startswith('#'):
continue

try:
old, new = line.split()
except ValueError:
# Not valid line, needs exactly 2 tokens
tokens = line.split()

# A valid line is either 3 tokens one of which is a '-' to start a
# markdown list item or 2 tokens (old and new url).
if len(tokens) == 3 and tokens[0] == '-':
old = tokens[1]
new = tokens[2]
elif len(tokens) == 2:
old = tokens[0]
new = tokens[1]
else:
# Not valid line, needs at least 2 tokens
continue

redirects[old] = new
Expand Down

0 comments on commit a831556

Please sign in to comment.