From a83155605492dd7da65af662de1e3d937f56be68 Mon Sep 17 00:00:00 2001 From: Luke Lee Date: Wed, 6 Apr 2016 10:32:38 +0200 Subject: [PATCH] Allow redirect URLs file to contain markdown lists - This allows redirect files to put redirects on lines starting with '- ' so the file renders better on github.com as a list. --- docs/github_repo_layout.rst | 7 ++++--- pskb_website/models/file.py | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/github_repo_layout.rst b/docs/github_repo_layout.rst index 2750dbb..ea88402 100644 --- a/docs/github_repo_layout.rst +++ b/docs/github_repo_layout.rst @@ -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 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 '` `' or +'`- `' 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. diff --git a/pskb_website/models/file.py b/pskb_website/models/file.py index 058420e..20fe834 100644 --- a/pskb_website/models/file.py +++ b/pskb_website/models/file.py @@ -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. """ @@ -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