-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path06_formatters.py
More file actions
241 lines (182 loc) · 6.45 KB
/
06_formatters.py
File metadata and controls
241 lines (182 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""Example 6: Formatters and Utilities - Working with markdown formatting.
This example shows how to use the formatting utilities
to create well-structured markdown documents.
"""
from code2docs.formatters.markdown import MarkdownFormatter
from code2docs.formatters.badges import generate_badges
from code2docs.formatters.toc import generate_toc, extract_headings
# =============================================================================
# MARKDOWN FORMATTER
# =============================================================================
def markdown_formatting_examples() -> None:
"""Demonstrate markdown formatting utilities."""
formatter = MarkdownFormatter()
# Headings
print(formatter.heading("Main Title", level=1))
print(formatter.heading("Section", level=2))
print(formatter.heading("Subsection", level=3))
# Text formatting
print(formatter.paragraph("This is a paragraph of text."))
print(formatter.bold("Bold text"))
print(formatter.italic("Italic text"))
print(formatter.code("inline_code"))
# Code blocks
code = """def hello():
print("Hello, World!")"""
print(formatter.code_block(code, language="python"))
# Lists
items = ["First item", "Second item", "Third item"]
print(formatter.list_item("First item"))
print(formatter.list_item("Second item"))
print(formatter.list_item("Nested item", indent=1))
# Blockquotes
print(formatter.blockquote("This is a quoted text"))
# Links and images
print(formatter.link("Visit GitHub", "https://github.com"))
print(formatter.image("Logo", "./logo.png"))
def generate_complex_document() -> str:
"""Generate a complex markdown document using the formatter."""
f = MarkdownFormatter()
sections = []
# Title
sections.append(f.heading("API Documentation", level=1))
sections.append("")
# Overview
sections.append(f.heading("Overview", level=2))
sections.append(f.paragraph(
"This document describes the API endpoints and usage."
))
sections.append("")
# Authentication section
sections.append(f.heading("Authentication", level=2))
sections.append(f.paragraph(
f"All API requests require an {f.bold('API key')} in the header."
))
sections.append(f.code_block(
'Authorization: Bearer YOUR_API_KEY',
language="http"
))
sections.append("")
# Endpoints
sections.append(f.heading("Endpoints", level=2))
# Example endpoint
sections.append(f.heading("GET /api/users", level=3))
sections.append(f.paragraph("List all users."))
sections.append("**Response:**")
sections.append(f.code_block(
'{"users": [{"id": 1, "name": "John"}]}',
language="json"
))
sections.append("")
# Table example
sections.append(f.heading("Error Codes", level=2))
sections.append(f.table(
headers=["Code", "Description"],
rows=[
["400", "Bad Request"],
["401", "Unauthorized"],
["404", "Not Found"],
["500", "Internal Server Error"],
]
))
return "\n".join(sections)
# =============================================================================
# BADGES
# =============================================================================
def badge_examples() -> None:
"""Generate various badge examples."""
from code2docs.analyzers.dependency_scanner import DependencyScanner
# Generate version badge
badges = generate_badges(
project_name="My Project",
badge_types=["version", "python", "license"],
stats={
"coverage": 85,
"complexity": "A",
"lines": 5000,
},
deps=["numpy", "pandas", "requests"]
)
print("Generated badges:")
for badge in badges.split("\n"):
print(f" {badge}")
# =============================================================================
# TABLE OF CONTENTS
# =============================================================================
def toc_examples() -> None:
"""Demonstrate table of contents generation."""
markdown_content = """
# Main Title
## Introduction
Some intro text.
## Getting Started
### Installation
Instructions here.
### Configuration
Config details.
## API Reference
### Functions
Function docs.
### Classes
Class docs.
"""
# Generate TOC
toc = generate_toc(markdown_content, max_depth=3)
print("Table of Contents:")
print(toc)
# Extract headings
print("\nExtracted headings:")
headings = extract_headings(markdown_content, max_depth=2)
for level, text, anchor in headings:
print(f" Level {level}: {text} (#{anchor})")
# =============================================================================
# COMBINED EXAMPLE: README GENERATOR
# =============================================================================
def build_custom_readme() -> str:
"""Build a custom README using formatters."""
f = MarkdownFormatter()
parts = []
# Header with badges
parts.append(f.heading("My Awesome Project", level=1))
parts.append("")
parts.append(generate_badges(
project_name="My Project",
badge_types=["version", "python", "license"],
stats={"coverage": 90}
))
parts.append("")
# Description
parts.append(f.paragraph(
"A powerful Python library for automating documentation generation."
))
parts.append("")
# TOC
readme_content = "\n".join(parts)
toc = generate_toc(readme_content + "\n## Features\n\n## Installation\n\n## Usage\n\n## API\n")
parts.append(f.heading("Table of Contents", level=2))
parts.append(toc)
parts.append("")
# Features
parts.append(f.heading("Features", level=2))
features = [
"Automatic README generation",
"API documentation extraction",
"Module-level documentation",
"Dependency graph visualization",
]
for feature in features:
parts.append(f.list_item(feature))
parts.append("")
# Installation
parts.append(f.heading("Installation", level=2))
parts.append(f.code_block("pip install my-project", language="bash"))
parts.append("")
return "\n".join(parts)
if __name__ == "__main__":
# Run examples
# markdown_formatting_examples()
# print(generate_complex_document())
# badge_examples()
# toc_examples()
# print(build_custom_readme())
pass