Skip to content

Commit 0abdaba

Browse files
Create generator.py
1 parent 48f11e6 commit 0abdaba

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

sphinx_autodoc_vyper/generator.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""Sphinx documentation generator for Vyper contracts."""
2+
3+
import os
4+
from typing import List
5+
from .parser import Contract
6+
7+
8+
class SphinxGenerator:
9+
"""Generate Sphinx documentation for Vyper contracts."""
10+
11+
def __init__(self, output_dir: str):
12+
self.output_dir = output_dir
13+
self.docs_dir = os.path.join(output_dir, 'docs')
14+
os.makedirs(self.docs_dir, exist_ok=True)
15+
16+
def generate(self, contracts: List[Contract]):
17+
"""Generate Sphinx documentation."""
18+
self._generate_conf_py()
19+
self._generate_index_rst(contracts)
20+
self._generate_contract_docs(contracts)
21+
22+
def _generate_conf_py(self):
23+
"""Generate Sphinx configuration file."""
24+
conf_content = '''# Configuration file for Sphinx documentation
25+
26+
project = 'Vyper Smart Contracts'
27+
copyright = '2023'
28+
author = 'Vyper Developer'
29+
30+
extensions = [
31+
'sphinx.ext.autodoc',
32+
'sphinx.ext.napoleon',
33+
'sphinx.ext.viewcode'
34+
]
35+
36+
templates_path = ['_templates']
37+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
38+
39+
html_theme = 'sphinx_rtd_theme'
40+
html_static_path = ['_static']
41+
'''
42+
with open(os.path.join(self.docs_dir, 'conf.py'), 'w', encoding='utf-8') as f:
43+
f.write(conf_content)
44+
45+
def _generate_index_rst(self, contracts: List[Contract]):
46+
"""Generate index.rst file."""
47+
content = '''Vyper Smart Contracts Documentation
48+
================================
49+
50+
.. toctree::
51+
:maxdepth: 2
52+
:caption: Contents:
53+
54+
'''
55+
for contract in contracts:
56+
content += f' {contract.name}\n'
57+
58+
with open(os.path.join(self.docs_dir, 'index.rst'), 'w', encoding='utf-8') as f:
59+
f.write(content)
60+
61+
def _generate_contract_docs(self, contracts: List[Contract]):
62+
"""Generate documentation for each contract."""
63+
for contract in contracts:
64+
content = f'''{contract.name}
65+
{'=' * len(contract.name)}
66+
67+
'''
68+
if contract.docstring:
69+
content += f'{contract.docstring}\n\n'
70+
71+
if contract.functions:
72+
content += 'Functions\n---------\n\n'
73+
for func in contract.functions:
74+
params = ', '.join(f'{p.name}: {p.type}' for p in func.params)
75+
return_type = f' -> {func.return_type}' if func.return_type else ''
76+
content += f'.. py:function:: {func.name}({params}){return_type}\n\n'
77+
78+
if func.docstring:
79+
content += f' {func.docstring}\n\n'
80+
81+
with open(os.path.join(self.docs_dir, f'{contract.name}.rst'), 'w', encoding='utf-8') as f:
82+
f.write(content)

0 commit comments

Comments
 (0)