Comments (1)
This is extremely hacky and far from perfect, but got about 90% of the links updated (most of the others were due to incorrect input data, but there were a couple of edge cases I just fixed manually also). I started out using the newer pathlib library, but later realized that it doesn't generate relative links that aren't in each others paths (so can't do ../../a/b
etc), so switched to os for that.
from pathlib import Path
import re
import yaml
import os
import pprint
with open(".config/redirects.yml") as config:
redirects = yaml.safe_load(config)
lookup = {}
reverse = {}
for redirect in redirects:
lookup[re.sub("/$", "", redirect['from_url']) + ".md"] = re.sub("/$", "", redirect['to_url']) + ".md"
reverse[re.sub("/$", "", redirect['to_url']) + ".md"] = re.sub("/$", "", redirect['from_url']) + ".md"
print(reverse)
rootdir = Path('.')
for new in rootdir.glob('**/*.md'):
if new.is_file() and not str(new).startswith('docs/'):
orig = new
if str(new) in reverse.keys():
orig = Path(reverse[str(new)])
print("orig: " + str(orig))
print("new: " + str(new))
orig_dir = orig.parent
new_dir = new.parent
with open(new) as file:
data = file.read()
links = re.finditer('\[[^\]]+\]\(([^)]+)\)', data)
for link in links:
url = link.group(1)
if ":" not in url:
print(" url: " + url)
parts = re.search('([^#]*)(#.*|)', url)
if len(parts.group(1)) > 0:
print(" file: " + parts.group(1))
print(" anchor: " + parts.group(2))
resolved = Path(str(orig_dir) + '/' + url).resolve().relative_to(Path('.').resolve())
print(" resolved: " + str(resolved))
dest = resolved
if str(resolved) in lookup.keys():
dest = Path(lookup[str(resolved)])
dest = os.path.relpath(str(dest), str(new_dir))
print(" dest: " + dest)
final = str(dest) + parts.group(2)
data = data.replace(url, final)
with open(new, "w") as file:
file.write(data)
with open(".config/mkdocs.yml") as file:
data = file.read()
for redirect in redirects:
data = data.replace(re.sub("/$", "", redirect['from_url']) + ".md", re.sub("/$", "", redirect['to_url']) + ".md")
with open(".config/mkdocs.yml", "w") as file:
file.write(data)
from guidebook.
Related Issues (20)
- Add "Updated on [date]"
- Broken link to EFF intro HOT 8
- Add nav reviewer for changes to mkdocs.yml HOT 2
- Proof of concept, submodule markdown files from another repo HOT 3
- Disable read the docs flyout menu
- Add instructions to configure git commit signing to the guidebook HOT 4
- Added underline text decoration for links in the content HOT 3
- Add support for markdown tables
- Have dependabot track mkdocs insiders (somehow?) HOT 2
- Link checker reporting URLs as not available (status 0 or other statuses) when they are actually available HOT 1
- Add a Front End practice area page under Practice Areas / Engineering
- Improve readability of guidebook navigation labels
- Accessibility issues to resolve HOT 6
- Purpose of the asterisk at the end of each of the holiday dates HOT 3
- Insecure recommendation in Screen lock ("Securing Your Workspace") documentation HOT 1
- Linkchecker updates (internal) and ignoring links that are 403/401s.
- Add documentation/instructions on updating navigation for editors HOT 6
- Wave a11y report errors for material theme HOT 3
- Got a few pages with links that have double (and more) anchors
- Consider adding a section for managing government furnished equipment
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guidebook.