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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#
# Copyright (C) Advanced Micro Devices. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import re
import os
from pathlib import Path
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata
class GoApiRefDirective(Directive):
"""
Directive for generating Go API reference documentation.
Usage:
.. go-api-ref:: path/to/gofile.go
:section: gpu
"""
required_arguments = 1 # Requires one argument: the path to the Go file
optional_arguments = 0
has_content = False
option_spec = {
"section": directives.unchanged, # Optional section filter
}
def run(self):
# Get the path to the Go file
go_file_path = self.arguments[0]
env = self.state.document.settings.env
# Get the section filter if provided
section_filter = self.options.get("section", None)
# Resolve the path relative to the document
doc_dir = Path(env.doc2path(env.docname)).parent
source_path = (doc_dir / go_file_path).resolve()
# Check if the file exists
if not source_path.exists():
msg = f"Go source file not found: {source_path}"
return [nodes.warning("", nodes.paragraph("", msg))]
# Parse the Go file and generate documentation
functions = parse_go_file(str(source_path))
# Create a container for the API documentation
container = nodes.container()
container["classes"].append("go-api-reference")
# Add the API documentation to the container
content = generate_rst_content(functions, section_filter)
self.state_machine.insert_input(content, source=str(source_path))
return [container]
def parse_go_file(file_path):
"""Parse a Go file and extract function documentation."""
with open(file_path, "r") as f:
content = f.read()
# Pattern to match function documentation and definition
pattern = r"(\/\/[^\n]*(?:\n\/\/[^\n]*)*)\n\s*func\s+([A-Za-z0-9_]+)\s*\((.*?)\)\s*(\(.*?\)|\w+)\s*\{"
matches = re.findall(pattern, content, re.DOTALL)
functions = []
for match in matches:
doc_comment = match[0]
func_name = match[1]
params = match[2].strip()
return_type = match[3].strip()
# Process the comment lines
doc_lines = []
for line in doc_comment.split("\n"):
if line.strip().startswith("//"):
# Remove the comment marker and one space after it (if present)
comment_text = line.strip()[2:]
if comment_text.startswith(" "):
comment_text = comment_text[1:]
doc_lines.append(comment_text)
# Extract sections from the doc comment
description = []
input_params = []
output_params = []
example = []
current_section = "description"
for line in doc_lines:
if line.startswith("Input parameter"):
current_section = "input"
input_params.append(line)
elif line.startswith("Output:"):
current_section = "output"
output_params.append(line)
elif line.startswith("Example:"):
current_section = "example"
example.append(line)
elif current_section == "description":
description.append(line)
elif current_section == "input":
input_params.append(line)
elif current_section == "output":
output_params.append(line)
elif current_section == "example":
example.append(line)
# Combine description lines into a single line
desc_text = " ".join([line.strip() for line in description if line.strip()])
# Combine output lines into a single line
output_text = " ".join([line.strip() for line in output_params if line.strip()])
# Determine the section based on function name
parts = func_name.split("_")
section = parts[1] if len(parts) > 1 else "other"
functions.append(
{
"name": func_name,
"params": params,
"return_type": return_type,
"description": desc_text,
"input_params": "\n".join(input_params).strip(),
"output_params": output_text,
"example": "\n".join(example).strip(),
"section": section.lower(), # Store the section for filtering
}
)
return functions
def generate_rst_content(functions, section_filter=None):
"""Generate reStructuredText content from parsed functions."""
lines = []
# Filter functions by section if a filter is provided
if section_filter:
section_filter = section_filter.lower()
functions = [f for f in functions if f["section"] == section_filter]
if not functions:
lines.append(f"No functions found in section: {section_filter}")
return lines
# Group functions by prefix if no section filter is provided
if not section_filter:
# Group functions by prefix (e.g., GO_gpu_, GO_cpu_)
function_groups = {}
for func in functions:
section = func["section"]
if section not in function_groups:
function_groups[section] = []
function_groups[section].append(func)
# Define the order of sections (GPU first, then CPU, then others)
section_order = []
# Add GPU section first if it exists
if "gpu" in function_groups:
section_order.append("gpu")
# Add CPU section next if it exists
if "cpu" in function_groups:
section_order.append("cpu")
# Add all other sections in alphabetical order
for prefix in sorted(function_groups.keys()):
if prefix not in ["gpu", "cpu"]:
section_order.append(prefix)
# Write each group in the specified order
for section in section_order:
funcs = function_groups[section]
lines.append(f"{section.upper()} Functions")
lines.append("-" * len(f"{section.upper()} Functions"))
lines.append("")
for func in funcs:
add_function_documentation(lines, func)
else:
# If a section filter is provided, just document those functions without section headers
for func in functions:
add_function_documentation(lines, func)
return lines
def add_function_documentation(lines, func):
"""Add documentation for a single function to the lines list."""
lines.append(func['name'])
lines.append("~" * len(f"``{func['name']}``"))
lines.append("")
# Function signature
return_type = func["return_type"]
if return_type.startswith("(") and return_type.endswith(")"):
return_type = return_type[1:-1]
lines.append(".. code-block:: go")
lines.append("")
lines.append(f" func {func['name']}({func['params']}) {return_type}")
lines.append("")
# Description
if func["description"]:
lines.append(func["description"])
lines.append("")
# Input parameters
if func["input_params"]:
for input_line in func["input_params"].split("\n"):
lines.append(input_line)
lines.append("")
# Output parameters
if func["output_params"]:
lines.append(func["output_params"])
lines.append("")
# Example
if func["example"]:
# Process the example to properly format code blocks
example_lines = func["example"].split("\n")
in_code_block = False
for i, line in enumerate(example_lines):
stripped_line = line.strip()
# Check if this is the Example: line
if stripped_line == "Example:":
lines.append("Example:")
continue
# Check if we're entering a code block
if (
not in_code_block
and i > 0
and (
stripped_line.startswith("import")
or stripped_line.startswith("if")
or stripped_line.startswith("for")
)
):
in_code_block = True
lines.append("")
lines.append(".. code-block:: go")
lines.append("")
# Add the line to the formatted example
if in_code_block:
# For code blocks, add indentation
lines.append(f" {line}")
elif stripped_line: # Only add non-empty lines outside code blocks
lines.append(line)
lines.append("")
def setup(app):
"""
Setup function for Sphinx extension.
This will be called by Sphinx when the extension is loaded.
"""
# Register the directive
app.add_directive("go-api-ref", GoApiRefDirective)
return {
"version": "0.1.0",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
|