Coverage for onnxcustom/utils/doc_helper.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-17 01:42 +0100

1""" 

2@file 

3@brief Helpers to improve documentation rendering. 

4""" 

5import re 

6 

7 

8def fix_link_operator_md(markdown): 

9 """ 

10 The redering of file `Operator.md <https://github.com/onnx/onnx/ 

11 blob/master/docs/Operators.md>`_ breaks links. This function 

12 restores some of them. 

13 

14 :param markdown: a string or a filename 

15 :return: modified content 

16 """ 

17 if len(markdown) < 5000 and markdown.endswith('.md'): 

18 with open(markdown, 'r', encoding='utf-8') as f: 

19 content = f.read() 

20 else: 

21 content = markdown # pragma: no cover 

22 

23 reg = re.compile( 

24 "([|]<a href=\\\"#(?P<name>[.A-Za-z]+)\\\">(?P=name)</a>[|])") 

25 pattern = "|[{1}](#a-name-{0}-a-a-name-{0}-{0}-a)|" 

26 

27 lines = content.split('\n') 

28 new_lines = [] 

29 for line in lines: 

30 find = reg.search(line) 

31 if find: 

32 gr = find.groups() 

33 exp = gr[0] 

34 op = gr[1] 

35 rep = pattern.format(op.lower(), op).replace(".", "-") 

36 line = line.replace(exp, rep) 

37 new_lines.append(line) 

38 return "\n".join(new_lines)