GithubHelp home page GithubHelp logo

Comments (1)

grugnog avatar grugnog commented on August 23, 2024 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.