|
| 1 | +# how to run: |
| 2 | +# docker run -v "${HOME}/Documents/jittor-blog":/srv/jittor-blog -v /home/jittor/Documents/site:/mnt/jittor-blog -e LC_ALL=C.UTF-8 --rm jittor-blog-compiler bash -c "jekyll build --baseurl=JITTOR_BASEURL -d /mnt/jittor-blog/ && chmod -R 777 /mnt/jittor-blog" |
| 3 | +# python /home/jittor/Documents/jittor-blog/local_doc_builder.py |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +os.chdir("/home/jittor/Documents/site") |
| 8 | + |
| 9 | +def check(dirname, fname): |
| 10 | + with open(os.path.join(dirname, fname), 'r') as f: |
| 11 | + src = f.read() |
| 12 | + ac = "JITTOR_BASEURL" |
| 13 | + rep = ( |
| 14 | + ("href=\"//", "href=\"http://"), |
| 15 | + ("src=\"//", "src=\"http://"), |
| 16 | + ('https://cg.cs.tsinghua.edu.cn/jittor', ac) |
| 17 | + ) |
| 18 | + found = False |
| 19 | + for a,b in rep: |
| 20 | + if a in src: |
| 21 | + src = src.replace(a, b) |
| 22 | + found = True |
| 23 | + if ac not in src and not found: return |
| 24 | + n = len(dirname.split('/'))-1 |
| 25 | + s = '.' + '/..' * n |
| 26 | + new_src = "" |
| 27 | + i = -1 |
| 28 | + print("="*20) |
| 29 | + print(dirname, fname) |
| 30 | + while True: |
| 31 | + i += 1 |
| 32 | + if i >= len(src): |
| 33 | + break |
| 34 | + if src[i] != 'J': |
| 35 | + new_src += src[i] |
| 36 | + continue |
| 37 | + if src[i:i+len(ac)] != ac: |
| 38 | + new_src += src[i] |
| 39 | + continue |
| 40 | + j = i |
| 41 | + while j<len(src) and src[j] != ' ' and src[j] != '"' and src[j] != "'": |
| 42 | + j += 1 |
| 43 | + x = src[i:j] |
| 44 | + y = x.replace(ac, s) |
| 45 | + if '#' in y: |
| 46 | + y, l = y.split('#') |
| 47 | + l = '#'+l |
| 48 | + else: |
| 49 | + l = "" |
| 50 | + # replace xx/xx/ --> xx/xx/index.html |
| 51 | + if y.endswith('/'): |
| 52 | + y += 'index.html' |
| 53 | + else: |
| 54 | + z = y.split('/')[-1] |
| 55 | + # replace xx/xx --> xx/xx/index.html |
| 56 | + if '.' not in z: |
| 57 | + y += '/index.html' |
| 58 | + y += l |
| 59 | + print("found", x, '-->', y) |
| 60 | + new_src += y |
| 61 | + i = j-1 |
| 62 | + with open(os.path.join(dirname, fname), 'w') as f: |
| 63 | + f.write(new_src) |
| 64 | + |
| 65 | +for r, _, f in os.walk('.'): |
| 66 | + for fname in f: |
| 67 | + ext = fname.split('.')[-1] |
| 68 | + if ext not in ['html', 'css', 'js']: |
| 69 | + continue |
| 70 | + # print(r, fname) |
| 71 | + check(r, fname) |
| 72 | + |
0 commit comments