issues2md-build-index.sh 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #! /usr/bin/env bash
  2. set -e
  3. set -u
  4. #set -x
  5. md_dir="$1"
  6. out_path="$2"
  7. # generate index file
  8. # workaround for https://github.com/mattduck/gh2md/issues/33
  9. (
  10. #echo "# github issues"
  11. #echo
  12. # loop sections
  13. # TODO add more sections
  14. # TODO? generate multiple index files, one file per section
  15. # FIXME skip empty sections
  16. for selector in "open issue" "closed issue" "merged pr"; do
  17. section="${selector}s"
  18. selected_status=${selector% *}
  19. selected_type=${selector#* }
  20. echo "<h2>$section</h2>"
  21. echo '<details>'
  22. echo "<summary>$section</summary>"
  23. echo '<table>'
  24. find "$md_dir" -type f -name "*.$selected_type.$selected_status.md" | sort -n --reverse | while read md_path; do
  25. # 2021-06-16.1.pr.merged.md 2023-08-07.2.issue.open.md 2023-08-07.3.issue.open.md 2023-08-08.4.issue.open.md
  26. md_name=$(basename "$md_path")
  27. md_url="md/$md_name"
  28. number=$(echo "$md_name" | cut -d. -f 2)
  29. type=$(echo "$md_name" | cut -d. -f 3) # "pr" or "issue"
  30. status=$(echo "$md_name" | cut -d. -f 4) # pr: merged/?/?, issue: open/?
  31. num_comments=$(cat "$md_path" | grep -E '^#### <img src="https://avatars\.githubusercontent\.com/u/[0-9]+\?(u=[0-9a-f]+&)?v=[0-9]+" width="50">\[.*?\]\(https://github.com/.*?\) commented at \[.*\):$' | wc -l)
  32. text=$(pandoc -f commonmark -t plain --wrap=none "$md_path")
  33. title=$(echo "$text" | head -n1 | cut -d' ' -f4-)
  34. author=$(echo "$text" | head -n3 | tail -n1 | cut -d' ' -f1)
  35. datetime=$(echo "$text" | head -n3 | tail -n1 | cut -d' ' -f5-6 | sed 's/:$//')
  36. # FIXME: use only the first comment
  37. start=$(echo "$text" | tail -n +5 | head -c 200)
  38. start_escaped=$(echo "$start" | sed 's/"/\&quot;/g' | perl -0777 -pe 's/\n/&#10;/g')
  39. # debug
  40. echo "start: $start" >&2
  41. echo "start_escaped: $start_escaped" >&2
  42. #echo '<tr>'
  43. if false; then
  44. # column: icon
  45. if [[ "$type" == "issue" ]]; then
  46. if [[ "$status" == "open" ]]; then
  47. echo '<td><svg class="octicon octicon-issue-opened open" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z"></path><path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Z"></path></svg></td>'
  48. else
  49. echo '<td>TODO icon</td>'
  50. fi
  51. else
  52. echo '<td>TODO icon</td>'
  53. fi
  54. else
  55. # column: type
  56. #echo "<td>$type</td>"
  57. # column: status
  58. #echo "<td>$status</td>"
  59. # column: status + type
  60. #echo "<td>$status $type</td>"
  61. :
  62. fi
  63. # column: title, number, date, author
  64. #echo "<td><b><a href=\"$md_url\">$title</a></b><br>#$number opened on $datetime by $author</td>"
  65. comments_str=$(if ((num_comments > 0)); then echo " &#x1f4ac; $num_comments"; else echo ""; fi)
  66. echo "<tr><td><b><a href=\"$md_url\" title=\"$start_escaped\">$title</a></b><br>#$number opened on $datetime by $author$comments_str</td></tr>"
  67. # column: number of replies
  68. # https://www.fileformat.info/info/unicode/char/1f4ac/index.htm
  69. if false; then
  70. if ((num_comments > 0)); then
  71. echo "<td>&#x1f4ac; $num_comments</td>"
  72. else
  73. echo "<td></td>"
  74. fi
  75. fi
  76. #echo '</tr>'
  77. done
  78. echo '</table>'
  79. echo '</details>'
  80. done
  81. ) >"$out_path"