|
9 | 9 | import glob |
10 | 10 | import json |
11 | 11 | import m2r2 |
| 12 | +import numpy as np |
12 | 13 |
|
13 | 14 | mdFiles = [ |
14 | 15 | 'README.md', |
|
19 | 20 |
|
20 | 21 | docSources = 'docs/source' |
21 | 22 |
|
| 23 | +counter = np.array(0) |
| 24 | + |
22 | 25 | with open('docs/emojis.json') as f: |
23 | 26 | emojis = set(json.load(f).keys()) |
24 | 27 |
|
25 | | -def convert(md): |
26 | | - rst = m2r2.parse_from_file(md, parse_relative_links=True) |
| 28 | +def wrappEmojis(rst): |
27 | 29 | for emoji in emojis: |
28 | 30 | rst = rst.replace(emoji, f'|{emoji}|') |
| 31 | + return rst |
| 32 | + |
| 33 | +def addSectionRefs(rst, baseName): |
| 34 | + sections = {} |
| 35 | + lines = rst.splitlines() |
| 36 | + # Search for sections in rst file |
| 37 | + for i in range(len(lines)-2): |
| 38 | + conds = [ |
| 39 | + len(lines[i+1]) and lines[i+1][0] in ['=', '-', '^', '"'], |
| 40 | + lines[i+2] == lines[i-1] == '', |
| 41 | + len(lines[i]) == len(lines[i+1])] |
| 42 | + if all(conds): |
| 43 | + sections[i] = lines[i] |
| 44 | + # Add unique references before each section |
| 45 | + for i, title in sections.items(): |
| 46 | + ref = '-'.join([elt for elt in title.lower().split(' ') if elt != '']) |
| 47 | + for char in ['#', "'", '^', '°', '!']: |
| 48 | + ref = ref.replace(char, '') |
| 49 | + ref = f'{baseName}/{ref}' |
| 50 | + lines[i] = f'.. _{ref}:\n\n'+lines[i] |
| 51 | + # Returns all concatenated lines |
| 52 | + return '\n'.join(lines) |
| 53 | + |
| 54 | +def completeRefLinks(rst, baseName): |
| 55 | + i = 0 |
| 56 | + while i != -1: |
| 57 | + i = rst.find(':ref:`', i) |
| 58 | + if i != -1: |
| 59 | + iLink = rst.find('<', i) |
| 60 | + rst = rst[:iLink+1]+f'{baseName}/'+rst[iLink+1:] |
| 61 | + i += 6 |
| 62 | + return rst |
| 63 | + |
| 64 | +def convert(md): |
29 | 65 | baseName = os.path.splitext(md)[0] |
| 66 | + rst = m2r2.parse_from_file(md, parse_relative_links=True) |
| 67 | + rst = wrappEmojis(rst) |
| 68 | + rst = addSectionRefs(rst, baseName) |
| 69 | + rst = completeRefLinks(rst, baseName) |
30 | 70 | with open(f'{docSources}/{baseName}.rst', 'w') as f: |
31 | 71 | f.write(rst) |
32 | 72 | print(f'Converted {md} to {docSources}/{baseName}.rst') |
|
0 commit comments