Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from dataclasses import dataclass, field
  4. import inspect
  5. import os
  6. import re
  7. import sys
  8. from typing import List
  9.  
  10. def log(message):
  11. open('log.txt', 'a').write('{}: {}\n'.format(inspect.currentframe().f_back.f_lineno, message))
  12.  
  13. def log_flush():
  14. open('log.txt', 'w').write('')
  15.  
  16. # TODO: Use regex, something like : list(filter(re.compile(FILE_EXCLUDE).match, ['phabricator.schema.json']))
  17. 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']
  18. DIR_EXCLUDE = ['_resources']
  19.  
  20. class NoPageTitleError(Exception):
  21. pass
  22.  
  23. SECTION_TEMPLATE = '''<ul class="content-nav-section expandable" data-nav-section="{section}">
  24. <button class="content-nav-button"></button>
  25. <div class="close--icon">
  26. <svg class="mdi-icon material-icons" width="24" height="24" fill="#000" viewBox="0 0 24 24">
  27. <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>
  28. </svg>
  29. </div>
  30. <li class="content-nav-no-hover">
  31. <a class="content-nav-section-header" href="/{section}">{section_title}</a>
  32. <ul class="content-nav-section-group">
  33. {section_group_items}
  34. </ul>
  35. </li>
  36. </ul>'''
  37.  
  38. SECTION_GROUP_ITEM_TEMPLATE = '''<li><a href="{section_group_item_url}">{section_group_item_title}</a></li>
  39. <li class="content-nav-no-hover">
  40. <ul class="content-nav-section-subsection">
  41. {section_group_items}
  42. </ul>
  43. </li>'''
  44.  
  45. SUB_SECTION_TEMPLATE = '''<li><a href="{section_group_subsection_item_url}">{section_group_subsection_item_title}</a></li>
  46. <li class="content-nav-no-hover" data-sub-section-item="{section_group_subsection_item_title}")>
  47. <ul>
  48. {section_group_subsection_items}
  49. </ul>
  50. </li>'''
  51.  
  52. @dataclass
  53. class MenuItem:
  54. title: str
  55. content: str
  56.  
  57. @dataclass
  58. class MenuItems:
  59. items:List[MenuItem] = field(default_factory=list)
  60.  
  61. def add(self, item:MenuItem):
  62. if type(item) == str:
  63. raise Exception
  64.  
  65. self.items.append(item)
  66.  
  67. def sorted(self) -> List[MenuItem]:
  68. return sorted(self.items, key=lambda menu_item: menu_item.title)
  69.  
  70. @property
  71. def count(self) -> int:
  72. return len(self.items)
  73. @property
  74. def contents(self) -> List[str]:
  75. return [item.content for item in self.sorted()]
  76.  
  77. def get_sections():
  78. sections = MenuItems()
  79.  
  80. for section in os.listdir('./'):
  81. if not os.path.isdir(section) or section in DIR_EXCLUDE:
  82. continue
  83.  
  84. section_title = get_title('{}/index.md'.format(section))
  85.  
  86. sections.add(
  87. MenuItem(
  88. title=section_title,
  89. content=SECTION_TEMPLATE.format(
  90. section=section,
  91. section_title=section_title,
  92. section_group_items=get_section_items(section).content
  93. )
  94. )
  95. )
  96.  
  97. return '\n'.join(sections.contents)
  98.  
  99. def get_section_items(section):
  100. section_items = sections = MenuItems()
  101. section_title = get_title('{}/index.md'.format(section))
  102.  
  103. for section_item in os.listdir(section):
  104. if section_item in FILE_EXCLUDE or section_item in DIR_EXCLUDE:
  105. continue
  106.  
  107. section_item_file_path = '{}/{}'.format(section, section_item)
  108.  
  109. if os.path.isdir(section_item_file_path):
  110. section_items.add(
  111. get_section_group_item(section, section_item, section_item_file_path)
  112. )
  113. continue
  114.  
  115. title = get_title(section_item_file_path)
  116. section_items.add(
  117. MenuItem(
  118. title=title,
  119. content=make_menu_item(file_path_to_url(section_item_file_path), title)
  120. )
  121. )
  122.  
  123. return MenuItem(
  124. title=section_title,
  125. content='\n'.join(section_items.contents)
  126. )
  127.  
  128. def get_section_group_item(section, section_item, url) -> str:
  129. section_group_items = MenuItems()
  130.  
  131. section_group_item_file_path = '{}/{}'.format(section, section_item, section_item)
  132. section_group_item_title = get_title('{}/index.md'.format(section_group_item_file_path))
  133. section_group_item_url = file_path_to_url(section_group_item_file_path)
  134.  
  135. for section_group_item_child in os.listdir('{}/{}'.format(section, section_item)):
  136. if section_group_item_child in FILE_EXCLUDE:
  137. continue
  138.  
  139. section_group_item_child_path = '{}/{}/{}'.format(section, section_item, section_group_item_child)
  140. section_group_item_child_url = file_path_to_url(section_group_item_child_path)
  141.  
  142. if os.path.isdir(section_group_item_child_path):
  143. section_group_items.add(
  144. get_section_group_subsection_item(section, section_item, section_group_item_child)
  145. )
  146. continue
  147.  
  148. else:
  149. section_group_item_child_title = get_title(section_group_item_child_path)
  150. section_group_items.add(
  151. MenuItem(
  152. title=section_group_item_child_title,
  153. content=make_menu_item(section_group_item_child_url, section_group_item_child_title)
  154. )
  155. )
  156.  
  157. if section_group_items.count <= 1:
  158. return MenuItem(
  159. title=section_group_item_title,
  160. content=make_menu_item(section_group_item_url, section_group_item_title)
  161. )
  162.  
  163. return MenuItem(
  164. title=section_group_item_title,
  165. content=SECTION_GROUP_ITEM_TEMPLATE.format(
  166. section_group_item_title=section_group_item_title,
  167. section_group_item_url=section_group_item_url,
  168. section_group_items='\n'.join(section_group_items.contents)
  169. )
  170. )
  171.  
  172. def get_section_group_subsection_item(section, section_item, section_group_item_child) -> str:
  173. section_group_subsection_items = MenuItems()
  174.  
  175. section_group_subsection_item_file_path = '{}/{}/{}'.format(section, section_item, section_group_item_child)
  176. section_group_subsection_item_title = get_title('{}/index.md'.format(section_group_subsection_item_file_path))
  177. section_group_subsection_item_url = file_path_to_url(section_group_subsection_item_file_path)
  178.  
  179. for section_group_subsection_item_child in os.listdir('{}/{}/{}'.format(section, section_item, section_group_item_child)):
  180. section_group_subsection_item_child_path = '{}/{}/{}/{}'.format(section, section_item, section_group_item_child, section_group_subsection_item_child)
  181. section_group_subsection_item_child_url = file_path_to_url(section_group_subsection_item_child_path)
  182.  
  183. if section_group_subsection_item_child in FILE_EXCLUDE or os.path.isdir(section_group_subsection_item_child_path):
  184. continue
  185.  
  186. section_group_subsection_item_child_title = get_title(section_group_subsection_item_child_path)
  187.  
  188. section_group_subsection_items.add(
  189. MenuItem(
  190. title=section_group_subsection_item_child_title,
  191. content=make_menu_item(section_group_subsection_item_child_url, get_title(section_group_subsection_item_child_path))
  192. )
  193. )
  194.  
  195. return MenuItem(
  196. title=section_group_subsection_item_title,
  197. content=SUB_SECTION_TEMPLATE.format(
  198. section_group_subsection_item_url=section_group_subsection_item_url,
  199. section_group_subsection_item_title=section_group_subsection_item_title,
  200. section_group_subsection_items='\n'.join(section_group_subsection_items.contents)
  201. ))
  202.  
  203. def make_menu_item(url:str, text:str) -> str:
  204. return '<li><a href="{}">{}</a></li>'.format(url, text)
  205.  
  206. def get_title(file_path:str) -> str:
  207. for line in open(file_path).read().split('\n'):
  208. try:
  209. return re.search('^# (.*)$', line).groups()[0]
  210. except Exception:
  211. continue
  212.  
  213. raise NoPageTitleError('No page title found for file {}'.format(file_path))
  214.  
  215. def file_path_to_url(file_path):
  216. return '/{}'.format(file_path.replace('.md', ''))
  217.  
  218. if __name__ == '__main__':
  219. log_flush()
  220. sys.stdout.write(get_sections())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement