mbcp/litedoc/style/markdown.py

60 lines
1.5 KiB
Python
Raw Normal View History

2024-08-28 12:02:30 +08:00
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/28 下午3:39
@Author : snowykami
@Email : snowykami@outlook.com
@File : markdown.py
@Software: PyCharm
"""
from typing import Optional
from litedoc.syntax.astparser import AstParser
from litedoc.syntax.node import *
from litedoc.i18n import get_text
2024-08-28 12:02:30 +08:00
def generate(parser: AstParser, lang: str, frontmatter: Optional[dict] = None) -> str:
"""
Generate markdown style document from ast
You can modify this function to generate markdown style that enjoys you
Args:
parser:
lang: language
frontmatter:
Returns:
markdown style document
"""
if frontmatter is not None:
md = "---\n"
for k, v in frontmatter.items():
md += f"{k}: {v}\n"
md += "---\n"
else:
md = ""
# var > func > class
"""遍历函数"""
2024-08-28 12:02:30 +08:00
for func in parser.functions:
if func.name.startswith("_"):
continue
2024-08-28 12:02:30 +08:00
md += func.markdown(lang)
"""遍历类"""
2024-08-28 12:02:30 +08:00
for cls in parser.classes:
md += cls.markdown(lang)
2024-08-28 12:02:30 +08:00
"""遍历变量"""
2024-08-28 21:27:52 +08:00
for var in parser.variables:
md += f"### ***var*** `{var.name} = {var.value}`\n\n"
if var.type != TypeHint.NO_TYPEHINT:
md += f"- **{get_text(lang, 'type')}**: `{var.type}`\n\n"
if var.docs is not None:
md += f"- **{get_text(lang, 'desc')}**: {var.docs}\n\n"
2024-08-28 21:27:52 +08:00
2024-08-28 12:02:30 +08:00
return md