11import os
22import pathlib
3- from .io_utils import read_file , data_name , prepend_baseurl
3+ from .io_utils import read_file , data_name , prepend_baseurl , read_yaml
4+ import yaml
45from .jekyll import create_jekyll_file , create_jekyll_content_header , get_colab_url
56from .notebook import generate_notebook_html
67from .md import build_markdown
@@ -31,11 +32,26 @@ def process_content(builder, link, kind):
3132 metadata_path = link .get ('metadata' , os .path .join (content_dir , 'metadata.yml' ))
3233 metadata_path = \
3334 f'{ metadata_path } { "" if metadata_path .endswith (".yml" ) or metadata_path .endswith (".yaml" ) else ".yml" } '
35+
36+ metadata = None
37+ if kind == 'md' :
38+ raw_str = read_file (filename )
39+ lines = raw_str .splitlines ()
40+ if lines [0 ] == '---' :
41+ try :
42+ idx = lines .index ('---' , 1 )
43+ metadata = yaml .full_load ('\n ' .join (lines [1 :idx ]))
44+ raw_str = '\n ' .join (lines [idx + 1 :])
45+ except ValueError :
46+ pass
47+ content_str = build_markdown (builder = builder , raw = raw_str ,
48+ assets_target_dir = builder .assets_dir )
3449 if os .path .isfile (metadata_path ):
35- metadata = builder .build_index (
50+ file_metadata = builder .build_index (
3651 index_file = metadata_path .replace (".ipynb" if kind == "notebook" else ".md" , '' ),
3752 assets_src_dir = content_dir , assets_target_dir = builder .assets_dir , index_type = 'content' ,
3853 search_slug = jekyll_target_file )
54+ metadata = file_metadata if metadata is None else {** metadata , ** file_metadata }
3955 metadata_path = data_name (index_file = metadata_path .replace (".ipynb" if kind == "notebook" else ".md" , '' ),
4056 index_type = 'content' )
4157 if 'text' not in link :
@@ -47,19 +63,24 @@ def process_content(builder, link, kind):
4763 link ['description' ] = metadata ['header' ]['description' ]
4864
4965 else :
50- metadata = dict (search = builder .config ['search' ])
51- metadata_path = None
66+ metadata_path = None if metadata is None else data_name (index_file = metadata_path .replace (
67+ ".ipynb" if kind == "notebook" else ".md" , '' ), index_type = 'content' )
68+ metadata = dict (search = builder .config ['search' ]) if metadata is None else builder .build_index (
69+ index = metadata , target_data_file = metadata_path .replace (".ipynb" if kind == "notebook" else ".md" , '' ),
70+ assets_src_dir = content_dir , assets_target_dir = builder .assets_dir , index_type = 'content' ,
71+ search_slug = jekyll_target_file , search_links = None , search_content = None )
5272
5373 link ['kind' ] = metadata .get ('kind' , kind .capitalize ()) if 'kind' not in link else link ['kind' ]
5474
5575 builder .checked_content [filename ] = link
56- content_str = generate_notebook_html (
57- builder = builder ,
58- src = filename ,
59- assets_dir = builder .assets_dir , # where to move notebook assets
60- search_links = metadata ['search' ]['links' ]
61- ) if kind == 'notebook' else build_markdown (builder = builder , raw = read_file (filename ),
62- assets_target_dir = builder .assets_dir )
76+ if kind == 'notebook' :
77+ content_str = generate_notebook_html (
78+ builder = builder ,
79+ src = filename ,
80+ assets_dir = builder .assets_dir , # where to move notebook assets
81+ search_links = metadata ['search' ]['links' ]
82+ )
83+
6384 if metadata ['search' ]['content' ]:
6485 builder .add_search_content (
6586 jekyll_target_file , content = content_str , title = link .get ('text' , kind .capitalize ()),
0 commit comments