在Doxygen中,如何通过示例代码而不是标签的函数名称来包含代码段 [英] In Doxygen how can include a snippet by function name from example code instead of by tag
问题描述
我知道如何通过标记示例文件的一部分来创建代码片段:
I know how to create a snippet by marking a section of an example file:
//! [myfunc example]
int i = myfunc(1,"example");
if (i = CORRECT_VALUE) printf("success");
//! [myfunc example]
,然后包含其他地方:
/snippet mytestfile.c myfunc example
在我的情况下,我的示例文件是我的测试文件,每个示例实际上已经在一个函数中,如下所示:
In my case, my example files are my test files, and each example is actually already in a function, like this:
void testMyFunc() {
int i = myfunc(1,"example");
if (i = CORRECT_VALUE) printf("success");
}
所以我想要的是能够引用片段这个:
So what I want, is to be able to refer to the snippet something like this:
/snippet mytestfile.c#testMyFunc
这样我就不必添加额外的标记。你会认为,因为Doxygen已经解析了代码,我可以参考具体的函数来包含。
and that way I wouldn't have to add extra markup. You would think that because Doxygen has parsed the code already that I could refer to specific functions for inclusion.
推荐答案
我没有发现任何官方解决方案,但我已经为我做了一个脚本。
希望这可能对你有用,这里是一个例子。
I haven't found any official solution but I have made a script that do this for me.
Hopping that this could be useful for you, here is an example.
doc/
- doxygen.conf
- generate_doc.sh
- generate_snippet.sh
src/
- api.h
- api.c
test/
- test_api.c
生成文档
我从我的项目的根目录运行,以生成带有自动代码段的文档:
cd ./doc&&& ./generate_doc.sh
在我的 ./ doc / doxygen.conf
我已经设置了这个:
In my ./doc/doxygen.conf
I have set this :
EXAMPLE_PATH = ../test/snippet/
脚本
./ doc / generate_doc.sh
Scripts
./doc/generate_doc.sh
#!/bin/bash
# ./doc/generate_doc.sh
rm ../test/snippet/* # Clean old snippet
for file in ../test/*
do
if [ -f $file ]; then
./generate_snippet.sh ../test/$file # Auto-generate snippet for this file
fi
done
doxygen doxygen.conf # Generate documentation
exit $?
./ doc / generate_snippet.sh
./doc/generate_snippet.sh
#!/bin/bash
# ./doc/generate_snippet.sh
CURRENT_FUNCTION_NAME=""
N_OPEN=0
SNIPPING=0
TARGET="snippet/""`basename "$1"`"
cd "`dirname "$1"`"
mkdir -p snippet
touch "$TARGET"
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ $SNIPPING -eq 0 ]; then
if [ "`echo "$line" | grep -E "^void\ ?\*?.*?\)"`" != "" ]; then
# Found a function
SNIPPING=1
CURRENT_FUNCTION_NAME="`expr "$line" : "^void \?\(.*\?\)(.*\?)"`"
echo "//! [$CURRENT_FUNCTION_NAME]" >> "$TARGET"
fi
fi;
countopen=${line//[^\{]/}
countclose=${line//[^\}]/}
if [[ "$line" == *"{"* ]]; then
let "N_OPEN+=${#countopen}" # Add number of open
fi
if [[ "$line" == *"}"* ]]; then
let "N_OPEN-=${#countclose}" # Add number of close
fi
echo "$line" >> "$TARGET"
if [ $N_OPEN -eq 0 ] && [ $SNIPPING -eq 1 ]; then
echo "//! [$CURRENT_FUNCTION_NAME]" >> "$TARGET"
SNIPPING=0
fi
done < "$1"
exit 0
详细信息
- 要在
./ src / api.h
中使用代码段,您只需添加以下行:
@snippet test_api.c name_of_your_function
- 我所有的测试函数返回
void
,如果不是你的情况,你必须修改这个expr$ line:^ void \?\(。* \?\)(。* \?) code>。
- To use a snippet in
./src/api.h
you've just to add this line :
@snippet test_api.c name_of_your_function
- All my test functions return
void
, if it's not your case you must adapt thisexpr "$line" : "^void \?\(.*\?\)(.*\?)"
.
Details
这篇关于在Doxygen中,如何通过示例代码而不是标签的函数名称来包含代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!