Last active
April 20, 2018 05:24
-
-
Save hotwatermorning/f3311d246566cd5edf96 to your computer and use it in GitHub Desktop.
doxygenでコメントに改行を付加する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# doxygenはソース中の改行を空白一文字に変換してしまう。 | |
# 日本語でドキュメントを書いている場合は | |
# これによって文章中に余計な空白が追加されることになって嬉しくない。 | |
# | |
# このinput_filterは、 | |
# //!という行形式のコメントが連続するドキュメントに対して、最後の行以外に<br>を付加する | |
# /*! */または/** */という複数行形式のドキュメント対して、 | |
# (先頭行/最後の行/空白行)以外に<br>を付加する。 | |
# また、先頭行が/*!あるいは/**だけではなくテキストが存在している場合は先頭行にも<br>を付加する | |
# | |
# いまの実装だと、複数行形式のコメントを一行で書いてある場合や、 | |
# 行の先頭位置以外からコメントが開始しているようなソースで不具合が起こる。 | |
debug_mode=0 | |
if [[ $1 = "--debug" ]]; then | |
debug_mode=1 | |
shift | |
fi | |
echo() { | |
if [[ $1 = -n ]]; then | |
shift | |
printf '%s' "$*" | |
else | |
printf '%s\n' "$*" | |
fi | |
} | |
echo_debug() { | |
if [ $debug_mode -eq 1 ] ; then | |
echo "$*" | |
fi | |
} | |
# ファイルの読み込み | |
OriginalIFS=$IFS | |
IFS= | |
lines=() | |
eof= | |
while [ -z "$eof" ] ; do | |
read line || eof=true | |
lines+=("$line") | |
done < $1 | |
# while read -r line | |
# do | |
# echo_debug "read line $line" | |
# lines+=("$line") | |
# done < $1 | |
edited_lines=() | |
num_lines=${#lines[*]} | |
my_trim() { | |
trimmed_string=`echo "$1" | awk '{gsub(/^[[:blank:]]*/,"");print}' | awk '{gsub(/[[:blank:]]*$/,"");print}'` | |
# ここのechoにダブルクオーテーション忘れないようにする。 | |
echo "$trimmed_string" | |
} | |
i=0 | |
while [ $i -lt $num_lines ] ; do | |
line=${lines[$i]} | |
# trimming leading and trailing spaces | |
trimmed_line=`my_trim "${lines[$i]}"` | |
echo_debug "trimmed line at head : $trimmed_line" | |
if [ `echo "$trimmed_line" | grep "^//!"` ] ; then | |
echo_debug "Step into inline comment" | |
in_inline_comment=0 | |
while [ $i -lt $num_lines ] ; do | |
echo_debug "index of i: $i" | |
line=${lines[$i]} | |
trimmed_line=`my_trim "$line"` | |
echo_debug "current line : $line" | |
if [ ! `echo "$trimmed_line" | grep "^//!"` ] ; then | |
# 行コメント終了 | |
# このインデックスでまた外側のループから処理開始 | |
echo_debug "Step out inline comment" | |
break | |
fi | |
if [ $in_inline_comment -eq 1 ] ; then | |
num_edited_lines=${#edited_lines[*]} | |
if [ $num_edited_lines -ne 0 ] ; then | |
last_index=`expr $i - 1` | |
if [ ! `echo "${edited_lines[$last_index]}" | grep ".*<br>$"` ] ; then | |
edited_lines[$last_index]="${edited_lines[$last_index]}<br>" | |
fi | |
fi | |
fi | |
in_inline_comment=1 | |
echo_debug "Add line in inline comment $line" | |
edited_lines+=("$line") | |
i=`expr $i + 1` | |
done | |
elif [[ `echo "$trimmed_line" | grep -e "^/\*[\*\!]"` ]] ; then | |
echo_debug "Step into multiline comment" | |
in_multiline_comment=0 | |
while [ $i -lt $num_lines ] ; do | |
line=${lines[$i]} | |
trimmed_line=`my_trim "$line"` | |
echo_debug "current line : $line" | |
echo_debug "trimmed line : $trimmed_line" | |
if [[ `echo "$trimmed_line" | grep '\*/'` ]] ; then | |
# この行はそのまま追加して、次のインデックスで外側のループから処理開始 | |
# 複数行コメントがこの行で終わっているかもしれない | |
# もしくは、この行の先頭が//!で始まっているかもしれない。 | |
edited_lines+=("$line") | |
i=`expr $i + 1` | |
echo_debug "Step out multiline comment" | |
break | |
fi | |
# "/*!"や"/**"の行でかつその行に空白以外の文字が後続している場合<br>を付加する | |
# 中間の行でかつその行に空白と連続する*以外の文字が含まれている場合<br>を付加する | |
should_add_break=0 | |
if [ $in_multiline_comment -eq 0 ] ; then | |
if [[ `echo "$trimmed_line" | awk '/^\/\*(\*|\!)[[:space:]]*[^\*]+/'` ]] ; then | |
echo_debug "text in comment header" | |
should_add_break=1 | |
fi | |
in_multiline_comment=1 | |
else | |
if [ ! `echo "$trimmed_line" | grep '/^\**$/'` ] ; then | |
echo_debug "text in middle of comment" | |
should_add_break=1 | |
fi | |
fi | |
echo_debug "Add line in multiline comment $line" | |
if [ $should_add_break -eq 1 ] ; then | |
if [ ! `echo "$line" | grep ".*<br>$"` ] ; then | |
edited_lines+=("$line<br>") | |
fi | |
else | |
edited_lines+=("$line") | |
fi | |
i=`expr $i + 1` | |
done | |
else | |
echo_debug "current line : $line" | |
edited_lines+=("$line") | |
i=`expr $i + 1` | |
fi | |
done | |
for line in "${edited_lines[@]}" ; do | |
echo "$line" | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment