| 
									
										
										
										
											2022-01-30 08:19:20 +01:00
										 |  |  | # pyright: basic | 
					
						
							| 
									
										
										
										
											2022-01-31 11:24:45 +01:00
										 |  |  | from typing import Dict, NamedTuple | 
					
						
							| 
									
										
										
										
											2022-01-15 06:09:37 +01:00
										 |  |  | import pkg_resources | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import flask | 
					
						
							| 
									
										
										
										
											2022-01-15 09:34:08 +01:00
										 |  |  | from flask.helpers import url_for | 
					
						
							| 
									
										
										
										
											2022-01-15 09:01:38 +01:00
										 |  |  | import mistletoe | 
					
						
							| 
									
										
										
										
											2022-01-15 06:09:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | from . import get_setting | 
					
						
							|  |  |  | from .version import GIT_URL | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 11:24:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class HelpPage(NamedTuple): | 
					
						
							|  |  |  |     title: str | 
					
						
							|  |  |  |     content: str | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 13:23:49 +01:00
										 |  |  | # Whenever a new .md file is added to help/ it needs to be added here | 
					
						
							|  |  |  | _TOC = ('about',) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 11:24:45 +01:00
										 |  |  | PAGES: Dict[str, HelpPage] = {} | 
					
						
							|  |  |  | """ Maps a filename under help/ without the file extension to the rendered page. """ | 
					
						
							| 
									
										
										
										
											2022-01-15 06:09:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def render(app: flask.Flask): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Renders the user documentation. Must be called after all Flask routes have been | 
					
						
							|  |  |  |     registered, because the documentation might try to link to them with Flask's `url_for`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     We render the user documentation once on startup to improve performance. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-15 09:34:08 +01:00
										 |  |  |     link_targets = { | 
					
						
							|  |  |  |         'brand.git_url': GIT_URL, | 
					
						
							|  |  |  |         'brand.public_instances': get_setting('brand.public_instances'), | 
					
						
							|  |  |  |         'brand.docs_url': get_setting('brand.docs_url'), | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     base_url = get_setting('server.base_url') or None | 
					
						
							|  |  |  |     # we specify base_url so that url_for works for base_urls that have a non-root path | 
					
						
							| 
									
										
										
										
											2022-01-15 06:09:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-15 09:34:08 +01:00
										 |  |  |     with app.test_request_context(base_url=base_url): | 
					
						
							|  |  |  |         link_targets['url_for:index'] = url_for('index') | 
					
						
							|  |  |  |         link_targets['url_for:preferences'] = url_for('preferences') | 
					
						
							|  |  |  |         link_targets['url_for:stats'] = url_for('stats') | 
					
						
							| 
									
										
										
										
											2022-01-15 06:09:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-15 09:34:08 +01:00
										 |  |  |     define_link_targets = ''.join(f'[{name}]: {url}\n' for name, url in link_targets.items()) | 
					
						
							| 
									
										
										
										
											2022-01-15 06:09:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 13:23:49 +01:00
										 |  |  |     for pagename in _TOC: | 
					
						
							|  |  |  |         file_content = pkg_resources.resource_string(__name__, 'help/' + pagename + '.md').decode() | 
					
						
							| 
									
										
										
										
											2022-01-31 11:24:45 +01:00
										 |  |  |         markdown = define_link_targets + file_content | 
					
						
							|  |  |  |         assert file_content.startswith('# ') | 
					
						
							|  |  |  |         title = file_content.split('\n', maxsplit=1)[0].strip('# ') | 
					
						
							|  |  |  |         content: str = mistletoe.markdown(markdown) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 13:23:49 +01:00
										 |  |  |         if pagename == 'about': | 
					
						
							| 
									
										
										
										
											2022-01-31 11:24:45 +01:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 content += pkg_resources.resource_string(__name__, 'templates/__common__/aboutextend.html').decode() | 
					
						
							|  |  |  |             except FileNotFoundError: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2022-01-31 13:23:49 +01:00
										 |  |  |         PAGES[pagename] = HelpPage(title=title, content=content) |