-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcip-108-markdown.sh
More file actions
executable file
·106 lines (78 loc) · 2.7 KB
/
Copy pathcip-108-markdown.sh
File metadata and controls
executable file
·106 lines (78 loc) · 2.7 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
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/messages.sh
source "$SCRIPT_DIR/lib/messages.sh"
# Usage message
usage() {
printf '%s%sCreate a human-readable Markdown representation of JSON-LD metadata%s\n\n' "$UNDERLINE" "$BOLD" "$NC"
printf 'Syntax:%s %s %s<file|directory>%s\n' "$BOLD" "$0" "$GREEN" "$NC"
print_usage_option "<file|directory>" "Path to JSON-LD file or directory"
print_usage_option "-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
print_pass "Markdown file generated at $(fmt_path "$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
print_fail "No .jsonld files found in directory (including subdirectories): $(fmt_path "$1")"
exit 1
fi
print_info "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
print_fail "Invalid input. $(fmt_path "$1") is not a valid file or directory."
exit 1
fi