2

I have the following function but it is repetitive and not scalable if I have 5, 6, etc.

append_footnote(){
  file=$1
  if [ "$#" -ge 2 ];then 
    depth=$2
    if [ $depth -eq 1 ];then
      echo '<span style="float: footnote;"><a href="../index.html#toc">Go to TOC</a></span>' >> "$file"
    elif [ $depth -eq 2 ];then
      echo '<span style="float: footnote;"><a href="../../index.html#toc">Go to TOC</a></span>' >> "$file"
    elif [ $depth -eq 3 ];then
      echo '<span style="float: footnote;"><a href="../../../index.html#toc">Go to TOC</a></span>' >> "$file"
    elif [ $depth -eq 4 ];then
      echo '<span style="float: footnote;"><a href="../../../../index.html#toc">Go to TOC</a></span>' >> "$file"
    fi
  else
    echo '<span style="float: footnote;"><a href="./index.html#toc">Go to TOC</a></span>' >> "$file"
  fi
}

It just add ../ in front of index.html#toc depending on $2 arg. How can I make it better?

1

1 Answer 1

0

I came up this one. It works but please let me know if you have better solutions.

repeat(){
  END=$2
  for i in $(eval echo "{1..$END}")
  do
    echo -n "$1"
  done
}

append_footnote(){
  file=$1
  if [ "$#" -ge 2 ];then 
    depth=$2
    path_str=$(repeat '../' $depth)
    if [ $depth -gt 1 ];then
        echo "<span style='float: footnote;'><a href=\"${path_str}index.html#toc\">Go to TOC</a></span>" >> "$file"
    fi
  else
    echo '<span style="float: footnote;"><a href="./index.html#toc">Go to TOC</a></span>' >> "$file"
  fi
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.