aboutsummaryrefslogtreecommitdiff
path: root/officium
blob: fae202fa4bfff516eeb1b93814c5b344e2e3738e (plain) (blame)
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
#!/bin/bash

# Scriptum ad preces e Divinum Officium extrahendas
# Auctor: Adam Kasprzak
# Data: 2026-01-13

set -o pipefail

if [ $# -eq 0 ]; then
  echo "Usus: $0 [laudes|prima|tertia|sexta|nona|vesperae]"
  exit 1
fi

HORA_INPUT="$1"
HORA="$(echo "${HORA_INPUT:0:1}" | tr '[:lower:]' '[:upper:]')$(echo "${HORA_INPUT:1}" | tr '[:upper:]' '[:lower:]')"

case "$HORA" in
  Laudes|Prima|Tertia|Sexta|Nona|Vesperae) ;;
  *)
    echo "Error: Argumentum invalidum: '$HORA_INPUT'"
    echo "Valida: laudes, prima, tertia, sexta, nona, vesperae"
    exit 1
    ;;
esac

BASE_URL="https://www.divinumofficium.com/cgi-bin/horas/officium.pl"
URL="${BASE_URL}?command=pray${HORA}"

USE_COLOR=1
if [ ! -t 1 ] || [ "${NO_COLOR:-0}" = "1" ]; then
  USE_COLOR=0
fi

WRAP="${WRAP:-80}"  # word-wrap width (no justify)

echo
echo
echo "        ?8888P"
echo "         \`88'"
echo "    8b,_  88  _,d8"
echo "    88888SEAL88888"
echo "    8P~   88   ~?8"
echo "         ,88."
echo "        d8888b"

echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
echo "Hora: $HORA"
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
echo

HTML="$(curl -s "$URL")"
if [ $? -ne 0 ] || [ -z "$HTML" ]; then
  echo "Error: Pagina recipi non potuit (curl error vel responsum vacuum)."
  exit 1
fi

echo "$HTML" | awk '
  /<FONT SIZE=.+1. COLOR="red"><B><I>Incipit<\/I><\/B><\/FONT>/ { printing=1 }
  printing {
    if (/<TD VALIGN=.TOP. WIDTH=.50%.><DIV ALIGN=.right.><FONT SIZE=.1. COLOR=.green.>[0-9]+/) in_english=1
    if (/<\/TR>/) { in_english=0; print "" }
    if (!in_english) print
  }
  /<\/TABLE>/ && printing { exit }
' | \
  sed 's/<[^>]*>//g' | \
  sed -E 's/^[0-9]+:[0-9]+[[:space:]]*//' | \
  sed 's/&nbsp;/ /g' | \
  sed 's/&ensp;/ /g' | \
  sed 's/&lt;/</g' | \
  sed 's/&gt;/>/g' | \
  sed 's/&amp;/\&/g' | \
  sed 's/&#[0-9]*;//g' | \
  # remove parentheses ranges after Psalm number: Psalmus 54(17-24) -> Psalmus 54
  sed -E 's/^(Psalmus[[:space:]]+[0-9]+)\([^)]*\)/\1/' | \
  cat -s | \
  grep -v "^Top" | \
  grep -v "^Next" | \
  grep -v "^[0-9]$" | \
  # blank lines between antiphon and psalm title; and before closing antiphon after V/. R/.
  awk '
    function is_ant(s)   { return (s ~ /^Ant\./) }
    function is_psalm(s) { return (s ~ /^Psalmus[[:space:]]+[0-9]+/) }
    function is_vr(s)    { return (s ~ /^[VR]\.\//) }

    {
      cur = $0
      if (is_psalm(cur) && is_ant(prev) && prev != "") {
        print prev
        print ""
        print cur
        prev=""
        next
      }
      if (is_ant(cur) && is_vr(prev) && prev != "") {
        print prev
        print ""
        print cur
        prev=""
        next
      }
      if (prev != "") print prev
      prev = cur
    }
    END { if (prev != "") print prev }
  ' | \
  # word-wrap (no justify), preserve indentation
  awk -v wrap="$WRAP" '
    function ltrim(s){ sub(/^[[:space:]]+/, "", s); return s }
    function rtrim(s){ sub(/[[:space:]]+$/, "", s); return s }
    function trim(s){ return rtrim(ltrim(s)) }

    function wrap_line(indent, text, limit,   n,i,words,line,tmp){
      text = trim(text)
      if (text == "") { print ""; return }
      gsub(/[[:space:]]+/, " ", text)

      n = split(text, words, " ")
      line = ""
      for (i=1; i<=n; i++){
        tmp = (line == "" ? words[i] : line " " words[i])
        if (length(tmp) <= limit) {
          line = tmp
        } else {
          print indent line
          line = words[i]
        }
      }
      if (line != "") print indent line
    }

    {
      raw = $0
      if (raw ~ /^[[:space:]]*$/) { print ""; next }

      # keep existing indentation
      indent = ""
      match(raw, /^[[:space:]]*/)
      indent = substr(raw, RSTART, RLENGTH)
      text = substr(raw, RLENGTH+1)

      # wrap only if needed
      if (length(raw) > wrap) {
        lim = wrap - length(indent)
        if (lim < 20) lim = 20
        wrap_line(indent, text, lim)
      } else {
        print raw
      }
    }
  ' | \
  # pretty print (colors + indentation unchanged; grey for [] and {})
  awk -v use_color="$USE_COLOR" '
    BEGIN{
      reset="\033[0m"; bold="\033[1m"; dim="\033[2m"
      red="\033[31m"; green="\033[32m"; yellow="\033[33m"
      blue="\033[34m"; mag="\033[35m"; cyan="\033[36m"
      gray="\033[90m"
      if (!use_color) { reset=bold=dim=red=green=yellow=blue=mag=cyan=gray="" }
    }

    function trim(s){ gsub(/^[[:space:]]+|[[:space:]]+$/, "", s); return s }

    function gray_blocks(s, cur, rep){
      rep = gray "&" cur
      gsub(/\[[^]]+\]/, rep, s)
      gsub(/\{[^}]*\}/, rep, s)
      return s
    }

    function blank_before_if_needed(t){
      if (t ~ /^(Oremus\.?|Conclusio)$/ && prev_blank == 0) print ""
    }

    {
      raw = $0

      if (raw ~ /^[[:space:]]*$/) { print ""; prev_blank=1; next }

      t = trim(raw)
      blank_before_if_needed(t)
      prev_blank=0

      prefix=""

      # headings in RED
      if (t == "Incipit" || t == "Hymnus") {
        prefix = bold red
      } else if (t ~ /^Psalmus[[:space:]]+[0-9]+/) {
        prefix = bold red
      } else if (t ~ /^Psalmi[[:space:]]*\{/) {
        prefix = dim gray
      } else if (t == "Conclusio") {
        prefix = bold red
      } else if (t ~ /^Ant\./) {
        prefix = mag
      } else if (t ~ /^[VR]\.\//) {
        prefix = green
      } else if (t ~ /^Gl(รณ|o)ria Patri,/) {
        prefix = blue
      } else if (t ~ /^Sicut erat/) {
        prefix = blue
      }

      line = gray_blocks(raw, prefix)
      print prefix line reset
    }
  '

echo
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
echo "Finis Horae: $HORA"
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"