Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import re | |
| # Function to read markdown files with error handling | |
| def load_markdown_file(path): | |
| try: | |
| with open(path, 'r', encoding='utf-8') as file: | |
| content = file.read() | |
| return content | |
| except Exception as e: | |
| return f"Error reading file: {e}" | |
| # Function to create a simple TOC from markdown content | |
| def create_toc(markdown_content): | |
| toc = [] | |
| for line in markdown_content.split('\n'): | |
| if line.startswith('#'): | |
| level = line.count('#') | |
| title = line.strip('# ').strip() | |
| anchor = title.lower().replace(' ', '-').replace('/', '-').replace('.', '') | |
| toc.append((level, title, anchor)) | |
| return toc | |
| # Streamlit UI Enhancements | |
| st.title('π Documentation') | |
| docs_base_path = './docs' | |
| if not os.path.exists(docs_base_path): | |
| st.error('Documentation directory does not exist.') | |
| else: | |
| try: | |
| categories = [d for d in os.listdir(docs_base_path) if os.path.isdir(os.path.join(docs_base_path, d))] | |
| if not categories: | |
| st.error('No categories found in the documentation directory.') | |
| else: | |
| st.sidebar.markdown('# π§· Navigation') | |
| category = st.sidebar.selectbox('π Select the Space', categories) | |
| pages_path = os.path.join(docs_base_path, category) | |
| pages = [f for f in os.listdir(pages_path) if os.path.isfile(os.path.join(pages_path, f)) and f.endswith('.md')] | |
| if not pages: | |
| st.error('No markdown pages found in the selected category.') | |
| else: | |
| page = st.sidebar.selectbox('π Select the Page', pages) | |
| markdown_path = os.path.join(pages_path, page) | |
| markdown_content = load_markdown_file(markdown_path) | |
| # Create TOC | |
| toc = create_toc(markdown_content) | |
| if toc: | |
| st.sidebar.markdown('# π Table of Contents') | |
| for level, title, anchor in toc: | |
| st.sidebar.markdown(f"{' ' * (level-1) * 2}- [{title}](#{anchor})") | |
| # Display markdown with potential anchors | |
| modified_markdown_content = re.sub(r'(#{1,6}) (.*)', r'\1 <a name="\2"></a>\2', markdown_content) | |
| st.markdown(modified_markdown_content, unsafe_allow_html=True) | |
| except Exception as e: | |
| st.error(f"Error listing categories or pages: {e}") | |