Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from dataclasses import dataclass, field
- import inspect
- import os
- import re
- import sys
- from typing import List
- def log(message):
- open('log.txt', 'a').write('{}: {}\n'.format(inspect.currentframe().f_back.f_lineno, message))
- def log_flush():
- open('log.txt', 'w').write('')
- # TODO: Use regex, something like : list(filter(re.compile(FILE_EXCLUDE).match, ['phabricator.schema.json']))
- FILE_EXCLUDE = ['.DS_Store', '.mypy_cache', 'index.md', 'img', 'aws_codecommit.schema.json', 'other_external_service.schema.json', 'bitbucket_server.schema.json', 'phabricator.schema.json', 'gitolite.schema.json', 'github.schema.json', 'gitlab.schema.json', 'critical.schema.json', 'site.schema.json']
- DIR_EXCLUDE = ['_resources']
- class NoPageTitleError(Exception):
- pass
- SECTION_TEMPLATE = '''<ul class="content-nav-section expandable" data-nav-section="{section}">
- <button class="content-nav-button"></button>
- <div class="close--icon">
- <svg class="mdi-icon material-icons" width="24" height="24" fill="#000" viewBox="0 0 24 24">
- <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"></path>
- </svg>
- </div>
- <li class="content-nav-no-hover">
- <a class="content-nav-section-header" href="/{section}">{section_title}</a>
- <ul class="content-nav-section-group">
- {section_group_items}
- </ul>
- </li>
- </ul>'''
- SECTION_GROUP_ITEM_TEMPLATE = '''<li><a href="{section_group_item_url}">{section_group_item_title}</a></li>
- <li class="content-nav-no-hover">
- <ul class="content-nav-section-subsection">
- {section_group_items}
- </ul>
- </li>'''
- SUB_SECTION_TEMPLATE = '''<li><a href="{section_group_subsection_item_url}">{section_group_subsection_item_title}</a></li>
- <li class="content-nav-no-hover" data-sub-section-item="{section_group_subsection_item_title}")>
- <ul>
- {section_group_subsection_items}
- </ul>
- </li>'''
- @dataclass
- class MenuItem:
- title: str
- content: str
- @dataclass
- class MenuItems:
- items:List[MenuItem] = field(default_factory=list)
- def add(self, item:MenuItem):
- if type(item) == str:
- raise Exception
- self.items.append(item)
- def sorted(self) -> List[MenuItem]:
- return sorted(self.items, key=lambda menu_item: menu_item.title)
- @property
- def count(self) -> int:
- return len(self.items)
- @property
- def contents(self) -> List[str]:
- return [item.content for item in self.sorted()]
- def get_sections():
- sections = MenuItems()
- for section in os.listdir('./'):
- if not os.path.isdir(section) or section in DIR_EXCLUDE:
- continue
- section_title = get_title('{}/index.md'.format(section))
- sections.add(
- MenuItem(
- title=section_title,
- content=SECTION_TEMPLATE.format(
- section=section,
- section_title=section_title,
- section_group_items=get_section_items(section).content
- )
- )
- )
- return '\n'.join(sections.contents)
- def get_section_items(section):
- section_items = sections = MenuItems()
- section_title = get_title('{}/index.md'.format(section))
- for section_item in os.listdir(section):
- if section_item in FILE_EXCLUDE or section_item in DIR_EXCLUDE:
- continue
- section_item_file_path = '{}/{}'.format(section, section_item)
- if os.path.isdir(section_item_file_path):
- section_items.add(
- get_section_group_item(section, section_item, section_item_file_path)
- )
- continue
- title = get_title(section_item_file_path)
- section_items.add(
- MenuItem(
- title=title,
- content=make_menu_item(file_path_to_url(section_item_file_path), title)
- )
- )
- return MenuItem(
- title=section_title,
- content='\n'.join(section_items.contents)
- )
- def get_section_group_item(section, section_item, url) -> str:
- section_group_items = MenuItems()
- section_group_item_file_path = '{}/{}'.format(section, section_item, section_item)
- section_group_item_title = get_title('{}/index.md'.format(section_group_item_file_path))
- section_group_item_url = file_path_to_url(section_group_item_file_path)
- for section_group_item_child in os.listdir('{}/{}'.format(section, section_item)):
- if section_group_item_child in FILE_EXCLUDE:
- continue
- section_group_item_child_path = '{}/{}/{}'.format(section, section_item, section_group_item_child)
- section_group_item_child_url = file_path_to_url(section_group_item_child_path)
- if os.path.isdir(section_group_item_child_path):
- section_group_items.add(
- get_section_group_subsection_item(section, section_item, section_group_item_child)
- )
- continue
- else:
- section_group_item_child_title = get_title(section_group_item_child_path)
- section_group_items.add(
- MenuItem(
- title=section_group_item_child_title,
- content=make_menu_item(section_group_item_child_url, section_group_item_child_title)
- )
- )
- if section_group_items.count <= 1:
- return MenuItem(
- title=section_group_item_title,
- content=make_menu_item(section_group_item_url, section_group_item_title)
- )
- return MenuItem(
- title=section_group_item_title,
- content=SECTION_GROUP_ITEM_TEMPLATE.format(
- section_group_item_title=section_group_item_title,
- section_group_item_url=section_group_item_url,
- section_group_items='\n'.join(section_group_items.contents)
- )
- )
- def get_section_group_subsection_item(section, section_item, section_group_item_child) -> str:
- section_group_subsection_items = MenuItems()
- section_group_subsection_item_file_path = '{}/{}/{}'.format(section, section_item, section_group_item_child)
- section_group_subsection_item_title = get_title('{}/index.md'.format(section_group_subsection_item_file_path))
- section_group_subsection_item_url = file_path_to_url(section_group_subsection_item_file_path)
- for section_group_subsection_item_child in os.listdir('{}/{}/{}'.format(section, section_item, section_group_item_child)):
- section_group_subsection_item_child_path = '{}/{}/{}/{}'.format(section, section_item, section_group_item_child, section_group_subsection_item_child)
- section_group_subsection_item_child_url = file_path_to_url(section_group_subsection_item_child_path)
- if section_group_subsection_item_child in FILE_EXCLUDE or os.path.isdir(section_group_subsection_item_child_path):
- continue
- section_group_subsection_item_child_title = get_title(section_group_subsection_item_child_path)
- section_group_subsection_items.add(
- MenuItem(
- title=section_group_subsection_item_child_title,
- content=make_menu_item(section_group_subsection_item_child_url, get_title(section_group_subsection_item_child_path))
- )
- )
- return MenuItem(
- title=section_group_subsection_item_title,
- content=SUB_SECTION_TEMPLATE.format(
- section_group_subsection_item_url=section_group_subsection_item_url,
- section_group_subsection_item_title=section_group_subsection_item_title,
- section_group_subsection_items='\n'.join(section_group_subsection_items.contents)
- ))
- def make_menu_item(url:str, text:str) -> str:
- return '<li><a href="{}">{}</a></li>'.format(url, text)
- def get_title(file_path:str) -> str:
- for line in open(file_path).read().split('\n'):
- try:
- return re.search('^# (.*)$', line).groups()[0]
- except Exception:
- continue
- raise NoPageTitleError('No page title found for file {}'.format(file_path))
- def file_path_to_url(file_path):
- return '/{}'.format(file_path.replace('.md', ''))
- if __name__ == '__main__':
- log_flush()
- sys.stdout.write(get_sections())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement