99CURDIR = Path (__file__ ).resolve ().parent
1010COVER_IMAGE = ('https://raw.githubusercontent.com/ipython-books/'
1111 'cookbook-2nd/master/cover-cookbook-2nd.png' )
12+ URLIZE_RE = r'(?<!"|\(|\+)(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))'
1213
1314
1415def get_readme ():
@@ -21,7 +22,7 @@ def process_header(contents, metadata=''):
2122 title = contents [i + 2 :j ]
2223 contents = contents [:i ] + contents [j :]
2324 contents = f'title: { title } \n ' + metadata + '\n ' + contents
24- return contents
25+ return contents
2526
2627
2728def process_urls (contents ):
@@ -45,25 +46,40 @@ def process_ad(contents):
4546 '*This is one of the 100+ free recipes of the [IPython Cookbook, '
4647 'Second Edition](/)' ,
4748 )
48-
49- contents = re .sub (r'\[\*Chapter [^\]]+\*\]\(\.\/\)' , '' , contents )
49+
50+ contents = re .sub (r'\[(\*Chapter [^\]]+\*)\]\((\.\/)\)' ,
51+ r"[\1]({filename}index.md)" ,
52+ contents )
5053
5154 return contents
5255
5356
5457def process_code (contents ):
5558
5659 contents = re .sub (r'\{output:[^\}]+\}' , 'output' , contents )
57-
5860 contents = re .sub (r'!\[([^\]]+)\]\(([^\)\{]+)\)' , r'' , contents )
61+ contents = re .sub (r'^([0-9]+)\. ' , r'**\1. ** ' , contents , flags = re .M )
62+ contents = re .sub (URLIZE_RE , r'[\1](\1)' , contents )
63+
64+ # lines = []
65+ # to_indent = False
66+ # for line in contents.splitlines():
67+ # if re.match(r'^[1-9]{1,}\. ', line):
68+ # to_indent = True
69+ # elif re.match(r'^[\#]{1,} ', line):
70+ # to_indent = False
71+ # if to_indent and not re.match(r'^[1-9]{1,}\. ', line):
72+ # line = ' ' + line
73+ # lines.append(line)
74+ # return '\n'.join(lines)
5975
6076 return contents
6177
6278
6379def create_index ():
6480 readme = get_readme ()
6581 readme = process_header (readme , 'save_as: index.html' )
66-
82+
6783 readme = readme .replace ('This repository contains the sources of the book (in Markdown, ' ,
6884 'Most of the book is freely available on this website (' )
6985
@@ -98,7 +114,7 @@ def create_chapter(chapter):
98114
99115 write_file (index_in , index_out )
100116 for file in sorted (chapter .glob ('*.md' )):
101- if file .name == 'README.md' :
117+ if file .name in ( 'README.md' , '00_intro.md' ) :
102118 continue
103119 fin = CURDIR / f'../{ chapter .name } /{ file .name } '
104120 fout = CURDIR / f'content/pages/{ chapter .name } /{ file .name } '
@@ -122,4 +138,3 @@ def create_chapter(chapter):
122138 chapters = sorted ((CURDIR / '../' ).glob ('chapter*' ))
123139 for chapter in chapters :
124140 create_chapter (chapter )
125- exit ()
0 commit comments