Skip to content
Snippets Groups Projects
links2org 3.31 KiB
Newer Older
OSiUX's avatar
OSiUX committed
#!/bin/bash

# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2019 Osiris Alejandro Gomez <osiux@osiux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# shellcheck disable=SC2129
# shellcheck disable=SC2002
# shellcheck disable=SC1117

START="$(date +%s)"

OSiUX's avatar
OSiUX committed
RED="$(tput setaf 196)"

TMP0=$(mktemp)
TMP1=$(mktemp)
TMP2=$(mktemp)

TXT='links.txt'
OSiUX's avatar
OSiUX committed
ORG='links.org'
[[ -n "$HDR"                  ]] || HDR="$HOME/blog/header-links.org"
[[ -e "$PWD/header-links.org" ]] && HDR="$PWD/header-links.org"
OSiUX's avatar
OSiUX committed

BIN_BASH="$(command -v bash)"

die()
{
  printf "%s\n" "${RED}[ERROR] $1 $NORMAL" && exit 1
}

stderror ()
{
  echo >&2 "$1"
}

OSiUX's avatar
OSiUX committed
get_title()
{
  $BIN_BASH url2title "$U" \
OSiUX's avatar
OSiUX committed
  | tr -d '[]'               \
  | sed 's/GitHub - .*: //g'
}

[[ -z "$1" ]] || TXT="$1"
[[ -z "$2" ]] || ORG="$2"
[[ -z "$3" ]] || HDR="$3"
OSiUX's avatar
OSiUX committed

for FILE in $TXT $HRD
do
  [[ ! -e "$FILE" ]] && die "NOT FOUND $FILE"
  [[ ! -s "$FILE" ]] && die "EMPTY $FILE"
done

BAK="$(basename "$TXT" .txt).bak"

OSiUX's avatar
OSiUX committed
sed -i 's/+$//g' "$TXT"
sed -i 's/ + +/ +/g' "$TXT"
OSiUX's avatar
OSiUX committed
sed -i 's/blob\/master\/README.md//gI' "$TXT"
sed -i 's/blob\/dev\/README.md//gI' "$TXT"

sort -u "$TXT" | sponge "$TXT"

awk '{print $2}' "$TXT" | sort -u > "$TMP0"

OSiUX's avatar
OSiUX committed
grep -f "$TMP0" "$TXT" | sort -u | sort -k3 | while read -r D U C
do
  CATEGORY=$(echo "$C" | tr ' ' '\n' | head -1)

  [[ -z "$CATEGORY" ]] && CATEGORY="+unknown"

  printf "%s %s\\n" "$CATEGORY" "$U"
OSiUX's avatar
OSiUX committed

  echo "$D $U $CATEGORY" >> "$BAK"
OSiUX's avatar
OSiUX committed
done > "$TMP1"

OSiUX's avatar
OSiUX committed
awk '{print $1}' "$TMP1" | sort -u > "$TMP2"

[[ -e "$ORG" ]] && true         >  "$ORG"
[[ -e "$HDR" ]] && cat "$HDR"   >> "$ORG"

printf "\\n** Categories\\n\\n"    >> "$ORG"

cat "$TMP2"       \
  | tr -d '+'     \
  | grep -Ev '^$' \
  | while read -r C
do
  printf "[[file:links.org::#+%s][=+%s=]] \\n" "$C" "$C"
done >> "$ORG"

OSiUX's avatar
OSiUX committed
printf "\\n** Recents links\\n" >> "$ORG"

tail -30 "$TXT" | sort -nr | while read -r DATE U C
do

  TITLE="$(get_title "$U")"

  if [[ -z "$TITLE" ]]
  then
    printf "*** %s [[file:links.org::#%s][=%s=]]\\n" "$U" "$C" "$C"
OSiUX's avatar
OSiUX committed
  else
    printf "*** [[%s][%s]] [[file:links.org::#%s][=%s=]]\\n" "$U" "$TITLE" "$C" "$C"
OSiUX's avatar
OSiUX committed
  fi

done >> "$ORG"

printf "\\n" >> "$ORG"

cat "$TMP2" | while read -r C
do
  printf "** =%s=\\n" "$C"
  printf "   :PROPERTIES:\\n"
  printf "   :CUSTOM_ID: %s\\n" "$C"
  printf "   :END:\\n"

  REGEXP="^\+${C//+/}"
  grep -E "$REGEXP" "$TMP1" | sort -u | while read -r _ U
OSiUX's avatar
OSiUX committed
  do

    [[ -z "$U" ]] && continue
    stderror "$U"
OSiUX's avatar
OSiUX committed
    TITLE="$(get_title "$U")"

    if [[ -z "$TITLE" ]]
    then
      printf "*** %s\\n" "$U"
OSiUX's avatar
OSiUX committed
    else
      printf "*** [[%s][%s]]\\n" "$U" "$TITLE"
OSiUX's avatar
OSiUX committed
    fi

  done

done >> "$ORG"

rm -f "$TMP0"
rm -f "$TMP1"
rm -f "$TMP2"

END="$(date +%s)"

SECONDS="$((END-START))"

stderror "total time $SECONDS seconds"