From c05f5f175e7027752357fa4c931ce4677e7f1c6e Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Thu, 12 Mar 2015 06:57:47 +0530 Subject: [PATCH] Added linkfix script to docs/utils https://github.com/scrapy/scrapy/pull/1041#issuecomment-78143576 --- docs/utils/linkfix.py | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 docs/utils/linkfix.py diff --git a/docs/utils/linkfix.py b/docs/utils/linkfix.py new file mode 100755 index 000000000..40316968f --- /dev/null +++ b/docs/utils/linkfix.py @@ -0,0 +1,63 @@ +#!/usr/bin/python + +""" + +Linkfix - a companion to sphinx's linkcheck builder. + +Uses the linkcheck's output file to fix links in docs. + +Originally created for this issue: +https://github.com/scrapy/scrapy/issues/606 + +Author: dufferzafar +""" + +import re + +# Used for remembering the file (and its contents) +# so we don't have to open the same file again. +_filename = None +_contents = None + +# A regex that matches standard linkcheck output lines +line_re = re.compile(ur'(.*)\:\d+\:\s\[(.*)\]\s(?:(.*)\sto\s(.*)|(.*))') + +# Read lines from the linkcheck output file +try: + with open("build/linkcheck/output.txt") as out: + output_lines = out.readlines() +except IOError: + print("linkcheck output not found; please run linkcheck first.") + exit(1) + +# For every line, fix the respective file +for line in output_lines: + match = re.match(line_re, line) + + if match: + newfilename = match.group(1) + errortype = match.group(2) + + # Broken links can't be fixed and + # I am not sure what do with the local ones. + if errortype.lower() in ["broken", "local"]: + print("Not Fixed: " + line) + else: + # If this is a new file + if newfilename != _filename: + + # Update the previous file + if _filename: + with open(_filename, "w") as _file: + _file.write(_contents) + + _filename = newfilename + + # Read the new file to memory + with open(_filename) as _file: + _contents = _file.read() + + _contents = _contents.replace(match.group(3), match.group(4)) + else: + # We don't understand what the current line means! + print("Not Understood: " + line)