aboutsummaryrefslogtreecommitdiff
path: root/components/comments.sh
blob: 5e0caca992f012a35774a694d885b4bf3653d2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash

parse-all-posts () {
  CONTENT=""

  if [ "$(ls "$COMMENTS_FOLDER" | wc -l)" = "0" ]; then
    echo ""
    return 1
  fi

  for post in "$COMMENTS_FOLDER/"*; do
    POST_FILE="$post/post"
    POST_ID="$(basename "$post")"

    # If is a private post, ignore
    if [ -n "$(head -n 1 "$POST_FILE")" ]; then
      continue
    fi

    POST_NAME="$(sed -n '2p' "$POST_FILE")"
    POST_EMAIL="$(sed -n '3p' "$POST_FILE")"
    POST_MESSAGE="$(tail -n +5 "$POST_FILE")"
    
    IFS=';' read -ra POST_FILES <<< "$(sed -n "4p" "$POST_FILE")"

    CONTENT="$CONTENT$(cat <<POST
<article class="post" id="post-id-$POST_ID">
  <section class="post-metadata">
    <a class="post-id" href="$REQUEST_URI#post-id-$POST_ID"># $POST_ID</a>
    <p class="post-name">$POST_NAME</p>
    <a class="post-email" href="mailto:$POST_EMAIL">$POST_EMAIL</a>
  </section>
  <section class="post-message">
    <pre>$POST_MESSAGE</pre>
  </section>
  <section class="post-attachment">
    $(
      for attach in "${POST_FILES[@]}"; do
        FMIME=$(file --mime-type -b "$SCRIPT_DIR$attach")
        if [ "${FMIME/\/*/}" = "image" ]; then
          echo "<img src="$attach" alt="attachment" /><br/>"
        fi
        echo "<a href=\"$attach\">Download attachment</a>"
      done
    )
  </section>
</article>
POST
)"
  done

  echo "$CONTENT"
}

PARSED_COMMENTS="$(parse-all-posts)"