#!/bin/bash getDocumentMetadataHeader() { local file="$1" sed -e '/^[@]document[.]meta/,/^[@]end$/!d' "$file" | \ grep -ve "^[@]\(document[.]meta\|end\)" | \ grep -oe "\S.*$" } getDocumentMetadataValue() { local file="$1" local key="$2" #Example header: # getDocumentMetadataHeader "$file" | grep -oP "(?<=${key}: ).*" } splitValuesByComma() { local str="$1" # Change comma (,) to whitespace and add under braces local array=($(echo "${str}" | tr ',' ' ')) for value in "${array[@]}"; do echo "$value" done } NorgGetFilename() { local file="$1" getDocumentMetadataValue "$1" 'title' } NorgGetTags() { local file="$1" # getDocumentMetadataValue "$1" 'categories'| tr ',' ' ' local value="$(getDocumentMetadataValue "$1" 'categories'| tr ',' ' ')" splitValuesByComma "${value}" } NorgGetAuthors() { local file="$1" # getDocumentMetadataValue "$1" 'authors'| tr ',' ' ' local value="$(getDocumentMetadataValue "$1" 'authors')" splitValuesByComma "${value}" } NorgGetDescription() { local file="$1" getDocumentMetadataValue "$1" 'description' } NorgGetCreated() { local file="$1" #date --date="$(getDocumentMetadataValue "$1" 'created')" +"%s" # date -j -f "%Y-%m-%d" "$(getDocumentMetadataValue "$1" 'created')" "+%s" date -d "$(getDocumentMetadataValue "$1" 'created')" "+%s" } NorgGetChanged() { local file="$1" date -r "${file}" "+%s" } NorgGetVersion() { local file="$1" getDocumentMetadataValue "$1" 'version' } #description: #authors: nige #categories: bash, ssh, script #created: 2021-12-29 #version: 0.0.9 NorgGetDocumentBody() { local file="$1" local lines=$(( $(getDocumentMetadataHeader "$file" | wc -l) + 3 )) tail -n +$lines "$file" } #title: 202112291700-sshconnect_script.md #description: #authors: nige #categories: bash, ssh, script #created: 2021-12-29 #version: 0.0.9 #echo "TEST:" #echo #echo "Header:" #echo " Filename: $(NorgGetFilename $1)" #echo " Description: $(NorgGetDescription $1)" #echo " Author: $(NorgGetAuthors $1)" #echo " Tags: $(NorgGetTags $1)" #echo " Create: $(NorgGetCreated $1)" #echo " Changed: $(NorgGetChanged $1)" #echo " Version: $(NorgGetVersion $1)" #echo "Body: $(NorgGetDocumentBody $1)" # filenameID="$(basename "$1" | xxd -p -u")" filenameID="$(NorgGetFilename "$1" | sed "s/[^0-9a-zA-Z_-]/_/g")" filename="$(basename "$1")" type="${filename##*.}" case ${type^^} in "SH" ) filetype="bash" ;; "MD" | "MARKDOWN" ) filetype="markdown" ;; "norg" ) filetype="neorg" ;; * ) filetype="${type,,}" ;; esac jq -n \ --arg id "${filenameID}" \ --arg filename "${filename}" \ --arg filetype "${filetype,,}" \ --arg description "$(NorgGetDescription "$1" )" \ --arg created "$(NorgGetCreated "$1")" \ --arg changed "$(NorgGetChanged "$1")" \ --arg version "$(NorgGetVersion "$1")" \ --arg authors "$(NorgGetAuthors "$1")" \ --arg tags "$(NorgGetTags "$1")" \ --arg headerlines "$(( $(getDocumentMetadataHeader "$1" | wc -l) + 2 ))" \ --arg refcount "$2" \ --arg body "$(NorgGetDocumentBody "$1")" \ '{ "id": $id, "image": "https://raw.githubusercontent.com/nvim-neorg/neorg/main/res/neorg.svg", "title": $filename, "type": $filetype, "description": $description, "created": $created, "changed": $changed, "version": $version, "authors": [], "tags": [], "headerlines": $headerlines, "refcount": $refcount, "body": $body }' > /tmp/tmpFile while read -re value; do jq --arg value "${value}" '.authors += [ $value ]' /tmp/tmpFile > /tmp/tmpFile2 mv /tmp/tmpFile2 /tmp/tmpFile done <<< $(NorgGetAuthors $1) while read -e value; do jq --arg value "${value}" '.tags += [ $value ]' /tmp/tmpFile > /tmp/tmpFile2 mv /tmp/tmpFile2 /tmp/tmpFile done <<< $(NorgGetTags $1) jq --argjson var "$(< /tmp/tmpFile)" '. |= . + [$var]' <<< "[]" #jq < /tmp/tmpFile rm /tmp/tmpFile /tmp/tmpFile2 &>/dev/null