Skip to main content

Generate Markdown-formatted text based on a Pydantic model.

Project description

Markdantic

概要

Pydantic の型情報を元に mkdocs 等で使用する Markdown 形式のテキストデータを出力する。 このとき、mkdocstrings では表現しきれない型情報を、ロスレスに Markdown 化することを目的とする。

デザインポリシー

本ツールは「見た目よりも型情報の正確さ」を最優先とする。

  • 型は完全修飾クラス (FQCN) でレンダリングする
  • 組み込み型 (int, str, list, None など) は読みやすさを考慮して短縮形でレンダリングする
  • 出力の整形や短縮は行わず、ロスレスな情報提供を優先する
  • Markdown レンダラーとの互換性を優先する
  • テーブルの破損を防ぐため、半角のパイプ記号を全角に変換する

非対応・割り切り

  • Markdown の装飾最適化(見栄え調整)は行わない
  • 出力結果の整形・短縮は利用者側で後処理することを前提とする
  • HTML や PDF の直接生成は対象外とする

Enum の展開

  • 通常の Enum
  • 数値ベースの Enum (IntEnum)

"メンバー値=メンバー名" を展開して description の末尾に出力する。

使用例

mkdocs / mkdocstrings 等での利用を想定した例:

コード

enums.py

from enum import Enum

class Area(Enum):
    TOKYO = 1
    OSAKA = 2

models.py

from pathlib import Path
from pydantic import BaseModel, Field
from enums import Area

class SampleModel(BaseModel):
    """テストのモデル"""
    num:   int | None = Field(None, description="Number of Files")
    name:  str        = Field("", description="Name")
    files: list[Path] = Field(default_factory=list, description="list of Path")
    view:  bool       = Field(False, description="View flag")
    area:  Area       = Field(..., description="Area")

main.py

from markdantic import generator
from models import SampleModel

print(generator(SampleModel))

出力

以下は main.py を実行して生成されるMarkdownの例:

## `SampleModel`

**Base:** `BaseModel`

テストのモデル

**Parameters:**

|Name|Type|Description|Default|
|---|---|---|---|
|`num`|`int|None`|Number of Files|`None`|
|`name`|`str`|Name|`''`|
|`files`|`list[pathlib.Path]`|list of Path|`[]`|
|`view`|`bool`|View flag|`False`|
|`area`|`enums.Area`|Area (members: 1=`TOKYO`, 2=`OSAKA`)|_Required_|

テーブル部分のレンダリング結果:

Name Type Description Default
num int|None Number of Files None
name str Name ''
files list[pathlib.Path] list of Path []
view bool View flag False
area enums.Area Area (members: 1=TOKYO, 2=OSAKA) Required

インストール方法

pip install markdantic

uv を使用する場合:

uv add markdantic

※ PyPI で公開予定。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdantic-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdantic-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file markdantic-0.1.0.tar.gz.

File metadata

  • Download URL: markdantic-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for markdantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2afa1c0ee0182f6c282d1ec520ad96f97bac39a6158075e35faee362a3ebe052
MD5 c942292b3aff0102f00316ee9aa7b9f1
BLAKE2b-256 79e78c743657e522792402de78b029d9199701fe6552615e7b30ee25e4d59229

See more details on using hashes here.

File details

Details for the file markdantic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: markdantic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for markdantic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e02caecbffd3c873d206b7337677b3a5ec0995153a98b4437d2c2c0a50f18283
MD5 859c941fdcc2597879c5d38e2740dfa4
BLAKE2b-256 f4b155102f55ca8a40ab674bf5420f925c40430366df24208ae653e6c7c3b97a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page