Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Functions about checking, statistics on files used in the documentation. 

4""" 

5import os 

6import re 

7from ..filehelper import explore_folder, explore_folder_iterfile 

8 

9 

10def enumerate_notebooks_link(nb_folder, nb_rst): 

11 """ 

12 Looks for all links to a notebook in the documentation. 

13 

14 @param nb_folder notebook folder 

15 @param nb_rst documentation folder 

16 @return iterator on *(rst_file, nb_file, link type, pos_start, pos_end, string, title)* 

17 

18 The function also outputs unreferenced notebooks. 

19 *rst_file* is None in that case. 

20 

21 Example of outputs: 

22 

23 :: 

24 

25 ('...index_class.rst', '...having_a_form_in_a_notebook.ipynb', 'ref', 79880, 79912, ':ref:`havingaforminanotebookrst`') 

26 ('...index_module.rst', '...having_a_form_in_a_notebook.ipynb', 'ref', 277928, 277960, ':ref:`havingaforminanotebookrst`') 

27 

28 """ 

29 # We check that all readme.txt follow utf-8. 

30 for name in explore_folder_iterfile(nb_folder, "((readme)|(README))[.]txt$", 

31 ".*((checkpoints)|(MACOSX)).*", fullname=True): 

32 with open(name, "r", encoding="utf-8") as f: 

33 try: 

34 nbcontent = f.read() 

35 except UnicodeDecodeError as e: 

36 raise ValueError( 

37 "Issue with file '{0}'".format(name)) from e 

38 reg_title = re.compile("\\\"([#] [^#]+?)\\n") 

39 

40 rsts = explore_folder(nb_rst, ".*[.]rst$")[1] 

41 crsts = {} 

42 for rst in rsts: 

43 with open(rst, "r", encoding="utf-8") as f: 

44 try: 

45 crsts[rst] = f.read() 

46 except UnicodeDecodeError as e: 

47 raise ValueError( 

48 "Issue with file '{0}'".format(rst)) from e 

49 

50 nbcount = {} 

51 

52 for name in explore_folder_iterfile(nb_folder, ".*[.]ipynb$", ".*checkpoints.*", fullname=True): 

53 with open(name, "r", encoding="utf-8") as f: 

54 try: 

55 nbcontent = f.read() 

56 except UnicodeDecodeError as e: 

57 raise ValueError( 

58 "Issue with file '{0}'".format(name)) from e 

59 reg_title = re.compile("\\\"([#] [^#]+?)\\n") 

60 ftitle = reg_title.findall(nbcontent) 

61 if len(ftitle) > 0: 

62 title = ftitle[0].strip(" \n\r\t") 

63 else: 

64 title = None 

65 sh = os.path.splitext(os.path.split(name)[-1])[0] 

66 reg1 = re.compile("[/ ](" + sh + ")\\n") 

67 reg2 = re.compile("(:ref:`.*? <{0}rst>`)".format(sh.replace("_", ""))) 

68 reg3 = re.compile("(:ref:`{0}rst`)".format(sh.replace("_", ""))) 

69 reg4 = re.compile("(<.*?" + sh + ">)\\n") 

70 nbcount[name] = 0 

71 for rst, content in crsts.items(): 

72 iter = reg1.finditer(content) 

73 for it in iter: 

74 nbcount[name] += 1 

75 yield (rst, name, "toctree", it.start(0), it.end(0), it.groups(0)[0], title) 

76 iter = reg4.finditer(content) 

77 for it in iter: 

78 nbcount[name] += 1 

79 yield (rst, name, "toctreen", it.start(0), it.end(0), it.groups(0)[0], title) 

80 iter = reg2.finditer(content) 

81 for it in iter: 

82 nbcount[name] += 1 

83 yield (rst, name, "refn", it.start(0), it.end(0), it.groups(0)[0], title) 

84 iter = reg3.finditer(content) 

85 for it in iter: 

86 nbcount[name] += 1 

87 yield (rst, name, "ref", it.start(0), it.end(0), it.groups(0)[0], title) 

88 if nbcount[name] == 0: 

89 yield (None, name, None, -1, -1, "", title)