fant0men

Comment fix script

Mar 5th, 2020
145
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script is meant to reformat comments in all my scripts, so they'll have no space or tabs in front of them.
  3. # The script will also make sure the comments aren't longer than 73 characters, which is the page width in the Geany text editor.
  4.  
  5. tmp_bn='/dev/shm/comment_fix'
  6. tmp_part="${tmp_bn}_part-${RANDOM}.txt"
  7. tmp_whole="${tmp_bn}_whole-${RANDOM}.txt"
  8.  
  9. limit='73'
  10. half_limit='40'
  11.  
  12. regex_comment='^[[:blank:]]{0,}#'
  13. regex_punct='[[:punct:]]$'
  14. regex_alphanumeric='^[0-9]|[A-Z]'
  15.  
  16. switch='0'
  17. start='0'
  18. end='0'
  19. continue='0'
  20. last_line='0'
  21.  
  22. declare -a comment_range_array lines range range2 output
  23. declare -A comment_range_hash
  24.  
  25. read_comments () {
  26.  
  27.     n='0'
  28.  
  29.     mapfile -t lines < <(cat "$1")
  30.     last_line=$(( ${#lines[@]} - 1 ))
  31.  
  32.     for (( i=0; i<${#lines[@]}; i++ )); do
  33.         line="${lines[${i}]}"
  34.  
  35.         if [[ $switch -eq 0 && $line =~ $regex_comment ]]; then
  36.             start="$i"
  37.             switch='1'
  38.         fi
  39.  
  40.         if [[ $switch -eq 1 ]]; then
  41.  
  42.             if [[ ! $line =~ $regex_comment ]]; then
  43.  
  44.                 end=$(( i - 1 ))
  45.  
  46.                 comment_range_hash[${start}]="$end"
  47.                 comment_range_array[${n}]="$start"
  48.  
  49. echo "${i}:${start} ${end}"
  50.                 n=$(( n + 1 ))
  51.  
  52.                 start='0'
  53.                 end='0'
  54.                 switch='0'
  55.  
  56.             elif [[ $i -eq $last_line ]]; then
  57.                 end="$i"
  58. echo "${i}:${start} ${end}"
  59.                 comment_range_hash[${start}]="$end"
  60.                 comment_range_array[${n}]="$start"
  61.  
  62.                 start='0'
  63.                 end='0'
  64.                 switch='0'             
  65.             fi
  66.         fi
  67.     done
  68.  
  69.     n=$(( ${#comment_range_array[@]} - 1 ))
  70.     last_range_start="${comment_range_array[${n}]}"
  71.     last_range_end="${comment_range_hash[${last_range_start}]}"
  72.  
  73.     echo "start: ${last_range_start}"
  74.     echo "end: ${last_range_end}"
  75. #exit
  76. }
  77.  
  78. write_tmp () {
  79.  
  80.     if [[ ! -f $tmp_part ]]; then
  81.         touch "$tmp_part"
  82.     fi
  83.  
  84.     for (( k=${continue}; k<${start}; k++ )); do
  85.  
  86.         echo "${lines[${k}]}" >> "$tmp_part"
  87.     done
  88. # Code below needs fixing...
  89.     for (( k=0; k<${#range[@]}; k++ )); do
  90.         length=$(wc -m <<<"${range[${k}]}" | tr -d '[:blank:]')
  91.  
  92.         if [[ $length -gt $limit ]]; then
  93.             line_tmp_length='1'
  94.             line_tmp='#'
  95.  
  96.             mapfile -d' ' -t line_parts <<<"${range[${k}]}"
  97.             last_part=$(( ${#line_parts[@]} - 1 ))
  98.             line_parts[${last_part}]=$(tr -d '\n' <<<"${line_parts[${last_part}]}")
  99.  
  100.             for (( l=0; l<${#line_parts[@]}; l++ )); do
  101.                 line_part="${line_parts[${l}]}"
  102.                 line_tmp_length=$(wc -m <<<"$line_tmp" | tr -d '[:blank:]')
  103.                 line_part_length=$(wc -m <<<"$line_part" | tr -d '[:blank:]')
  104.                 line_total_length=$(( (line_tmp_length + line_part_length) + 1 ))
  105.  
  106.                 if [[ $line_total_length -le $limit || $line_part_length -ge $limit ]]; then
  107.                     line_tmp="${line_tmp} ${line_part}"
  108.  
  109.                     if [[ $l -eq $last_part ]]; then
  110.                         echo "$line_tmp" >> "$tmp_part"
  111.                     fi
  112.                 else
  113.                     echo "$line_tmp" >> "$tmp_part"
  114.                     line_tmp="# ${line_part}"
  115.                     line_tmp_length=$(wc -m <<<"$line_tmp" | tr -d '[:blank:]')
  116.                 fi
  117.             done
  118.         else
  119.             echo "# ${line}" >> "$tmp_part"
  120.         fi
  121.     done
  122.  
  123.         #for (( i=0; i<${#range[@]}; i++ )); do
  124.         #   echo "ohi ${range[${i}]}"
  125.         #done
  126.     unset -v range
  127.  
  128. }
  129.  
  130. process_comments () {
  131.  
  132.     switch='1'
  133.     range_switch='1'
  134.     skip='0'
  135.  
  136.     for (( i=0; i<${#comment_range_array[@]}; i++ )); do
  137.  
  138.         start="${comment_range_array[${i}]}"
  139.         end="${comment_range_hash[${start}]}"
  140.  
  141.         write_tmp
  142.  
  143.         continue=$(( end + 1 ))
  144.  
  145.         for (( j=${start}; j<=${end}; j++ )); do
  146.             line=$(sed -E -e 's/^[[:blank:]]*#*[[:blank:]]*(.*)$/\1/' -e 's/[[:blank:]]*$//' -e 's/ {2,}/ /g' <<<"${lines[${j}]}")
  147.  
  148.             range+=("$line")
  149.         done
  150.  
  151.         while [[ $range_switch -eq 1 ]]; do
  152.             for (( j=0; j<${#range[@]}; j++ )); do
  153.                 if [[ $skip -eq 1 ]]; then
  154.                     skip='0'
  155.                     continue
  156.                 fi
  157.  
  158.                 line="${range[${j}]}"
  159.  
  160.                 length=$(wc -m <<<"$line" | tr -d '[:blank:]')
  161.  
  162.                 next=$(( j + 1 ))
  163.                 next_line="${range[${next}]}"
  164.                 next_length=$(wc -m <<<"$next_line" | tr -d '[:blank:]')
  165.  
  166.                 if [[ $line && $next_line ]]; then
  167.                     range2+=("${line} ${next_line}")
  168. #echo hello
  169.                     skip='1'
  170.                 else
  171.                     range2+=("$line")
  172.                 fi
  173.             done
  174.  
  175. #exit
  176.             unset -v range
  177.  
  178.             for (( j=0; j<${#range2[@]}; j++ )); do
  179.                 line="${range2[${j}]}"
  180.                 range+=("$line")
  181. #echo "$line"
  182.             done
  183. #exit
  184.             unset -v range2
  185.  
  186.             if [[ $last_range -ne ${#range[@]} ]]; then
  187.                 range_switch='1'
  188.             else
  189.                 range_switch='0'
  190.             fi
  191.             last_range="${#range[@]}"
  192.         done
  193.  
  194.         range_switch='1'
  195.  
  196.     done
  197.  
  198. #for (( j=0; j<${#range[@]}; j++ )); do
  199. #   echo "${j}: ${line}"
  200. #done
  201. #exit
  202. }
  203.  
  204.  
  205.  
  206. read_comments "$1"
  207.  
  208. #exit
  209.  
  210. process_comments
RAW Paste Data