This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
visualize
executable file
·223 lines (194 loc) · 5.53 KB
/
visualize
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env sh
# SPDX-FileCopyrightText: 2020 - 2023 Robin Vobruba <[email protected]>
# SPDX-License-Identifier: Unlicense
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
#set -Eeuo pipefail
set -eu
script_path="$(readlink -f "$0")"
script_dir="$(dirname "$script_path")"
script_name="$(basename "$script_path")"
root_dir="$script_dir"
build_dir="$root_dir/build"
publish_dir="$root_dir/public"
tsdc_tools_dl_url="https://osegermany.gitlab.io/oh-tsdc-tools/tsdc-tools-latest-jar-with-dependencies.jar"
tsdc_tools="$build_dir/tsdc-tools.jar"
vrdf_dl_url="https://gitlab.com/OSEGermany/rdf-visualizer/raw/master/visualize-rdf"
vrdf="$build_dir/visualize-rdf"
force=false
_install_requirements() {
mkdir -p "$build_dir"
# Installs wget, JDK, Python
if ! ( which wget > /dev/null && which pip > /dev/null && which javac > /dev/null )
then
apt-get update
apt-get install -y -qq wget python3-pip default-jdk > /dev/null
fi
# Installs `visualize-rdf`
mkdir -p "$(dirname "$vrdf")"
wget "$vrdf_dl_url" \
--output-document="$vrdf"
chmod +x "$vrdf"
"$vrdf" --install-requirements
# Installs TsDC tools
mkdir -p "$(dirname "$tsdc_tools")"
wget "$tsdc_tools_dl_url" \
--output-document="$tsdc_tools"
}
_print_help() {
echo "Create differently layouted visual (graph-style) representations"
echo "of all the RDF files from CWD, recursively."
echo "This generates multiple SVG, PNG and a single DOT file"
echo "for each TTL file."
echo "It also creates a structures Markdown file with the RDFs content."
echo "In the end, it also creates an index.html file"
echo "that links to all the generated files."
echo
echo "Usage:"
echo " $script_name [OPTIONS]"
echo "Options:"
echo " -h, --help"
echo " Show this help message and exit"
echo " -s, --install-requirements"
echo " Installs all the requirements for this script to run successfully and exit"
echo " -f, --force"
echo " Overwrite index.html if it already exists."
echo " NOTE: All the other generated files are overwritten"
echo " even without this flag."
echo "Examples:"
echo " $script_name"
echo " $script_name --help"
}
# read command-line args
i=1
while [ "$i" -lt "$#" ]
do
arg="$(eval "echo \$$i")"
case "$arg" in
-h|--help)
shift "$i"
_print_help
exit 0
;;
-s|--install-requirements)
shift "$i"
_install_requirements
exit 0
;;
-f|--force)
shift "$i"
force=true
;;
*) # non-/unknown option
i=$((i + 1))
;;
esac
done
if [ ! -f "$vrdf" ]
then
_install_requirements
fi
_clean_dot_file() {
dot_file="$1"
layout_engine=fdp
dot_file_clean="${dot_file%.*}-clean.dot"
echo "Cleaning '$dot_file' -> '$dot_file_clean' ..."
{
grep -v 'wikidata' \
| grep -v ':base' \
| grep -v 'ObjectProperty' \
| grep -v 'DatatypeProperty' \
| grep -v 'Class' \
| grep -v 'Ontology'
} < "$dot_file" > "$dot_file_clean"
svg_file="${dot_file_clean%.dot}.svg"
png_file="${dot_file_clean%.dot}.png"
echo "Generating SVG and PNG from '$dot_file_clean' ..."
dot -Glayout="$layout_engine" -Grankdir=LR -Tsvg -o "$svg_file" "$dot_file_clean"
dot -Glayout="$layout_engine" -Grankdir=LR -Tpng -o "$png_file" "$dot_file_clean"
}
mkdir -p "$publish_dir"
echo
find . -name "*.ttl" \
| grep -v "^\./$(basename "$build_dir")/" \
| grep -v "^\./$(basename "$publish_dir")/" \
| while read -r ttl_file
do
printf "\n\nProcessing input file '%s' ...\n" "$ttl_file"
base_output_dir="$publish_dir/$(dirname "$ttl_file")"
mkdir -p "$base_output_dir"
base_name="$(basename "${ttl_file%.*}")"
cp "$ttl_file" "$base_output_dir/"
# Create graphs
for layout in fdp dot
do
base_output_file="$base_output_dir/${layout}_$base_name"
# Generates PNG, SVG & DOT files
"$vrdf" \
--do-not-open \
--layout "$layout" \
"$ttl_file" \
"$base_output_file"
# Generates a cleaned-up DOT file and SVG and PNG from it
dot_file="${base_output_file}.dot"
_clean_dot_file "$dot_file"
done
# Create structured Markdown
java \
-classpath "$tsdc_tools" \
de.opensourceecology.tsdc.tools.rdf2md.MainKt \
--input "$ttl_file" \
--output "${base_output_file}.md"
# --structure-property "http://purl.org/oseg/din/ontologies/tsdc/0.1/specification#isBelow" \
done
index_file="$publish_dir/index.html"
if ! [ -e "$index_file" ] || $force
then
{
cd "$publish_dir"
# Create HTML that links to all the files generated above
# plus the sources
echo "<html>"
echo "<head>"
echo "<title>Sources & Generated output</title>"
echo "</head>"
echo "<body>"
echo "<h1>Sources & Generated output</h1>"
echo "<h3>Turtle/RDF sources</h3>"
echo "<ul>"
for file in *.ttl
do
echo "<li><a href=\"$file\">$file</a></li>"
done
echo "</ul>"
echo "<h3>Markdown files laying out the structure</h3>"
echo "<ul>"
for file in *.md
do
echo "<li><a href=\"$file\">$file</a></li>"
done
echo "</ul>"
echo "<h3>GraphViz/DOT graps representing the structure</h3>"
echo "<ul>"
for file in *.dot
do
echo "<li><a href=\"$file\">$file</a></li>"
done
echo "</ul>"
echo "<h3>PNG & SVG renders of the above graphs</h3>"
echo "<ul>"
for file in *.svg
do
file_png="${file%.svg}.png"
echo "<li>"
echo "<a href=\"$file\">$file - <img src=\"$file\" alt=\"$file\" width=\"640\"/></a> - "
echo "<a href=\"$file_png\">$file_png - <img src=\"$file_png\" alt=\"$file\" width=\"640\"/></a>"
echo "</li>"
done
echo "</ul>"
echo "</body>"
echo "</html>"
} > "$index_file"
else
>&2 echo "WARNING: Index file not written, because it already exists, and --force is not given."
fi