Build an index from a keyword designating a doc to _DocInfo objects.
(src_dir)
| 333 | |
| 334 | |
| 335 | def build_doc_index(src_dir): |
| 336 | """Build an index from a keyword designating a doc to _DocInfo objects.""" |
| 337 | doc_index = {} |
| 338 | if not os.path.isabs(src_dir): |
| 339 | raise ValueError("'src_dir' must be an absolute path.\n" |
| 340 | " src_dir='%s'" % src_dir) |
| 341 | |
| 342 | if not os.path.exists(src_dir): |
| 343 | raise ValueError("'src_dir' path must exist.\n" |
| 344 | " src_dir='%s'" % src_dir) |
| 345 | |
| 346 | for dirpath, _, filenames in os.walk(src_dir): |
| 347 | suffix = os.path.relpath(path=dirpath, start=src_dir) |
| 348 | for base_name in filenames: |
| 349 | if not base_name.endswith('.md'): |
| 350 | continue |
| 351 | title_parser = _GetMarkdownTitle() |
| 352 | title_parser.process(os.path.join(dirpath, base_name)) |
| 353 | if title_parser.title is None: |
| 354 | msg = ('`{}` has no markdown title (# title)'.format( |
| 355 | os.path.join(dirpath, base_name))) |
| 356 | raise ValueError(msg) |
| 357 | key_parts = os.path.join(suffix, base_name[:-3]).split('/') |
| 358 | if key_parts[-1] == 'index': |
| 359 | key_parts = key_parts[:-1] |
| 360 | doc_info = _DocInfo(os.path.join(suffix, base_name), title_parser.title) |
| 361 | doc_index[key_parts[-1]] = doc_info |
| 362 | if len(key_parts) > 1: |
| 363 | doc_index['/'.join(key_parts[-2:])] = doc_info |
| 364 | |
| 365 | return doc_index |
| 366 | |
| 367 | |
| 368 | class _GuideRef(object): |