-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcip-108-create-human-readable.sh
More file actions
executable file
·116 lines (88 loc) · 2.8 KB
/
cip-108-create-human-readable.sh
File metadata and controls
executable file
·116 lines (88 loc) · 2.8 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BRIGHTWHITE='\033[0;37;1m'
NC='\033[0m'
UNDERLINE='\033[4m'
BOLD='\033[1m'
GRAY='\033[0;90m'
# Usage message
usage() {
local col=50
echo -e "${UNDERLINE}${BOLD}Create a human-readable Markdown representation of JSON-LD metadata${NC}"
echo -e "\n"
echo -e "Syntax:${BOLD} $0 ${GREEN}<file|directory>${NC}"
printf "Params: ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "<file|directory>" "- Path to JSON-LD file or directory"
printf " ${GREEN}%-*s${GRAY}%s${NC}\n" $((col-8)) "-h, --help" "- Show this help message and exit"
exit 1
}
# Function to extract fields from the JSON-LD file
extract_jsonld_data() {
local jsonld_file=$1
# Extract the data fields using jq
local title=$(jq -r '.body.title // empty' "$jsonld_file")
local abstract=$(jq -r '.body.abstract // empty' "$jsonld_file")
local motivation=$(jq -r '.body.motivation // empty' "$jsonld_file")
local rationale=$(jq -r '.body.rationale // empty' "$jsonld_file")
local authors=$(jq '.authors[] // empty' "$jsonld_file")
local onchain=$(jq '.body.onChain // empty' "$jsonld_file")
# Extract the references and format them
local references=$(jq -r '.body.references[] | "- [\(.label)](\(.uri))" // empty' "$jsonld_file")
# Output to a markdown file
local output_file="${jsonld_file}.md"
# Create markdown content
cat > "$output_file" <<EOF
# Markdown Representation of $(basename "$jsonld_file")
## Title
$title
## Abstract
$abstract
## Motivation
$motivation
## Rationale
$rationale
## References
$references
## Authors
$authors
## Onchain
$onchain
EOF
echo "Markdown file generated: $output_file"
}
# Check if a file or directory is passed as an argument
if [ $# -eq 0 ]; then
usage
fi
# Handle help flag
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
fi
# If the argument is a directory, process each JSON-LD file (including subdirectories)
if [ -d "$1" ]; then
# Get all .jsonld files in the directory and subdirectories
jsonld_files=()
while IFS= read -r -d '' file; do
jsonld_files+=("$file")
done < <(find "$1" -type f -name "*.jsonld" -print0)
# Check if any .jsonld files were found
if [ ${#jsonld_files[@]} -eq 0 ]; then
echo "Error: No .jsonld files found in directory (including subdirectories): $1"
exit 1
fi
echo "Found ${#jsonld_files[@]} .jsonld files to process"
# Process each .jsonld file
for jsonld_file in "${jsonld_files[@]}"; do
extract_jsonld_data "$jsonld_file"
done
elif [ -f "$1" ]; then
# If it's a single file, process it
extract_jsonld_data "$1"
else
echo "Invalid input. Please provide a valid file or directory."
exit 1
fi