ai-first-os 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/CLAUDE.md CHANGED
@@ -43,7 +43,8 @@ commit `.system/tree.md` in that same turn, like any other file the brain edits.
43
43
  could not be written and the reason the line gives, and do not retry silently.
44
44
 
45
45
  If the read prints `gitignore-declared:`, it generated an index for the first time and added that
46
- exact path to the brain's `.gitignore`: commit `.gitignore` in that same turn, the same way. The
46
+ exact path to the brain's `.gitignore`, or retired an index it no longer generates and dropped that
47
+ exact path from it: commit `.gitignore` in that same turn, the same way. The
47
48
  index itself is never committed. If it prints `gitignore-declare-failed:`, that index could not be
48
49
  kept out of git and was not written: tell the operator, in outcome language, and do not retry silently.
49
50
 
package/core/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.1.0
@@ -8,29 +8,39 @@
8
8
  # What it writes, all of it derived and none of it edited by a person:
9
9
  # - `<ws>/decisions/index.md` — every decision of the workspace, newest `date:` first.
10
10
  # - `<ws>/learnings/index.md` — every learning of the workspace, the same way.
11
- # - `<node>/index.md` — at the root of each area node (a product, a course, a channel, an account)
12
- # that an initiative, a decision or a learning of the workspace names in `about:`, with three
13
- # sections: Initiatives (a table: title · status · horizon), Decisions, Learnings.
11
+ # - `<node>/index.md` — at the root of every area node (a product, a course, a channel, an account) of
12
+ # the workspace (spec 078, C1), whether or not anything names it. Spec 079 gives it, in this order: a
13
+ # title and a line that say what it is (C3); the marker `<!-- os:focus-start -->`; Initiatives in
14
+ # progress (a table: initiative linked to its `README.md` · status · horizon · tasks linked to its
15
+ # `backlog.md` with the open count; C1, C6, C9, C11); the marker `<!-- os:focus-end -->`; Closed
16
+ # initiatives (the same table); and then Key decisions and Other decisions (two tables), Learnings and
17
+ # Documents, each a table (C12). A node with nothing to list (no initiative, decision or learning about it,
18
+ # no document of its own) has no index, and a generated one it had is deleted (C13).
19
+ # The focus read prints only what is between the two markers (C4).
20
+ # Spec 079 (C10) takes out what spec 078 wrote for an initiative: no `<initiative>/index.md` is written
21
+ # any more, and every generated one found in the workspace is deleted, with its own `.gitignore` line.
14
22
  # The workspace itself has no `index.md` (Gate 1, P4): its folders already show its initiatives,
15
23
  # decisions and learnings.
16
24
  #
17
- # Every line is a link relative to the index. A decision or learning line is `os_doc_index_line`
18
- # (common.sh), the one place that knows the shape. Every file opens with `generated: true` in its
19
- # frontmatter and then the three-line warning of D8, in the language of the brain; the third line
20
- # names where that index comes from.
25
+ # Every link is relative to the index. A decision or learning line of `decisions/index.md` and
26
+ # `learnings/index.md` is `os_doc_index_line` (common.sh), the one place that knows that shape. Every file
27
+ # opens with `generated: true` in its frontmatter, then a `# Title` and a line that say what it is, then the
28
+ # three-line warning of D8, in the language of the brain; the third line names where that index comes from.
21
29
  #
22
30
  # Rules, each in its one place here:
23
31
  # - Content unchanged, file untouched: the comparison is against the bytes on disk, so a second read
24
32
  # keeps the `mtime`, and a hand edit of a generated index is gone on the next read.
25
33
  # - An `index.md` that exists without `generated: true` was written by a person: it is skipped,
26
34
  # never overwritten (the scan names it as a finding).
27
- # - A generated node index whose node nobody names any more is removed: it is derived, and a stale
28
- # one would list what is no longer about that node.
29
35
  # - Before a path is written its entry goes to the brain's `.gitignore`: that exact path anchored at
30
36
  # the root (`/<path>`), its pattern characters escaped — never a pattern that could reach an
31
37
  # `index.md` a person wrote — and the line `gitignore-declared: <path>` is printed. If the entry
32
38
  # cannot be written, `gitignore-declare-failed: <path>` is printed and the index is not written.
33
39
  # The session contract (`core/CLAUDE.md`) says what the session does with either.
40
+ # - The reverse, for the initiative indexes of spec 078 (spec 079, C10): a generated
41
+ # `<initiative>/index.md` is deleted, and only the exact `.gitignore` line that spec wrote for it is
42
+ # dropped, with the same `gitignore-declared: <path>` line so the session commits the `.gitignore`.
43
+ # One written by a person stays untouched and keeps its line.
34
44
  #
35
45
  # Deterministic and offline: it reads frontmatter and first titles, nothing else. Exit 0 always —
36
46
  # an index that cannot be written never stops the read that called it.
@@ -42,6 +52,7 @@ here=$(cd "$(dirname "$0")" && pwd)
42
52
 
43
53
  nl=$(printf '\nx')
44
54
  nl="${nl%x}"
55
+ tab=$(printf '\tx'); tab="${tab%x}"
45
56
 
46
57
  brain=""
47
58
  ws_slug=""
@@ -100,11 +111,21 @@ while IFS= read -r f || [ -n "$f" ]; do
100
111
  fm_read "$brain/$f"
101
112
  i_rel[$i_n]="$f"
102
113
  i_about[$i_n]="$(os_about_set "$fm_about")$nl"
103
- i_status[$i_n]="$fm_status"
104
- i_horizon[$i_n]="$fm_horizon"
114
+ i_status[$i_n]="${fm_status//[[:cntrl:]]/ }"
115
+ i_horizon[$i_n]="${fm_horizon//[[:cntrl:]]/ }"
105
116
  t=$(os_titulo "$brain/$f")
106
117
  [ -n "$t" ] || t=$(os_node_name "$f")
107
- i_title[$i_n]="$t"
118
+ i_title[$i_n]="${t//[[:cntrl:]]/ }"
119
+ # The open tasks (spec 079, C6): the unindented `- [ ] ` lines of the `backlog.md` that sits beside the
120
+ # head of an initiative with a folder of its own. Empty when there is no such backlog.
121
+ i_bl[$i_n]=""
122
+ i_blrel[$i_n]=""
123
+ idir="${f%/*}"
124
+ if [ "${f##*/}" = "$head" ] && [ "${idir%/*}" = "$ws/initiatives" ] && [ -f "$brain/$idir/backlog.md" ]; then
125
+ i_blrel[$i_n]="$idir/backlog.md"
126
+ i_bl[$i_n]=$(grep -c '^- \[ \] ' "$brain/$idir/backlog.md" 2>/dev/null) || true
127
+ [ -n "${i_bl[$i_n]}" ] || i_bl[$i_n]=0
128
+ fi
108
129
  i_n=$(( i_n + 1 ))
109
130
  done <<HEADS
110
131
  $(printf '%s\n' "$heads" | LC_ALL=C sort -u)
@@ -147,6 +168,86 @@ declarar_ignorado() {
147
168
  return 0
148
169
  }
149
170
 
171
+ # retirar_indices_de_iniciativa — spec 079, C10: the initiative indexes of spec 078 are gone. Every
172
+ # `<ws>/initiatives/<slug>/index.md` with `generated: true` is deleted; one without it was written by a
173
+ # person and stays. The `.gitignore` loses the exact line that spec wrote for each index that is deleted or
174
+ # already absent (never a line for a hand-written one, and never any other line), written back with every
175
+ # other byte as it was, and `gitignore-declared: <path>` is printed for each line dropped.
176
+ retirar_indices_de_iniciativa() {
177
+ local d rel quitar="" lineas=""
178
+ for d in "$brain/$ws"/initiatives/*/; do
179
+ # A folder that is a link, or resolves outside the brain, is never entered: nothing is read or deleted there.
180
+ [ -d "$d" ] && [ ! -L "${d%/}" ] || continue
181
+ rel="${d#"$brain"/}"
182
+ rel="${rel%/}/index.md"
183
+ os_inside_brain "$brain" "$rel" || continue
184
+ if [ -e "$brain/$rel" ] || [ -L "$brain/$rel" ]; then
185
+ [ ! -L "$brain/$rel" ] || continue
186
+ fm_read "$brain/$rel"
187
+ [ "$fm_generated" = "true" ] || continue
188
+ rm -f "$brain/$rel" 2>/dev/null || continue
189
+ fi
190
+ quitar="$quitar$(ignorar_patron "$rel")$nl"
191
+ lineas="$lineas$rel$nl"
192
+ done
193
+ soltar_ignorados "$quitar" "$lineas"
194
+ }
195
+
196
+ # retirar_indice_de_nodo REL — spec 079, C13: a node with nothing to list has no index. A generated one left from
197
+ # before is deleted, with its `.gitignore` line; one written by a person stays untouched and keeps its line. The
198
+ # line is dropped also when the file is already gone (a failed cleanup of an earlier read is retried), and a
199
+ # `.gitignore` that cannot be written is reported by `soltar_ignorados`.
200
+ retirar_indice_de_nodo() {
201
+ local rel="$1"
202
+ if [ -e "$brain/$rel" ] || [ -L "$brain/$rel" ]; then
203
+ [ ! -L "$brain/$rel" ] || return 0
204
+ fm_read "$brain/$rel"
205
+ [ "$fm_generated" = "true" ] || return 0
206
+ rm -f "$brain/$rel" 2>/dev/null || return 0
207
+ fi
208
+ soltar_ignorados "$(ignorar_patron "$rel")$nl" "$rel$nl"
209
+ }
210
+
211
+ # soltar_ignorados QUITAR LINEAS — drops from the `.gitignore` the exact lines of QUITAR (one per line), writes
212
+ # it back with every other byte as it was (each kept line keeps its own terminator, an unterminated last line
213
+ # stays unterminated only when it is kept), and prints `gitignore-declared: <path>` for each path of LINEAS whose
214
+ # line was there. A file that cannot be written prints `gitignore-declare-failed: <path>` for each of them.
215
+ soltar_ignorados() {
216
+ local quitar="$1" lineas="$2" gi="$brain/.gitignore" orig out line tmp hubo rel ultima_ok=1 ok=1
217
+ [ -n "$quitar" ] && [ -f "$gi" ] || return 0
218
+ # Nothing to do when the lines read at the start are not there: the common case costs a string match.
219
+ hubo=0
220
+ while IFS= read -r line || [ -n "$line" ]; do
221
+ [ -n "$line" ] || continue
222
+ case "$ignorados" in *"$nl$line$nl"*) hubo=1; break ;; esac
223
+ done < <(printf '%s' "$quitar")
224
+ [ "$hubo" = "1" ] || return 0
225
+ orig=$(cat "$gi"; printf x); orig="${orig%x}"
226
+ out=""; hubo=0
227
+ while IFS= read -r line || [ -n "$line" ]; do
228
+ case "$nl$quitar" in *"$nl$line$nl"*) hubo=1; ultima_ok=0; continue ;; esac
229
+ out="$out$line$nl"; ultima_ok=1
230
+ done < <(printf '%s' "$orig")
231
+ [ "$hubo" = "1" ] || return 0
232
+ # The loop ends every line with a newline: a last line that had none keeps it that way, if it was kept.
233
+ case "$orig" in *"$nl") ;; *) [ "$ultima_ok" = "1" ] && out="${out%"$nl"}" ;; esac
234
+ tmp=$(mktemp "$gi.os-tmp.XXXXXX" 2>/dev/null) || ok=0
235
+ if [ "$ok" = "1" ]; then
236
+ printf '%s' "$out" > "$tmp" 2>/dev/null || { rm -f "$tmp"; ok=0; }
237
+ fi
238
+ [ "$ok" = "0" ] || os_replace_file "$tmp" "$gi" || ok=0
239
+ while IFS= read -r rel || [ -n "$rel" ]; do
240
+ [ -n "$rel" ] || continue
241
+ case "$nl$orig$nl" in
242
+ *"$nl$(ignorar_patron "$rel")$nl"*)
243
+ if [ "$ok" = "1" ]; then printf 'gitignore-declared: %s\n' "$rel"; else printf 'gitignore-declare-failed: %s\n' "$rel"; fi ;;
244
+ esac
245
+ done < <(printf '%s' "$lineas")
246
+ [ "$ok" = "0" ] || ignorados="$nl$out$nl"
247
+ return 0
248
+ }
249
+ retirar_indices_de_iniciativa
250
+
150
251
  # escribir REL CONTENT — writes the generated file at REL when its bytes differ, unless a person wrote
151
252
  # that file. The ignore entry goes first: an index is never published untracked-and-visible to git. A
152
253
  # `.gitignore` that cannot be written is reported (`gitignore-declare-failed: <path>`) and the index is
@@ -172,46 +273,70 @@ escribir() {
172
273
  return 0
173
274
  }
174
275
 
175
- # cabecera SOURCE_LINE — the frontmatter and the three lines of D8.
276
+ # cabecera TITLE DESCRIPTION SOURCE_LINE — the frontmatter, the title that says what the file is, the line
277
+ # that says what is in it (spec 079, C3) and the three lines of D8.
176
278
  cabecera() {
177
- printf -- '---\ngenerated: true\n---\n\n%s\n%s\n%s\n' \
178
- "$S_INDEX_WARNING_TITLE" "$S_INDEX_WARNING_NOT_SAVED" "$1"
279
+ printf -- '---\ngenerated: true\n---\n\n# %s\n%s\n\n%s\n%s\n%s\n' \
280
+ "$1" "$2" "$S_INDEX_WARNING_TITLE" "$S_INDEX_WARNING_NOT_SAVED" "$3"
179
281
  }
180
282
 
181
- # md_cell TEXT -> the text safe inside a table cell or a link label: backslash, `|`, `[` and `]` escaped.
182
- md_cell() {
183
- local t="$1"
184
- t="${t//\\/\\\\}"
185
- t="${t//|/\\|}"
186
- t="${t//[/\\[}"
187
- t="${t//]/\\]}"
283
+ # titulo_de_cabeza REL FALLBACK -> the `# Title` of the head at REL, else FALLBACK (the name of its folder).
284
+ titulo_de_cabeza() {
285
+ local t=""
286
+ [ -f "$brain/$1" ] && t=$(os_titulo "$brain/$1")
287
+ [ -n "$t" ] || t="$2"
188
288
  printf '%s' "$t"
189
289
  }
190
290
 
191
- # md_dest PATH -> the path as a link destination: space, parentheses, `|`, `<`, `>` and `%` encoded.
192
- md_dest() {
193
- local t="$1"
194
- t="${t//%/%25}"
195
- t="${t// /%20}"
196
- t="${t//(/%28}"
197
- t="${t//)/%29}"
198
- t="${t//|/%7C}"
199
- t="${t//</%3C}"
200
- t="${t//>/%3E}"
201
- printf '%s' "$t"
291
+ # etiqueta_estado CODE / etiqueta_horizonte CODE -> the label of a `status:` / `horizon:` value in the
292
+ # language of the brain (spec 079, C11); any other value as written, escaped for a cell.
293
+ etiqueta_estado() {
294
+ case "$1" in
295
+ active) printf '%s' "$S_INDEX_STATUS_ACTIVE" ;;
296
+ ongoing) printf '%s' "$S_INDEX_STATUS_ONGOING" ;;
297
+ closed) printf '%s' "$S_INDEX_STATUS_CLOSED" ;;
298
+ blocked) printf '%s' "$S_INDEX_STATUS_BLOCKED" ;;
299
+ *) printf '%s' "$(md_cell "$1")" ;;
300
+ esac
301
+ }
302
+ etiqueta_horizonte() {
303
+ case "$1" in
304
+ now) printf '%s' "$S_INDEX_HORIZON_NOW" ;;
305
+ next) printf '%s' "$S_INDEX_HORIZON_NEXT" ;;
306
+ later) printf '%s' "$S_INDEX_HORIZON_LATER" ;;
307
+ *) printf '%s' "$(md_cell "$1")" ;;
308
+ esac
309
+ }
310
+
311
+ # celda_tareas I FROM -> the Tasks cell of initiative I (spec 079, C1): the count of open tasks as the text of
312
+ # a link to its `backlog.md`; with no backlog, text and no link. The text and the destination are escaped
313
+ # apart, then joined.
314
+ celda_tareas() {
315
+ local i="$1" from="$2" texto
316
+ if [ -z "${i_blrel[$i]}" ]; then
317
+ printf '%s' "$(md_cell "$S_INDEX_TASKS_NO_BACKLOG")"
318
+ return 0
319
+ fi
320
+ case "${i_bl[$i]}" in
321
+ 0) texto="$S_INDEX_TASKS_NONE" ;;
322
+ 1) texto="$S_INDEX_TASKS_ONE" ;;
323
+ *) texto=$(printf "$S_INDEX_TASKS_MANY" "${i_bl[$i]}") ;;
324
+ esac
325
+ printf '[%s](%s)' "$(md_cell "$texto")" "$(md_dest "$(os_rel_link "$from" "${i_blrel[$i]}")")"
202
326
  }
203
327
 
204
- # linea_ini I FROM — the table row of initiative I, its title linked from the folder FROM. Every cell and
205
- # the link label are escaped and the destination encoded, so a row never splits and a link never breaks.
328
+ # linea_ini I FROM — the table row of initiative I, its title linked from the folder FROM to its head (spec 079,
329
+ # C1). Every cell and the link label are escaped and the destination encoded (`md_cell`, `md_dest`), so a row
330
+ # never splits and a link never breaks.
206
331
  linea_ini() {
207
332
  local i="$1" from="$2"
208
- printf '| [%s](%s) | %s | %s |' "$(md_cell "${i_title[$i]}")" "$(md_dest "$(os_rel_link "$from" "${i_rel[$i]}")")" \
209
- "$(md_cell "${i_status[$i]}")" "$(md_cell "${i_horizon[$i]}")"
333
+ printf '| [%s](%s) | %s | %s | %s |' "$(md_cell "${i_title[$i]}")" "$(md_dest "$(os_rel_link "$from" "${i_rel[$i]}")")" \
334
+ "$(etiqueta_estado "${i_status[$i]}")" "$(etiqueta_horizonte "${i_horizon[$i]}")" "$(celda_tareas "$i" "$from")"
210
335
  }
211
336
 
212
337
  # ---------------------------------------------------------------- decisions/index.md, learnings/index.md
213
338
  carpeta_index() {
214
- # carpeta_index KIND FOLDER SOURCE_LINE
339
+ # carpeta_index KIND FOLDER SOURCE_LINE TITLE DESCRIPTION
215
340
  local kind="$1" folder="$2" source="$3" body="" i
216
341
  i=0
217
342
  while [ "$i" -lt "$d_n" ]; do
@@ -221,21 +346,181 @@ carpeta_index() {
221
346
  i=$(( i + 1 ))
222
347
  done
223
348
  [ -n "$body" ] || body="$S_INDEX_EMPTY$nl"
224
- escribir "$folder/index.md" "$(cabecera "$source")$nl$nl${body%"$nl"}"
349
+ escribir "$folder/index.md" "$(cabecera "$4" "$5" "$source")$nl$nl${body%"$nl"}"
225
350
  }
226
- carpeta_index decision "$ws/decisions" "$S_INDEX_SOURCE_DECISIONS"
227
- carpeta_index learning "$ws/learnings" "$S_INDEX_SOURCE_LEARNINGS"
351
+ ws_title=$(titulo_de_cabeza "$ws/$head" "$ws_slug")
352
+ carpeta_index decision "$ws/decisions" "$S_INDEX_SOURCE_DECISIONS" "$(printf "$S_INDEX_DECISIONS_TITLE" "$ws_title")" "$S_INDEX_DECISIONS_DESC"
353
+ carpeta_index learning "$ws/learnings" "$S_INDEX_SOURCE_LEARNINGS" "$(printf "$S_INDEX_LEARNINGS_TITLE" "$ws_title")" "$S_INDEX_LEARNINGS_DESC"
228
354
 
229
- # ---------------------------------------------------------------- the index of each area node
355
+ # ---------------------------------------------------------------- the documents of an index (spec 078)
230
356
  seccion() {
231
- # seccion TITLE BODY — the heading, a blank line and the lines, with no trailing newline.
357
+ # seccion TITLE BODY — the heading and the body right under it, with no trailing newline; an empty body
358
+ # says there is nothing yet (a section with nothing in it has no table).
232
359
  if [ -n "$2" ]; then
233
- printf '## %s\n\n%s' "$1" "${2%"$nl"}"
360
+ printf '## %s\n%s' "$1" "${2%"$nl"}"
234
361
  else
235
- printf '## %s\n\n%s' "$1" "$S_INDEX_EMPTY"
362
+ printf '## %s\n%s' "$1" "$S_INDEX_EMPTY"
236
363
  fi
237
364
  }
238
365
 
366
+ # tabla HEADER_CELLS ROWS -> the table (header row, separator, rows), or nothing when there are no rows.
367
+ # HEADER_CELLS are the already-translated column names separated by a tab.
368
+ tabla() {
369
+ local cols="$1" rows="$2" h="" sep="" c
370
+ [ -n "$rows" ] || return 0
371
+ while IFS= read -r c || [ -n "$c" ]; do
372
+ h="$h $c |"
373
+ sep="$sep---|"
374
+ done <<COLS
375
+ ${cols//$tab/$nl}
376
+ COLS
377
+ printf '|%s%s|%s%s%s' "$h" "$nl" "$sep" "$nl" "${rows%"$nl"}"
378
+ }
379
+
380
+ # fecha_valida STR -> 0 when STR is a real day written `YYYY-MM-DD`. Anything else counts as no date.
381
+ fecha_valida() {
382
+ local d="$1" y m dd max
383
+ case "$d" in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) ;; *) return 1 ;; esac
384
+ y=$(( 10#${d:0:4} )); m=$(( 10#${d:5:2} )); dd=$(( 10#${d:8:2} ))
385
+ [ "$y" -ge 1 ] && [ "$m" -ge 1 ] && [ "$m" -le 12 ] && [ "$dd" -ge 1 ] || return 1
386
+ case "$m" in
387
+ 1|3|5|7|8|10|12) max=31 ;;
388
+ 4|6|9|11) max=30 ;;
389
+ *)
390
+ if [ $(( y % 400 )) -eq 0 ] || { [ $(( y % 4 )) -eq 0 ] && [ $(( y % 100 )) -ne 0 ]; }; then max=29; else max=28; fi
391
+ ;;
392
+ esac
393
+ [ "$dd" -le "$max" ]
394
+ }
395
+
396
+ # doc_record REL BASE FROM -> one record `sort-date TAB title TAB path TAB link TAB date` for the file at REL
397
+ # (relative to the brain). The date is the `date:` key of a `.md`, else the `YYYY-MM-DD` prefix of the file
398
+ # name; a value that is not a real day is no date (the fourth field is then empty), and no date sorts last. The
399
+ # title is `os_doc_title` for a `.md`, and the path relative to BASE for any other file. The link goes from the
400
+ # folder FROM.
401
+ doc_record() {
402
+ local rel="$1" base="$2" from="$3" name t d sd fecha
403
+ name="${rel##*/}"
404
+ d=""
405
+ case "$name" in
406
+ *.md)
407
+ t=$(os_doc_title "$brain/$rel")
408
+ fm_read "$brain/$rel"
409
+ d="$fm_date"
410
+ [ -n "$d" ] || d="${name:0:10}"
411
+ ;;
412
+ *)
413
+ t="${rel#"$base"/}"
414
+ d="${name:0:10}"
415
+ ;;
416
+ esac
417
+ t="${t//[[:cntrl:]]/ }"
418
+ sd="0000-00-00"
419
+ fecha=""
420
+ if fecha_valida "$d"; then sd="$d"; fecha="$d"; fi
421
+ printf '%s\t%s\t%s\t[%s](%s)\t%s\n' "$sd" "$t" "${rel//$tab/%09}" "$(md_cell "$t")" "$(md_dest "$(os_rel_link "$from" "$rel")")" "$fecha"
422
+ }
423
+
424
+ # docs_sorted LABEL BASE FROM -> the table rows `| LABEL | date | link |` of the files whose paths come on
425
+ # stdin: the dated first, newest date first; the same date, by title and then by path; the undated last, by
426
+ # title and then by path.
427
+ docs_sorted() {
428
+ local label="$1" base="$2" from="$3" rel
429
+ while IFS= read -r rel || [ -n "$rel" ]; do
430
+ [ -n "$rel" ] || continue
431
+ doc_record "$rel" "$base" "$from"
432
+ done | LC_ALL=C sort -t "$tab" -k1,1r -k2,2 -k3,3 | while IFS="$tab" read -r _d _t _r l f; do
433
+ printf '| %s | %s | %s |\n' "$(md_cell "$label")" "$(md_cell "$f")" "$l"
434
+ done
435
+ }
436
+
437
+ # limpiar TEXT -> TEXT with every control character (tab, newline...) turned into a space: a field that goes into a
438
+ # tab-separated record can never split it.
439
+ limpiar() {
440
+ printf '%s' "${1//[[:cntrl:]]/ }"
441
+ }
442
+
443
+ # tipo_de_titulo TITLE -> the tag at the start of a title, `[producto] Algo` -> `Producto` (first ASCII letter
444
+ # raised); empty when the title has no tag or nothing after it.
445
+ tipo_de_titulo() {
446
+ local re='^\[([^]]+)\][[:space:]]*(.+)$' tag f idx lower=abcdefghijklmnopqrstuvwxyz upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
447
+ [[ "$1" =~ $re ]] || return 0
448
+ tag="${BASH_REMATCH[1]}"
449
+ f="${tag:0:1}"
450
+ idx="${lower%%"$f"*}"
451
+ if [ "${#idx}" -lt 26 ]; then tag="${upper:${#idx}:1}${tag:1}"; fi
452
+ printf '%s' "$tag"
453
+ }
454
+
455
+ # titulo_sin_etiqueta TITLE -> the title without the tag `tipo_de_titulo` read.
456
+ titulo_sin_etiqueta() {
457
+ local re='^\[([^]]+)\][[:space:]]*(.+)$'
458
+ [[ "$1" =~ $re ]] || { printf '%s' "$1"; return 0; }
459
+ printf '%s' "${BASH_REMATCH[2]}"
460
+ }
461
+
462
+ # fila_decision REL FROM -> a record `class TAB date TAB title TAB path TAB row` (spec 079, C12): class 0 for a
463
+ # key decision that was not replaced, 1 for the others (the node index prints them in two tables). The row is `| date | [title](link) | type | when |`.
464
+ fila_decision() {
465
+ local rel="$1" from="$2" title date key note sup clase=1 shown tipo="" cuando="" sup_title sd
466
+ title=$(os_doc_title "$brain/$rel")
467
+ fm_read "$brain/$rel"
468
+ date="$fm_date"; key="$fm_key"; note="$fm_index_note"; sup=$(os_doc_superseded_path)
469
+ # The tag of the title becomes the Type and leaves the title, for every decision; a replaced one then says
470
+ # «Reemplazada» instead (its tag is not shown twice).
471
+ tipo=$(tipo_de_titulo "$title")
472
+ shown="$title"
473
+ [ -z "$tipo" ] || shown=$(titulo_sin_etiqueta "$title")
474
+ if os_doc_superseded; then
475
+ tipo="$S_INDEX_TYPE_SUPERSEDED"
476
+ if [ -n "$sup" ]; then
477
+ if [ -f "$brain/$sup" ]; then sup_title=$(os_doc_title "$brain/$sup"); else sup_title="${sup##*/}"; fi
478
+ cuando="$S_INDEX_SUPERSEDED_BY_CELL [$(md_cell "$(limpiar "$sup_title")")]($(md_dest "$(os_rel_link "$from" "$sup")"))"
479
+ fi
480
+ else
481
+ os_doc_is_key "$key" && clase=0
482
+ fi
483
+ if [ -z "$cuando" ]; then
484
+ case "$note" in
485
+ '❓'*|'📌'*|'🔍'*) note="" ;;
486
+ esac
487
+ cuando="$(md_cell "$(limpiar "$note")")"
488
+ fi
489
+ date=$(limpiar "$date"); shown=$(limpiar "$shown"); tipo=$(limpiar "$tipo")
490
+ sd="${date:-0000-00-00}"
491
+ printf '%s\t%s\t%s\t%s\t| %s | [%s](%s) | %s | %s |\n' "$clase" "$sd" "$shown" "${rel//$tab/%09}" \
492
+ "$(md_cell "$date")" "$(md_cell "$shown")" "$(md_dest "$(os_rel_link "$from" "$rel")")" "$(md_cell "$tipo")" "$cuando"
493
+ }
494
+
495
+ # fila_aprendizaje REL FROM -> a record `date TAB title TAB path TAB row`, the row `| date | [title](link) |`.
496
+ fila_aprendizaje() {
497
+ local rel="$1" from="$2" title date sd
498
+ title=$(os_doc_title "$brain/$rel")
499
+ fm_read "$brain/$rel"
500
+ date=$(limpiar "$fm_date")
501
+ sd="${date:-0000-00-00}"
502
+ title=$(limpiar "$title")
503
+ printf '%s\t%s\t%s\t| %s | [%s](%s) |\n' "$sd" "$title" "${rel//$tab/%09}" \
504
+ "$(md_cell "$date")" "$(md_cell "$title")" "$(md_dest "$(os_rel_link "$from" "$rel")")"
505
+ }
506
+
507
+ # ultimo_campo -> the last tab-separated field of each record that comes on stdin, in the order they come.
508
+ ultimo_campo() {
509
+ local r
510
+ while IFS= read -r r || [ -n "$r" ]; do
511
+ [ -n "$r" ] || continue
512
+ printf '%s\n' "${r##*$tab}"
513
+ done
514
+ }
515
+
516
+ # ---------------------------------------------------------------- the index of each area node
517
+ contents=$(os_tree_content_files "$brain" 2>/dev/null | LC_ALL=C sort -u) || contents=""
518
+
519
+ # filas_iniciativas -> the sorted rows of the initiatives that come on stdin as `rank TAB title TAB path TAB row`.
520
+ filas_iniciativas() {
521
+ LC_ALL=C sort -t "$tab" -k1,1 -k2,2 -k3,3 | ultimo_campo
522
+ }
523
+
239
524
  while IFS= read -r node || [ -n "$node" ]; do
240
525
  [ -n "$node" ] || continue
241
526
  case "$node" in "$ws"/*/*/*) ;; *) continue ;; esac
@@ -244,38 +529,109 @@ while IFS= read -r node || [ -n "$node" ]; do
244
529
  *) continue ;;
245
530
  esac
246
531
  from="${node%/*}"
247
- inis=""; decs=""; lrns=""
532
+ inis_open=""; inis_closed=""; decs=""; lrns=""
248
533
  i=0
249
534
  while [ "$i" -lt "$i_n" ]; do
250
535
  case "${i_about[$i]}" in
251
- *"$nl$node$nl"*) inis="$inis$(linea_ini "$i" "$from")$nl" ;;
536
+ *"$nl$node$nl"*)
537
+ t="${i_title[$i]//$tab/ }"
538
+ pth="${i_rel[$i]//$tab/%09}"
539
+ if [ "${i_status[$i]}" = "closed" ]; then
540
+ inis_closed="${inis_closed}0$tab$t$tab$pth$tab$(linea_ini "$i" "$from")$nl"
541
+ else
542
+ case "${i_horizon[$i]}" in now) rk=0 ;; next) rk=1 ;; later) rk=2 ;; *) rk=3 ;; esac
543
+ inis_open="$inis_open$rk$tab$t$tab$pth$tab$(linea_ini "$i" "$from")$nl"
544
+ fi
545
+ ;;
252
546
  esac
253
547
  i=$(( i + 1 ))
254
548
  done
549
+ # Initiatives: the open ones by horizon (the codes, not the labels), title and path; the closed ones by title.
550
+ cols_ini="$S_INDEX_COL_INITIATIVE$tab$S_INDEX_COL_STATUS$tab$S_INDEX_COL_HORIZON$tab$S_INDEX_COL_TASKS"
551
+ rows="$(printf '%s' "$inis_open" | filas_iniciativas)"
552
+ sec_open="$(seccion "$S_INDEX_INITIATIVES_OPEN" "$(tabla "$cols_ini" "$rows")")"
553
+ rows="$(printf '%s' "$inis_closed" | filas_iniciativas)"
554
+ sec_closed=""
555
+ [ -z "$rows" ] || sec_closed="$nl$nl$(seccion "$S_INDEX_INITIATIVES_CLOSED" "$(tabla "$cols_ini" "$rows")")"
556
+ # Decisions and learnings about the node.
557
+ recs_d=""; recs_l=""
255
558
  i=0
256
559
  while [ "$i" -lt "$d_n" ]; do
257
560
  case "${d_about[$i]}" in
258
561
  *"$nl$node$nl"*)
259
- l="$(os_doc_index_line "$brain" "${d_rel[$i]}" "$from")$nl"
260
- if [ "${d_kind[$i]}" = "decision" ]; then decs="$decs$l"; else lrns="$lrns$l"; fi
562
+ if [ "${d_kind[$i]}" = "decision" ]; then
563
+ recs_d="$recs_d$(fila_decision "${d_rel[$i]}" "$from")$nl"
564
+ else
565
+ recs_l="$recs_l$(fila_aprendizaje "${d_rel[$i]}" "$from")$nl"
566
+ fi
261
567
  ;;
262
568
  esac
263
569
  i=$(( i + 1 ))
264
570
  done
265
- idx="$from/index.md"
266
- if [ -n "$inis" ]; then
267
- inis="| $S_INDEX_COL_INITIATIVE | $S_INDEX_COL_STATUS | $S_INDEX_COL_HORIZON |$nl|---|---|---|$nl$inis"
571
+ # Decisions: two tables like the initiatives, the key ones that were not replaced and all the others (C12).
572
+ cols_dec="$S_INDEX_COL_DATE$tab$S_INDEX_COL_DECISION$tab$S_INDEX_COL_TYPE$tab$S_INDEX_COL_WHEN_TO_READ"
573
+ rows_k="$(printf '%s' "$recs_d" | LC_ALL=C sort -t "$tab" -k1,1 -k2,2r -k3,3 -k4,4 | while IFS= read -r r || [ -n "$r" ]; do
574
+ [ "${r%%$tab*}" = "0" ] && printf '%s\n' "$r"; done | ultimo_campo)"
575
+ rows_o="$(printf '%s' "$recs_d" | LC_ALL=C sort -t "$tab" -k1,1 -k2,2r -k3,3 -k4,4 | while IFS= read -r r || [ -n "$r" ]; do
576
+ [ "${r%%$tab*}" = "1" ] && printf '%s\n' "$r"; done | ultimo_campo)"
577
+ decs=""
578
+ if [ -z "$rows_k$rows_o" ]; then
579
+ decs="$(seccion "$S_INDEX_DECISIONS" "")"
580
+ else
581
+ [ -z "$rows_k" ] || decs="$(seccion "$S_INDEX_DECISIONS_KEY" "$(tabla "$cols_dec" "$rows_k")")"
582
+ [ -z "$rows_o" ] || decs="$decs${decs:+$nl$nl}$(seccion "$S_INDEX_DECISIONS_OTHER" "$(tabla "$cols_dec" "$rows_o")")"
268
583
  fi
269
- if [ -z "$decs$lrns" ] && [ -z "$inis" ]; then
270
- # Nobody names this node: a generated index left from before goes; one a person wrote stays.
271
- if [ -f "$brain/$idx" ]; then
272
- fm_read "$brain/$idx"
273
- [ "$fm_generated" = "true" ] && rm -f "$brain/$idx"
584
+ lrns="$(tabla "$S_INDEX_COL_DATE$tab$S_INDEX_COL_LEARNING" \
585
+ "$(printf '%s' "$recs_l" | LC_ALL=C sort -t "$tab" -k1,1r -k2,2 -k3,3 | ultimo_campo)")"
586
+ # The node's own documents: what the tree reaches directly in its `context/`, `research/` and
587
+ # `resources/`, one folder after the other, never deeper than the tree goes.
588
+ g_context=""; g_research=""; g_resources=""
589
+ while IFS= read -r f || [ -n "$f" ]; do
590
+ [ -n "$f" ] || continue
591
+ [ -f "$brain/$f" ] || continue
592
+ case "${f##*/}" in *.os-tmp.*) continue ;; esac
593
+ if [ "${f##*/}" = "index.md" ]; then
594
+ fm_read "$brain/$f"
595
+ [ "$fm_generated" = "true" ] && continue
274
596
  fi
597
+ case "$f" in
598
+ "$from/context/"*) g_context="$g_context$f$nl" ;;
599
+ "$from/research/"*) g_research="$g_research$f$nl" ;;
600
+ "$from/resources/"*) g_resources="$g_resources$f$nl" ;;
601
+ esac
602
+ done <<CONTENTS
603
+ $contents
604
+ CONTENTS
605
+ docs=""
606
+ for g in context research resources; do
607
+ case "$g" in
608
+ context) list="$g_context"; label="$S_INDEX_FOLDER_CONTEXT" ;;
609
+ research) list="$g_research"; label="$S_INDEX_FOLDER_RESEARCH" ;;
610
+ *) list="$g_resources"; label="$S_INDEX_FOLDER_RESOURCES" ;;
611
+ esac
612
+ [ -n "$list" ] || continue
613
+ docs="$docs$(docs_sorted "$label" "$from" "$from" <<LIST
614
+ $list
615
+ LIST
616
+ )$nl"
617
+ done
618
+ docs="$(tabla "$S_INDEX_COL_FOLDER$tab$S_INDEX_COL_DATE$tab$S_INDEX_COL_DOCUMENT" "$docs")"
619
+ # A node with nothing to list (no initiative, decision or learning about it, no document of its own) has no index.
620
+ if [ -z "$inis_open$inis_closed$recs_d$recs_l$docs" ]; then
621
+ retirar_indice_de_nodo "$from/index.md"
275
622
  continue
276
623
  fi
277
- contenido="$(cabecera "$S_INDEX_SOURCE_NODE")$nl$nl$(seccion "$S_INDEX_INITIATIVES" "$inis")$nl$nl$(seccion "$S_INDEX_DECISIONS" "$decs")$nl$nl$(seccion "$S_INDEX_LEARNINGS" "$lrns")"
278
- escribir "$idx" "$contenido"
624
+ # The title and the line that say what this index is: the type of the node comes from the folder it lives in.
625
+ case "${node#"$ws"/}" in
626
+ products/*) desc="$S_INDEX_NODE_DESC_PRODUCT" ;;
627
+ courses/*) desc="$S_INDEX_NODE_DESC_COURSE" ;;
628
+ channels/*) desc="$S_INDEX_NODE_DESC_CHANNEL" ;;
629
+ accounts/*) desc="$S_INDEX_NODE_DESC_ACCOUNT" ;;
630
+ *) desc="$S_INDEX_NODE_DESC_NODE" ;;
631
+ esac
632
+ node_title=$(titulo_de_cabeza "$node" "$(os_entity_name "$node")")
633
+ contenido="$(cabecera "$(printf "$S_INDEX_NODE_TITLE" "$node_title")" "$desc" "$S_INDEX_SOURCE_NODE")$nl$nl$OS_FOCUS_START$nl$sec_open$nl$nl$OS_FOCUS_END$sec_closed$nl$nl$decs$nl$nl$(seccion "$S_INDEX_LEARNINGS" "$lrns")$nl$nl$(seccion "$S_INDEX_DOCUMENTS" "$docs")"
634
+ escribir "$from/index.md" "$contenido"
279
635
  done <<NODES
280
636
  $(printf '%s\n' "$heads" | LC_ALL=C sort -u)
281
637
  NODES