-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathocrd-page-transform
executable file
·209 lines (189 loc) · 7.87 KB
/
ocrd-page-transform
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
#!/usr/bin/env bash
# shellcheck disable=SC2086
set -eu
set -o pipefail
# set -x
### arbitrary XSL transformation for PAGE-XML in OCR-D
#
# Finds and downloads all files in the input fileGrp
# of the workspace. Then for each page, finds the
# corresponding PAGE-XML file, and processes it with
# the given XSLT. The result is added to the output
# fileGrp.
which ocrd >/dev/null 2>/dev/null || { echo >&2 "ocrd not in \$PATH. Panicking"; exit 1; }
((BASH_VERSINFO<4 || BASH_VERSINFO==4 && BASH_VERSINFO[1]<4)) && echo >&2 "bash $BASH_VERSION is too old. Please install bash 4.4 or newer." && exit 1
SHAREDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
PRESERVE_NAMESPACE=1 # 1 preserves the input file's PAGE namespace prefix and URL (version)
ADD_METADATAITEM=1 # 1 adds a MetadataItem detailling the transform params used
MIMETYPE_PAGE=$(ocrd bashlib constants MIMETYPE_PAGE)
declare -A NAMESPACES
eval "NAMESPACES=( $(ocrd bashlib constants NAMESPACES) )"
function process_file {
local in_fpath="$1" in_id="$2" in_pageId="$3" xsl="$4" param="$5" out_fpath="$6" out_id="$7" out_file_grp="$8" pretty="$9"
# to become independent of whether and what
# namespace prefix is used for PAGE-XML,
# we first have to know the namespace:
namespace=$(xmlstarlet sel -t -m '/*[1]' -v 'namespace-uri()' "$in_fpath")
# now (using --no-doc-namespace) we can
# safely query with -N pc=${namespace}
# and safely add with prefix ${ns_prefix}:
ns_prefix=$(xmlstarlet sel -t -m '/*[1]' -v 'substring-before(name(),"PcGts")' "$in_fpath"; true)
function ingest {
if ((PRESERVE_NAMESPACE)); then
# preserve namespace and prefix
cat "$1"
else
# stylesheet transforms to standard namespace:
xmlstarlet tr <(cat <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="${NAMESPACES[xsl]}"
xmlns="${NAMESPACES[page]}">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|text()|comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
EOF
) "$1"
fi
}
function addmeta {
declare -a options
options+=( --no-doc-namespace ed
-N "pc=$namespace"
-u '/pc:PcGts/@pcGtsId'
-v "$out_id" )
if ((ADD_METADATAITEM)); then
# insert agent
options+=(
-s '/pc:PcGts/pc:Metadata'
-t elem -n "${ns_prefix}MetadataItem"
# bind previous element to "new-item":
--var new-item '$prev'
-s '$new-item' -t attr -n type
-v "processingStep"
-s '$new-item' -t attr -n name
-v "*"
-s '$new-item' -t attr -n value
-v "$OCRD_TOOL_NAME"
# add "Labels" for params:
-s '$new-item' -t elem -n "${ns_prefix}Labels"
# bind previous element to "new-labels":
--var new-labels '$prev'
-s '$new-labels' -t attr -n externalModel
-v ocrd-tool
-s '$new-labels' -t attr -n externalId
-v parameters
)
for key in ${!params[@]}; do
# shellcheck disable=SC2016
options+=( # add another "Label":
-s '$new-labels' -t elem -n "${ns_prefix}Label"
# bind previous element to "new-label":
--var new-label '$prev'
-s '$new-label' -t attr -n value
-v "${params[$key]}"
-s '$new-label' -t attr -n type
-v "$key" )
done
tool_version=$(ocrd ocrd-tool "$OCRD_TOOL_JSON" version | sed 's,^Version ",,;s,".*$,,')
core_version=$(ocrd --version | sed 's/ocrd, version //')
options+=(
# add "Labels" for versions:
-s '$new-item' -t elem -n "${ns_prefix}Labels"
# bind previous element to "new-labels":
--var new-labels '$prev'
# add another "Label":
-s '$new-labels' -t attr -n externalModel
-v ocrd-tool
-s '$new-labels' -t attr -n externalId
-v version
-s '$new-labels' -t elem -n "${ns_prefix}Label"
# bind previous element to "new-label":
--var new-label '$prev'
-s '$new-label' -t attr -n value
-v "$tool_version"
-s '$new-label' -t attr -n type
-v "$OCRD_TOOL_NAME"
-s '$new-labels' -t elem -n "${ns_prefix}Label"
# bind previous element to "new-label":
--var new-label '$prev'
-s '$new-label' -t attr -n value
-v "$core_version"
-s '$new-label' -t attr -n type
-v "ocrd/core"
)
fi
xmlstarlet "${options[@]}"
}
function pprint {
if ((pretty)); then
xmlstarlet fo -s $pretty
else
cat
fi
}
ingest "$in_fpath" | xmlstarlet tr "$xsl" $param | addmeta | pprint >"$out_fpath"
}
function main {
# Load ocrd bashlib functions
# shellcheck source=../core/ocrd/bashlib/lib.bash
source $(ocrd bashlib filename)
ocrd__wrap "$SHAREDIR/ocrd-tool.json" "ocrd-page-transform" "$@"
ocrd__minversion 2.58.1
local xsl="${params[xsl]}"
local xsltparam="${params[xslt-params]}"
local pretty="${params[pretty-print]}"
if test -e "$xsl"; then
xsl="$(realpath "$xsl")"
elif ocrd__list_resources | fgrep -q "/$xsl"; then
xsl="$(ocrd__list_resources | fgrep -m1 "/$xsl")"
else
ocrd__raise "cannot find xsl resource '$xsl'"
fi
cd "${ocrd__argv[working_dir]}"
local out_file_grp=${ocrd__argv[output_file_grp]}
for ((n=0; n<${#ocrd__files[*]}; n++)); do
local in_fpath="$(ocrd__input_file $n local_filename)"
local in_id="$(ocrd__input_file $n ID)"
local in_pageId="$(ocrd__input_file $n pageId)"
local in_mimetype="$(ocrd__input_file $n mimetype)"
local out_id="$(ocrd__input_file $n outputFileId)"
local out_fpath="$out_file_grp/${out_id}.xml"
local out_mimetype="${params[mimetype]}"
if ! test -f "${in_fpath#file://}"; then
ocrd log error "input file ID=${in_id} (pageId=${in_pageId} MIME=${in_mimetype}) is not on disk"
continue
fi
mkdir -p $out_file_grp
ocrd log info "processing PAGE-XML input file $in_id ($in_pageId)"
process_file "$in_fpath" "$in_id" "$in_pageId" "$xsl" "$xsltparam" "$out_fpath" "$out_id" "$out_file_grp" $pretty
# Add PAGE file to METS
declare -a add_options
if [ -n "$in_pageId" ]; then
add_options=( -g $in_pageId )
else
add_options=()
fi
if [[ "${ocrd__argv[overwrite]}" == true ]];then
add_options+=( --force )
fi
add_options+=( -G $out_file_grp
-m $out_mimetype
-i "$out_id"
"$out_fpath" )
declare -a workspace_options
if [[ -n "${ocrd__argv[mets_server_url]}" ]];then
workspace_options+=( -U "${ocrd__argv[mets_server_url]}" )
fi
ocrd workspace "${workspace_options[@]}" add "${add_options[@]}"
done
}
main "$@"